- 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); } } }