1、求字符串最後一個單詞長度
- 計算字符串最後一個單詞的長度,單詞以空格隔開。
import java.util.Scanner;
/**
* @Author: Stephen
* @Date: 2020/3/21 13:24
* @Content: 計算字符串最後一個單詞的長度,單詞以空格隔開。
*/
public class StrLength01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()){
String[] str = input.nextLine().split(" ",-1);
System.out.println(str[str.length-1].length());
}
}
}
2、計算字符串個數
- 寫出一個程序,接受一個由字母和數字組成的字符串,和一個字符,
- 然後輸出輸入字符串中含有該字符的個數。不區分大小寫。
import java.util.Scanner;
/**
* @Author: Stephen
* @Date: 2020/3/21 14:17
* @Content: 寫出一個程序,接受一個由字母和數字組成的字符串,和一個字符,
* 然後輸出輸入字符串中含有該字符的個數。不區分大小寫。
*/
public class WordCount {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str = input.nextLine();
String cha = input.nextLine();
int count = 0;
if (str != null && str.length()>0){
for (int i=0;i<str.length();i++){
if (cha.toLowerCase().charAt(0)==str.toLowerCase().charAt(i)){
count++;
}
}
}
System.out.println(count);
}
}
3、明明的隨機數
- 明明想在學校中請一些同學一起做一項問卷調查,爲了實驗的客觀性,他先用計算機生成了N個1到1000之間的隨機整數(N≤1000),
- 對於其中重複的數字,只保留一個,把其餘相同的數去掉,不同的數對應着不同的學生的學號。
- 然後再把這些數從小到大排序,按照排好的順序去找同學做調查。
- 請你協助明明完成“去重”與“排序”的工作(同一個測試用例裏可能會有多組數據,希望大家能正確處理)。
import java.util.*;
/**
* @Author: Stephen
* @Date: 2020/3/21 14:45
* @Content:
* 明明想在學校中請一些同學一起做一項問卷調查,爲了實驗的客觀性,他先用計算機生成了N個1到1000之間的隨機整數(N≤1000),
* 對於其中重複的數字,只保留一個,把其餘相同的數去掉,不同的數對應着不同的學生的學號。
* 然後再把這些數從小到大排序,按照排好的順序去找同學做調查。
* 請你協助明明完成“去重”與“排序”的工作(同一個測試用例裏可能會有多組數據,希望大家能正確處理)。
*/
public class RandomTest {
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
while (num.hasNext()){
int number = num.nextInt();
Set<Integer> numbers = new HashSet<Integer>();
List<Integer> list = new ArrayList<Integer>();
for (int i =0;i<number;i++){
// 曲解了題意誤以爲隨機輸入是用隨機數輸入
// Random rand = new Random();
// int a=rand.nextInt(1001);
numbers.add(num.nextInt());
}
for (int a:numbers){
list.add(a);
}
Collections.sort(list);
for (int a:list){
Systems.out,println(a);
}
}
}
}
寫的時候是用hashset去重再轉arrayList排序,經閱讀代碼補充使用treeset直接去重排序treeset詳解
import java.util.*;
public class RandomTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int num = sc.nextInt();
TreeSet<Integer> set = new TreeSet<Integer>();
for(int i = 0 ; i < num ;i++){
int curr = sc.nextInt();
set.add(curr);
}
for(Integer i : set){
System.out.println(i);
}
}
}
}
4、字符串分割
- 連續輸入字符串,請按長度爲8拆分每個字符串後輸出到新的字符串數組;
- 長度不是8整數倍的字符串請在後面補數字0,空字符串不處理。
import java.util.Scanner;
/**
* @Author: Stephen
* @Date: 2020/3/21 15:38
* @Content:
* •連續輸入字符串,請按長度爲8拆分每個字符串後輸出到新的字符串數組;
* •長度不是8整數倍的字符串請在後面補數字0,空字符串不處理。
*/
public class StrSplit {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()){
String str = input.nextLine();
while (str.length()>=8){
System.out.println(str.substring(0,8));
str=str.substring(8);
}
if (str.length()<8 && str.length()>0){
str = str+"0000000";
System.out.println(str.substring(0,8));
}
}
}
}
5、進制轉換
- 寫出一個程序,接受一個十六進制的數,輸出該數值的十進制表示。(多組同時輸入 )
import java.util.Scanner;
/**
* @Author: Stephen
* @Date: 2020/3/21 15:57
* @Content:
* 寫出一個程序,接受一個十六進制的數,輸出該數值的十進制表示。(多組同時輸入 )
*/
public class SystemTransform {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
System.out.println(fun(str.substring(2)));
}
}
public static int fun(String s){
int n=0;
int count= 0;
int temp = 0;
char ch;
while(count<s.length())
{
ch = s.charAt(s.length()-count-1);
if(ch>='0'&&ch<='9'){
temp = ch-'0';
}else if(ch>='A'&&ch<='Z'){
temp = ch-'A'+10;
}else if(ch>='a'&&ch<='z'){
temp = ch-'a'+10;
}else{
break;
}
n += temp*Math.pow(16,count);
count++;
}
return n;
}
}
6、質數因子
- 輸入一個正整數,按照從小到大的順序輸出它的所有質因子(如180的質因子爲2 2 3 3 5 )
- 最後一個數後面也要有空格
package com.njbdqn.services;
import java.util.Scanner;
/**
* @Author: Stephen
* @Date: 2020/3/23 21:46
* @Content:
* 輸入一個正整數,按照從小到大的順序輸出它的所有質因子(如180的質因子爲2 2 3 3 5 )
* 最後一個數後面也要有空格
*/
public class Primefactors {
public static void main(String[] args) {
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
long params=sc.nextLong();
if(params<2)
{
sc.close();
return ;
}
String result =getResult(params);
System.out.println(result);
sc.close();
}
}
public static String getResult(long ulDataInput){
StringBuilder str=new StringBuilder();
int index=2;
while(index<=ulDataInput)
{
if(ulDataInput%index==0){
if(index==ulDataInput){
str.append(index).append(" ");
break;
}else{
str.append(index).append(" ");
ulDataInput=ulDataInput/index;
}
}else
{
index++;
}
}
return str.toString();
}