文章目录
1、求字符串最后一个单词长度
2、计算字符串个数
3、明明的随机数
4、字符串分割
5、进制转换
6、质数因子
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());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
if (cha.toLowerCase().charAt(0)==str.toLowerCase().charAt(i)){
count++;
}
}
}
System.out.println(count);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
List
for (int i =0;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);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
写的时候是用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
for(int i = 0 ; i < num ;i++){
int curr = sc.nextInt();
set.add(curr);
}
for(Integer i : set){
System.out.println(i);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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));
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
————————————————
版权声明:本文为CSDN博主「beautiful_huang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/beautiful_huang/article/details/105009791