1-1 BEGIN-1 A+B问题
import java.util.*;public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); System.out.println(a+b); }}
1-2 BEGIN-2 序列求和
import java.util.*;public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); double n = in.nextInt(); double s = (1 + n) / 2; double t = s * n; System.out.println((long)t); }}
1-3 BEGIN-3 圆的面积
import java.util.*;public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); final double PI = 3.14159265358979323; double a = in.nextInt(); double c = a*a*PI; System.out.printf("%.7f", c); }}
1-4 BEGIN-4 Fibonacci数列
public class Main { public static void main(String[] ars){ Scanner in = new Scanner(System.in); int n = in.nextInt(); if(n < 3){ System.out.println(1); }else { int[] a = new int[n + 1]; a[1] = 1 % 10007; a[2] = 1 % 10007; for (int i = 3; i <= n; i++) { a[i] = (a[i - 1] + a[i - 2]) % 10007; } System.out.println(a[n]); } }}
基础练习:
2-1 BASIC-1 闰年判断
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int y = in.nextInt(); if((y%4 == 0 && y%100 != 0) || (y%400 == 0)){ System.out.println("yes"); }else { System.out.println("no"); } }}
2-2 BASIC-2 01字串
public class Main { public static void main(String[] args){ for(int i=0; i<32; i++) { String str = Integer.toBinaryString(i); System.out.printf("%05d", Integer.valueOf(str)); System.out.println(); } }}
2-3 BASIC-3 字母图形
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); String s = "BCDEFGHIJKLMNOPQRSTUVWXYZ"; StringBuilder str = new StringBuilder(s); str = str.reverse(); str.append("A"); str.append(s); int a = str.indexOf("A"); int b = a+m; for(int i=0; i<n; p="" }}
2-4 BASIC-4 数列特征
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] s = new int[n]; for(int i=0; i<n; }="" i++){="" if(s[j]="" j++){="" j max){ max = s[j]; } if(s[j] < min){ min = s[j]; } sum = sum + s[j]; } System.out.println(max); System.out.println(min); System.out.println(sum); }}
2-5 BASIC-5 查找整数
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] s = new int[n]; for(int i=0; i<n; p="" }}<="" }="" i++){="" if(s[j]="=" j++){="" j
2-6 BASIC-6 杨辉三角形
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] a = new int[n][n]; if(n == 1){ System.out.println(1); }else{ a[0][0] = 1; for(int i=1; i<n; p="" }}<="" }="" i++){="" j++){="" j="1;" for(int="" system.out.println();="" ?);="" system.out.print(?="" if(i!="j){" system.out.print(a[i][j]);="" j<="i-1;" i
2-7 BASIC-7 特殊的数字
public class Main { public static void main(String[] args){ for(int dd=100; dd<=999; dd++){ String data = dd + ""; char d0 = data.charAt(0); char d1 = data.charAt(1); char d2 = data.charAt(2); double sum1 = Math.pow((d0-'0'), 3) + Math.pow((d1-'0'), 3) + Math.pow((d2-'0'), 3); if((int)sum1 == dd){ System.out.println(dd); } } }}
2-8 BASIC-8 回文数
public class Main { public static void main(String[] args){ StringBuilder a = new StringBuilder(); for(int i=1; i<=9; i++){ for(int j=0; j<=9; j++){ a.append(i); a.append(j); a.append(j); a.append(i); System.out.println(a.toString()); a.delete(0, a.length()); } } }}
2-9 BASIC-9 特殊回文数
2-9-1 解法一
import java.util.Scanner;public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=10000; i<=999999; i++){ String data = "" + i; int sum = 0; for(int k=0; k<data.length(); p="" }}<="" }="" j++){="" j="0;" for(int="" sum="sum" int="" count="len/2;" +="" continue;="" else{="" system.out.println(i);="" sum)){="" (n="=" &&="" flag.tostring().equals(?11?))="" ||="" if((flag.tostring().equals(?111?)="" flag.append(0);="" }else{="" flag.append(1);="" data.charat(len-j-1)){="" if(data.charat(j)="=" j
2-9-2 解法二
import java.util.Scanner; public class Main { public static void f5(int n){ /* 五位数处理代码 */ StringBuilder anser1 = new StringBuilder(); // 测试第1位的值 for(int i=1; i<=9; i++){ // 测试第2位的值 for(int j=0; j<=9; j++){ // 计算第3位的值 int third_number = n - 2*i - 2*j; if(third_number>=0 && third_number<=9){ anser1.append(i); anser1.append(j); anser1.append(third_number); anser1.append(j); anser1.append(i); System.out.println(anser1); anser1.delete(0, anser1.length()); }else{ continue; } } } } public static void f6(int n){ /* 六位数处理代码 */ StringBuilder anser2 = new StringBuilder(); // temp: 前三位相加的值 int temp = n/2; // 测试第1位的值 for(int i=1; i<=9; i++){ // 测试第2位的值 for(int j=0; j<=9; j++){ // 计算第3位的值 int third_number2 = temp-i-j; if(third_number2>=0 && third_number2<=9){ anser2.append(i); anser2.append(j); anser2.append(third_number2); anser2.append(third_number2); anser2.append(j); anser2.append(i); System.out.println(anser2); anser2.delete(0, anser2.length()); }else{ continue; } } } } public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); f5(n); // 偶数才可能是6位 if(n%2==0){ f6(n); } }}
2-10 BASIC-10 十进制转十六进制
import java.util.Scanner; public class Main { public static char f(long a){ char temp = ' '; if(a<=9){ temp = (char)(a+48); }else { switch ((int) a) { case 10: temp = 'A'; break; case 11: temp = 'B'; break; case 12: temp = 'C'; break; case 13: temp = 'D'; break; case 14: temp = 'E'; break; case 15: temp = 'F'; break; } }// System.out.println(temp); return temp; } public static void main(String[] args){ Scanner in = new Scanner(System.in); long a = in.nextLong(); StringBuilder ss = new StringBuilder(); if(a == 0){ ss.append(0); }else { while (a != 0) { long number = a % 16; char s = f(number); ss.append(s); a = a / 16; } } System.out.println(ss.reverse().toString()); }}
2-11 BASIC-11 十六进制转十进制
import java.util.*; public class Main { public static int f(char a){ int x=0; if(a>='0' && a<='9'){ x = a-'0'; }else{ switch (a){ case 'A': x = 10;break; case 'B': x = 11;break; case 'C': x = 12;break; case 'D': x = 13;break; case 'E': x = 14;break; case 'F': x = 15;break; default:break; } } return x; } public static void h(String str){ int len = str.length(); double x = 0; for(int i=0; i<len; p="" }}
2-12 BASIC-12 十六进制转八进制
import java.util.Scanner; public class Main { public static String f(String s){ int len = s.length(); StringBuilder str = new StringBuilder(); for(int i=0; i<len; p="" }}<="" }="" i++){="" j++){="" j="0;" for(int="" int="" break;="" in.close();="" a="new" i<n;="" i="0;" +="" stringbuilder();="" stringbuilder="" len="str.length();" string="" scanner(system.in);="" in="new" scanner="" args){="" main(string[]="" void="" static="" public="" g(str2);="" str2;}="" 2){str2="0" 3="=" %="" if(len2="" else="" 1){str2="00" len2="str2.length();" str2="f(a[j]);" j<n;="" a[i]="in.next();" string[n];="" string[]="" n="in.nextInt();" ??));="" system.out.println(a.tostring().replacefirst(?^0*?,="" a.append(temp);="" temp="(str.charAt(i)-'0')*4+(str.charAt(i+1)-'0')*2+(str.charAt(i+2)-'0');" i+="3){" i
2-13 BASIC-13 数列排序
import java.util.*;public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for(int i=0; i<n; }="" i++){="" j++){="" j="0;" for(int="" k++){="" a[i]="in.nextInt();" if(a[j]="" k<n;="" k="j+1;" ja[k]){ int tt = a[k]; a[k] = a[j]; a[j] = tt; } } } for(int j=0; j<n; p="" }}
|
|