1、字符串最后一个单词的长度
思路:
1、用String下的split()切割方法,对空格处进行切割,切割为字符串数组;
2、判断最后一位是否是空格,若是,则计算倒数第二位置字符串长度,
否则,直接打印出最后一个字符串长度即可。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
if(str==null){
return;
}
String[] ss = str.split(" ");
int i = ss.length-1;
while(ss[i].equals(" ") && i>=0){
--i;
}
System.out.println(ss[i].length());
}
}
2、计算某字符出现次数
思路:
1、使用string下的toLowerCase()方法,将字符串字母转为对应小写字母;
2、将对应要匹配的字符也转为小写字母;
3、循环遍历字符串每一个字母,判断是否与要匹配字符相同;
4、若相同,计数器值加1即可。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().toLowerCase();
String ch = sc.nextLine().toLowerCase();
char c = ch.charAt(0);
int total = 0;
for(int i = 0; i < str.length(); i++) {
if(c == str.charAt(i)) {
total++;
}
}
System.out.println(total);
}
}
3、明明的随机数
思路:
可以使用TreeSet集合,去重加排序;
1、循环遍历共要输入的数据行,一次将数据添加到集合;
2、循环遍历集合,打印出数据即可;
import java.util.Scanner;
import java.util.TreeSet;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
TreeSet<Integer> array = new TreeSet();
for(int i=0;i<n;i++){
int b=in.nextInt();
array.add(b);
}
//循环遍历数组
for(Integer g : array){
System.out.println(g);
}
}
}
4、字符串分割
思路:
1、利用String下的toCharArray()方法将字符串转为字符数组;
2、循环遍历数组,不换行打印字符,判断是否打印了8个,若是,每间隔8个字符打印一个换行;
3、判断是否每次打印都刚好8个字符,如果不够,for循环打印补0
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//接收字符串
String s = sc.nextLine();
//将字符串转为字符串数组
char[] chars = s.toCharArray();
//每输出8个字符换行
for (int i = 1; i < chars.length + 1; i++) {
System.out.print(chars[i - 1]);
if (i % 8 == 0) {
System.out.println();
}
}
//如果长度不为8,补0
if (chars.length % 8 != 0) {
for (int j = 0; j < 9 - chars.length % 8-1; j++) {
System.out.print('0');
}
}
}
}
5、进制的转换
思路分析(十六进制转为十进制):
1、判断字符串是否包含“0x”,若包含,利用String下的substring()截取方法,将“0x”截取掉;
2、使用Integer包装数据类型下的parseInt(字符串,16)方法,即将一个16进制数转为十进制。
parseInt(字符串)默认为十进制数。
import java.util.Scanner;
import java.lang.Integer;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String inputStr = in.next();
if (inputStr.contains("0x")) {
inputStr = inputStr.substring(2, inputStr.length());
}
System.out.println(Integer.parseInt(inputStr, 16));
}
}
6、质数因子
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int num=in.nextInt();
int y=2;
while(num!=1){
if(num%y==0){
num/=y;
System.out.print(y+" ");
}else{
if(y>num/y){
y=num;
}else{
y++;
}
}
}
}
}
}
7、取近似值
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double d = in.nextDouble();
System.out.print(d+0.5>=(int)d+1?(int)d+1:(int)d);
}
}
8、合并表记录
1、可以利用TreeSet集合进行去重及排序;
2、for循环,依次往集合添加index和value;
添加value时对index相同的,value值相加可以使用Map下的getOrDefault()方法;
map.put(index,map.getOrDefault(index,0)+value)表示若key值index不存在,则
value等于0+value,若index存在,则value=index[0]+value。
import java.util.Scanner;
import java.util.TreeMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
//多行
Scanner sc = new Scanner(System.in);
//一共多少数据行
int count = sc.nextInt();
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i = 0; i < count; i++) {
int index = sc.nextInt();
int value = sc.nextInt();
map.put(index,map.getOrDefault(index,0)+value);
}
map.forEach((a,b)-> System.out.println(a+" "+b));
}
}
9、提取不重复的整数
思路分析:
1、利用HashSet集合进行去重;
2、for循环遍历得到每一个倒序字符,并添加到set集合中进行去重;
import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
Set<Character> set = new HashSet<>();
for (int i = s.length()-1; i >=0 ; i--) {
if (set.add(s.charAt(i))){
System.out.print(s.charAt(i));
}
}
}
}
10、字符个数统计
思路:
1、计算字符串中所含不同字符个数,可以使用set集合进行去重,计算长度;
2、将字符串循环遍历,得到每一个字符,将字符添加到set集合中,集合去重;
3、计算集合的长度即可。
import java.util.Scanner;
import java.util.HashSet;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
HashSet<Character> set = new HashSet<>();
//获取到每一个字符
for (int i = 0; i < s.length(); i++) {
set.add(s.charAt(i));
}
System.out.println(set.size());
}
}
11、数字颠倒
思路:
1、可直接使用StringBuffer下的reverse()反转方法。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String sc = in.nextLine();
String s= new StringBuffer(sc).reverse().toString();
System.out.println(s);
}
}
12、字符串反转
同11题
Scanner in = new Scanner(System.in);
String sc = in.nextLine();
String s= new StringBuffer(sc).reverse().toString();
System.out.println(s);
13、句子逆序
思路:
1、通过String下的split()方法,将空格处分割成字符串数组;
2、循环遍历数组,通过倒序将每一个字符串输出即可。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String sc = in.nextLine();
String[] s=sc.split(" ");
for(int i=s.length-1;i>=0;i--){
System.out.print(s[i]+" ");
}
}
}
14、字符串排序
思路:
1、new一个字符串数组,将输入的每个字符串赋值给每一个索引数组;
2、用数组下的sort()方法进行排序即可。
注意:不要用TreeSet集合的自带排序功能,因为TreeSet默认去重,需要重写方法,比较麻烦。
import java.util.Scanner;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int row = in.nextInt();
String[] a=new String[row];
for (int i = 0; i <row; i++) {
a[i]= in.next();
}
Arrays.sort(a);
for (String s : a) {
System.out.println(s);
}
}
}
15、求int型正整数在内存中存储时1的个数
在这里插入代码片
在这里插入代码片