文章目录[隐藏]
QuestionA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); String str = cin.next(); String str2 =""; for(int i=0;i<str.length();i++){ if(str.charAt(i)>=65&&str.charAt(i)<=90||str.charAt(i)>=97&&str.charAt(i)<=122) str2=str2+str.charAt(i); } System.out.println(str2); //TODO cin.close(); } } |
对输入的字符串进行遍历,charAt访问string字符串字符,大写英文字母字符65-90;小写英文字符97-122,要记住。java字符串可以直接拼接,并且string拼接后的字符串其实是jvm中新的一个字符串,这也是为什么使用太多字符串常量消耗空间,这里说多了。直接判断后进行拼接即可,输出str2。
QuestionB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.*; public class Main { static String check(String str){ //去掉字符串中多余的符号,只留下字母 String str2 =""; for(int i=0;i<str.length();i++){ if(str.charAt(i)>=65&&str.charAt(i)<=90||str.charAt(i)>=97&&str.charAt(i)<=122) str2=str2+str.charAt(i); } return str2; } public static void main(String[] args) { Scanner cin = new Scanner(System.in); //TODO String str = cin.next(); String str2 = check(str); System.out.println(str2.length()); cin.close(); } } |
这里,写了一个去掉非字母的字符串函数,调用函数去掉输入的字符串的非字母后,直接输出字符串的长度即可。
QuestionC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); //TODO int n = cin.nextInt(); int i=0; int sum=0; for(i=1;;i++){ sum=i*i*i+sum; if(sum>n) break; } System.out.println(i-1); cin.close(); } } |
暴力循环求解即可, 没什么好说的
QuestionD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.*; public class Main { static boolean check(String s){ String t =new StringBuffer(s).reverse().toString(); return s.equals(t); } public static void main(String[] args) { Scanner cin = new Scanner(System.in); //TODO while(cin.hasNext()){ String s =cin.next(); if(check(s)){ System.out.println("Y"); }else{ System.out.println("N"); } } cin.close(); } } |
使用stringbuffer中的reverse事半功倍,string 转为stringbuffer 直接new stringbuffer(str) ,stringbuffer转为str使用tostring()函数
QuestionE
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); //TODO while(cin.hasNextLine()){ String str = cin.nextLine(); System.out.println(str.toUpperCase()); } cin.close(); } } |
使用string字符串的toUppercase大写转为小写函数即可
QuestionF
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 |
import java.util.*; public class Main { public static void main(String[] args) { Scanner cn = new Scanner(System.in); String strA = cn.nextLine(); String strB = cn.nextLine(); System.out.println(getDistance(strA,strB)); cn.close(); } public static int getDistance(String strA, String strB){ if (strA.equals(strB)) {//若两个字符串相同,返回0 return 0; } int lengthA=strA.length(); int lengthB=strB.length(); int length=Math.max(lengthA,lengthB);//找到两个字符串长度最大值定义,二维数组的长度 int array[][]=new int[length+1][length+1];//申请一个二维数组,存储转移矩阵 for(int i=0;i<=length;i++){//边界条件初始化 array[i][0]=i; } for(int j=0;j<=length;j++){ array[0][j]=j; } for(int i=1;i<=lengthA;i++){//状态转移方程 for(int j=1;j<=lengthB;j++){ array[i][j]=min(array[i-1][j]+1, array[i][j-1]+1, array[i-1][j-1]+(strA.charAt(i-1)==strB.charAt(j-1)?0:1)); } } return array[lengthA][lengthB]; } //取三个数中的最小值 public static int min(int a,int b, int c){ return Math.min(Math.min(a,b),c); } } |
使用动态规划。
QuestionG
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 |
import java.util.*; public class Main { static int getmax(String str){ int sum=65; for(int i=0;i<str.length();i++){ if(str.charAt(i)>sum) sum=str.charAt(i); } return sum; } public static void main(String[] args) { Scanner cin = new Scanner(System.in); //TODO while(cin.hasNextLine()){ String str = cin.nextLine(); int k =getmax(str); for(int i=0;i<str.length();i++){ if(str.charAt(i) == k) System.out.print(str.charAt(i)+"(max)"); else System.out.print(str.charAt(i)); } System.out.println(); } cin.close(); } } |
找到最大字符,这里字符的比较使用的asii码,A表示65最小,小写a97最小,找到后,将字符串进行遍历输出,遇到最大字符时,再其后输出max即可
QuestionH
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); //TODO int array[] = new int[27]; while(cin.hasNextLine()){ String str = cin.nextLine(); boolean key=false; for(int i=0;i<str.length();i++){ if(str.charAt(i)=='a'||str.charAt(i)=='A') array[0]++; if(str.charAt(i)=='b'||str.charAt(i)=='B') array[1]++; if(str.charAt(i)=='c'||str.charAt(i)=='C') array[2]++; if(str.charAt(i)=='d'||str.charAt(i)=='D') array[3]++; if(str.charAt(i)=='e'||str.charAt(i)=='E') array[4]++; if(str.charAt(i)=='f'||str.charAt(i)=='F') array[5]++; if(str.charAt(i)=='g'||str.charAt(i)=='G') array[6]++; if(str.charAt(i)=='h'||str.charAt(i)=='H') array[7]++; if(str.charAt(i)=='i'||str.charAt(i)=='I') array[8]++; if(str.charAt(i)=='j'||str.charAt(i)=='J') array[9]++; if(str.charAt(i)=='k'||str.charAt(i)=='K') array[10]++; if(str.charAt(i)=='l'||str.charAt(i)=='L') array[11]++; if(str.charAt(i)=='m'||str.charAt(i)=='M') array[12]++; if(str.charAt(i)=='n'||str.charAt(i)=='N') array[13]++; if(str.charAt(i)=='o'||str.charAt(i)=='O') array[14]++; if(str.charAt(i)=='p'||str.charAt(i)=='P') array[15]++; if(str.charAt(i)=='q'||str.charAt(i)=='Q') array[16]++; if(str.charAt(i)=='r'||str.charAt(i)=='R') array[17]++; if(str.charAt(i)=='s'||str.charAt(i)=='S') array[18]++; if(str.charAt(i)=='t'||str.charAt(i)=='T') array[19]++; if(str.charAt(i)=='u'||str.charAt(i)=='U') array[20]++; if(str.charAt(i)=='v'||str.charAt(i)=='V') array[21]++; if(str.charAt(i)=='w'||str.charAt(i)=='W') array[22]++; if(str.charAt(i)=='x'||str.charAt(i)=='X') array[23]++; if(str.charAt(i)=='y'||str.charAt(i)=='Y') array[24]++; if(str.charAt(i)=='z'||str.charAt(i)=='Z') array[25]++; if(str.charAt(i)=='#') key=true; } if(key) break;; } System.out.println("a "+array[0]); System.out.println("b "+array[1]); System.out.println("c "+array[2]); System.out.println("d "+array[3]); System.out.println("e "+array[4]); System.out.println("f "+array[5]); System.out.println("g "+array[6]); System.out.println("h "+array[7]); System.out.println("i "+array[8]); System.out.println("j "+array[9]); System.out.println("k "+array[10]); System.out.println("l "+array[11]); System.out.println("m "+array[12]); System.out.println("n "+array[13]); System.out.println("o "+array[14]); System.out.println("p "+array[15]); System.out.println("q "+array[16]); System.out.println("r "+array[17]); System.out.println("s "+array[18]); System.out.println("t "+array[19]); System.out.println("u "+array[20]); System.out.println("v "+array[21]); System.out.println("w "+array[22]); System.out.println("x "+array[23]); System.out.println("y "+array[24]); System.out.println("z "+array[25]); cin.close(); } } |
使用数组array存储字母的个数,读取多条数据使用while(cin.hasNextLine())即可。
QuestionI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); //TODO int m=cin.nextInt(); for(int i=0;i<m;i++){ String str = cin.next(); // StringBuilder strb = new StringBuilder(str); for(int j=0;j<str.length();j++){ if(str.charAt(j)>='a'&&str.charAt(j)<='z'){ System.out.print((char)(str.charAt(j)-32)); } if(str.charAt(j)>='A'&&str.charAt(j)<='Z'){ System.out.print((char)(str.charAt(j)+32)); } } System.out.println(); } cin.close(); } } |
简单,没啥好说的
QuestionJ
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); //TODO String key[] = new String[16]; key[0]="0000"; key[1]="0001"; key[2]="0010"; key[3]="0011"; key[4]="0100"; key[5]="0101"; key[6]="0110"; key[7]="0111"; key[8]="1000"; key[9]="1001"; key[10]="1010"; key[11]="1011"; key[12]="1100"; key[13]="1101"; key[14]="1110"; key[15]="1111"; while(cin.hasNextLine()){ String str =cin.nextLine(); for(int i=0;i<str.length();i++){ if(str.charAt(i)=='0') System.out.print(key[0]); if(str.charAt(i)=='1') System.out.print(key[1]); if(str.charAt(i)=='2') System.out.print(key[2]); if(str.charAt(i)=='3') System.out.print(key[3]); if(str.charAt(i)=='4') System.out.print(key[4]); if(str.charAt(i)=='5') System.out.print(key[5]); if(str.charAt(i)=='6') System.out.print(key[6]); if(str.charAt(i)=='7') System.out.print(key[7]); if(str.charAt(i)=='8') System.out.print(key[8]); if(str.charAt(i)=='9') System.out.print(key[9]); if(str.charAt(i)=='A') System.out.print(key[10]); if(str.charAt(i)=='B') System.out.print(key[11]); if(str.charAt(i)=='C') System.out.print(key[12]); if(str.charAt(i)=='D') System.out.print(key[13]); if(str.charAt(i)=='E') System.out.print(key[14]); if(str.charAt(i)=='F') System.out.print(key[15]); } System.out.println(); } cin.close(); } } |
我这里使用的一个笨方法,0-f十六进制,对应的二进制使用数组存储着,对于样例输入,直接输出对应的输出。
LatsQuestion
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 |
import java.util.Scanner; import java.util.regex.Pattern; //* // 没考虑ip为空的情况,写了很久*/ public class last { public static boolean isInteger(String str) { if(str == null) return false; Pattern pattern = Pattern.compile("^(([0-9]|([1-9]\\d)|(1\\d\\d)|(2([0-4]\\d|5[0-5]))))$"); return pattern.matcher(str).matches(); } public static void main(String[] args) { Scanner cin = new Scanner(System.in); //TODO while(cin.hasNextLine()){ String str = cin.nextLine(); if(str.equals("End of file")) break; String s[] =str.split("\\."); boolean k = true; if(s.length<4) k=false; for(int i=0;i<s.length;i++){ if(!isInteger(s[i])) k=false; } if(k) System.out.println("Y"); else System.out.println("N"); } cin.close(); } } |
将一个ip,通过.使用split函数进行分隔,对分隔后的字符串数组中每一个字符串进行判读,看其是否符号即可。这里注意一点,就是分割后的字符串数组是否长度等于4,如果不等于4,那就根本不是一个ip地址