#A + B 问题
##A + B 问题 I
创建class, 创建main函数, Scanner用法
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 sc = new Scanner(System.in); while (sc.hasNextInt()){ int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(a + b); } sc.close(); } }
|
##A + B 问题 II
for循环, while循环, ++ 和 –;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import java.util.*;
class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int n = sc.nextInt(); for (int i = 0; i < n ;i++){ int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(a + b); } } sc.close(); } }
|
##A + B 问题 III
if 语句,关系运算,break退出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import java.util.*; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int a = sc.nextInt(); int b = sc.nextInt(); if (a == 0 && b == 0){ break; } else { System.out.println(a + b); } } sc.close(); } }
|
##A + B 问题 IV
累加运算,算术运算, 赋值运算,三元运算
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
| import java.util.*;
public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); if (n == 0){ break; } int sum = 0; while (n-- > 0){ sum += sc.nextInt(); } System.out.println(sum); } sc.close(); } }
|
##A + B 问题 VIII
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.util.*;
public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); sc.nextLine(); while (n-- > 0){ int m = sc.nextInt(); int sum = 0; while (m-- > 0){ sum += sc.nextInt(); } System.out.println(sum); if (n != 0) System.out.println(); } } } }
|
#倒序输出数组与隔位输出
##fix length 数组
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
| import java.util.*;
public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); int[] record = new int[n]; for (int i = 0; i < n; i++){ record[i] = sc.nextInt(); } for(int i = n - 1; i >= 0; i--){ System.out.print(record[i] + " "); } System.out.println(); for (int i = 0; i < n; i += 2){ System.out.print(record[i] + " "); } } sc.close(); } }
|
##ArrayList 实现
ArrayList 方法
添加:.add()
查找:.get(index)
长度:.size()
删除:.remove(index)
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
| import java.util.*;
public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); List<Integer> record = new ArrayList<>(); for (int i = 0; i < n; i++){ record.add(sc.nextInt()); } for(int i = record.size() - 1; i >= 0; i--){ System.out.print(record.get(i) + " "); } System.out.println(); for (int i = 0; i < record.size(); i += 2){ System.out.print(record.get(i) + " "); } } sc.close(); } }
|
摆平积木
算average的方法
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 sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); if (n == 0){ break; } List<Integer> record = new ArrayList<>(); int sum = 0; int m = n; while (m-- > 0){ int temp = sc.nextInt(); record.add(temp); sum += temp; } int recordAvg = sum / n; int result = 0; for (int i = 0; i < n; i++){ if (record.get(i) > recordAvg){ result += record.get(i) - recordAvg; } } System.out.println(result); System.out.println(); } } }
|
奇怪的信
找到int中每个数字的做法
n % 10 找到数字
n /= 10 除去数字
n % 2 判断奇偶数
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 sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); int sum = 0; while (n > 0){ int temp = n % 10; if (temp % 2 == 0){ sum += temp; } n/=10; } System.out.println(sum); System.out.println(); } } }
|
打印正方形
简单的模拟题
设置result[][] 数组,检查填充,再打印
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
| import java.util.*;
public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); char[][] result = new char[n][n]; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ if (i == 0 || j == 0 || i == n- 1 || j == n -1 ){ result[i][j] = '*'; } else { result[i][j] = ' '; } } } for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ System.out.print(result[i][j]); } System.out.println(); } } }
|
平均绩点
String的用法
长度:.length();
比较:.equals();
切割拆分:.split();
索引:.charAt(index);
替换:.replace()
查找:.indexOf(子串);
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
| import java.util.*;
public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNextLine()){ String[] n = sc.nextLine().split(" "); int sum = 0, count = 0; int flag = 0; for (int i = 0; i < n.length; i++){ if (n[i].equals("A")){ sum += 4; count++; } else if (n[i].equals("B")){ sum += 3; count++; } else if (n[i].equals("C")){ sum += 2; count++; } else if (n[i].equals("D")){ sum += 1; count++; } else if (n[i].equals("F")){ sum += 0; count++; } else { flag = 1; } } if (flag == 1){ System.out.println("Unknown"); } else { System.out.printf("%.2f", (double) sum / count); System.out.println(); } } } }
|
句子缩写
Character.toUpperCase()
Character.toLowerCase();
String.trim() delete extra space
延伸删除额外的space的写法
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
| import java.util.*;
public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); sc.nextLine(); while (n-- > 0){ String result = removeExtraSpaces(sc.nextLine()); StringBuilder sb = new StringBuilder(); int i = 0; sb.append(Character.toUpperCase(result.charAt(0))); while (i < result.length()){ if (result.charAt(i) == ' '){ sb.append(Character.toUpperCase(result.charAt(i + 1))); } i++; } System.out.println(sb); } } } private static String removeExtraSpaces(String s){ int left = 0; int right = s.length() - 1; while (left < s.length() && s.charAt(left) == ' ') left++; while (right >= 0 && s.charAt(right) == ' ') right--; StringBuilder sb = new StringBuilder(); while (left <= right){ if(s.charAt(left) != ' ' || sb.charAt(sb.length()-1) != ' '){ sb.append(s.charAt(left)); } left++; } return sb.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
| import java.util.*;
public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); sc.nextLine(); while (n-- > 0){ String s = sc.nextLine(); char[] str = s.toCharArray(); int i = 0; while (i < s.length()){ swapInArray(str, i, i+1); i += 2; } System.out.println(new String(str)); } } } private static void swapInArray(char[] str, int m, int n){ str[m] ^= str[n]; str[n] ^= str[m]; str[m] ^= str[n]; } }
|