- 11.【题目描述】- 【数字颠倒】
【答案参考】- 提交成功
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num = in.nextInt();
StringBuilder sbd = new StringBuilder().append(num);
StringBuilder reverse = sbd.reverse();
System.out.println(reverse);
}
}
知识点:
StringBuilder、StringBuffer的方法append()和reverse()
- 12.【题目描述】- 【字符串翻转】
【答案参考】- 提交成功
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
StringBuffer input = new StringBuffer(in.nextLine());
System.out.print(input.reverse());
}
}
13.【题目描述】- 【句子逆序】
【答案参考】- 提交成功
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String[] strs = in.nextLine().split(" ");
StringBuffer sbf = new StringBuffer();
for(int i=strs.length-1;i>=0;i--){
sbf.append(strs[i]+" ");
}
System.out.print(sbf.toString().trim());
}
}
知识点:
String的split()和trim()方法;StringBuilder、StringBuffer的toString()方法
14.【题目描述】- 【字串的连接最长路径查找】
【答案参考】- 提交成功
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num = Integer.parseInt(in.nextLine());
String[] inputs = new String[num];
for (int i = 0; i < num; i++) {
inputs[i]=in.nextLine();
}
Arrays.sort(inputs);
for (int i = 0; i < num; i++) {
System.out.println(inputs[i]);
}
}
}
知识点:
Integer.parseInt(String)和Arrays.sort(arr)方法
- 15.【题目描述】- 【求int型正整数在内存中存储时1的个数】
【答案参考】- 提交成功
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num = in.nextInt();
String str = Integer.toBinaryString(num);
int count =0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='1'){
count++;
}
}
System.out.println(count);
}
}
知识点:
Integer.toBinaryString(int i)
String.charAt(int index)
扩展题一:
【题目描述】【分别统计字符串中的字母、汉字、数字个数】
public class Main{ public static void strCount(){ String str2 = "发发风HFSD4655DSAJKD的发放日阿女发发dfh465sjfh到科技馆恐惧感"; int en = 0; int ch = 0; int num = 0; for (int i = 0; i < str2.length(); i++) { char b = str2.charAt(i); if ((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z')) { en += 1; } else if (b >= '0' && b <= '9') { num += 1; } else { ch += 1; } } System.out.println("2. 字母:" + en + "\t汉字:" + ch + "\t数字:" + num); } }
扩展题二:
【题目描述】【分别统计字符串中出现的所有字符的个数】
public class Main{ public static void main(String[] args){ String str = "6hj$#%&*()IGRGjI6hj$#%&@&**)^$j@$#^&)(^&$6hj$#%&(RG@@IIjh6hj$#%&65"; Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < str.length(); i++) { Character a = str.charAt(i); Integer count = map.get(a); if (count == null) { count = 1; map.put(a, count); } else { count += 1; map.put(a, count); } } System.out.println("1. " + map.toString()); } }
- 17.【题目描述】- 【座标移动】
【答案参考】- IDE编译成功
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNextLine()){ String readLine = in.nextLine(); String[] strs = readLine.split(";"); int leftAndRight = 0; int upAndDown = 0; for(int i = 0;i<strs.length;i++){ boolean flag=strs[i].matches("^[A|W|D|S][1-9][0-9]?$"); if(flag){ String first = strs[i].substring(0,1); int num = Integer.parseInt(strs[i].substring(1)); switch(first){ case "A": leftAndRight -= num; break; case "D": leftAndRight += num; break; case "W": upAndDown += num; break; case "S": upAndDown -= num; break; } } } System.out.println(leftAndRight+","+upAndDown); } } }