大部分程序来自查阅网络资源总结。
package Chapter01;
import java.util.Scanner;
//A+B的问题
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a+b);
}
}
package Chapter01;
import java.util.Scanner;
//Fibonacci数列
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.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]);
}
}
}
package Chapter01;
import java.util.Scanner;
//序列求和
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long n = scanner.nextLong();
long sum = 0;
for (int i = 1; i <=n ; i++) {
sum = sum+i;
}
System.out.println(sum);
}
}
package Chapter01;
import java.util.Scanner;
//圆的面积
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final double PI = 3.14159265358979323;
double r = scanner.nextInt();
double s = r*r*PI;
System.out.println(String.format("%.7f",s));//保留七位小数。
}
}
package Chapter01;
import java.util.Scanner;
//十六进制转八进制
public class Demo05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.next();
}
scanner.close();
for (int j = 0; j < n; j++) {
String str2 = f(a[j]);
int len2 = str2.length();
if (len2%3==1){str2="00"+str2;}
if (len2%3==2){str2="0"+str2;}
g(str2);
}
}
public static String f(String s){
int len = s.length();
StringBuilder str = new StringBuilder();
for (int i = 0; i < len; i++) {
switch (s.charAt(i)){
case '0':str.append("0000");break;
case '1':str.append("0001");break;
case '2':str.append("0010");break;
case '3':str.append("0011");break;
case '4':str.append("0100");break;
case '5':str.append("0101");break;
case '6':str.append("0110");break;
case '7':str.append("0111");break;
case '8':str.append("1000");break;
case '9':str.append("1001");break;
case 'A':str.append("1010");break;
case 'B':str.append("1011");break;
case 'C':str.append("1100");break;
case 'D':str.append("1101");break;
case 'E':str.append("1110");break;
case 'F':str.append("1111");break;
default:break;
}
}
return str.toString();
}
public static void g(String str) {
int len = str.length();
StringBuilder s = new StringBuilder();
for (int i = 0; i < len; i=i+3) {
int temp = ((str.charAt(i)-'0')*4+(str.charAt(i+1)-'0')*2+(str.charAt(i+2)-'0'));//二进制变八进制
s.append(temp);
}
System.out.println(s.toString().replaceFirst("^0*",""));
//正则表达式:^:匹配输入字符串的开始位置; *:匹配前面的子表达式零次或多次。
}
}
package Chapter01;
import java.util.Scanner;
//数列排序
public class Demo06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int temp;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < n-j-1; i++) {
if (arr[i]>arr[i+1]){
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+" ");
}
sc.close();
}
}
package Chapter01;
import java.util.Scanner;
//十六进制转十进制
public class Demo07 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.next();
//装箱,可转换为十进制long型整数。
System.out.println(Long.valueOf(string,16));
//拆箱。
System.out.println(Long.parseLong(string,16));
}
}
package Chapter01;
import java.util.Scanner;
//十进制转十六进制
public class Demo08 {
//方法一
/*public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.println(String.format("%X",n));//转换为大写的十六进制整数类型。
}*/
//方法二
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
//Integer.toHexString(n)表示把十进制表示为十六进制。
//toUpperCase())表示把十六进制中的小写字母转换为大写字母。
System.out.println(Integer.toHexString(n).toUpperCase());
}
}
package Chapter01;
import java.util.Scanner;
//杨辉三角型
//number = number*(i-j)/(j+1);
//题目要求 1 <= n <= 34.要用long类型。
public class Demo09 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
long number = 1;
for (int j = 0; j <= i; j++) {
System.out.print(number+" ");
number = number*(i-j)/(j+1);
}
System.out.println();
}
}
}
package Chapter01;
//回文数
public class Demo10 {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.println(i+""+j+""+j+""+i);
}
}
}
}
package Chapter01;
import java.util.Scanner;
//特殊回文数
public class Demo11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 1; i <10 ; i++) {
for (int j = 0; j <10 ; j++) {
for (int k = 0; k < 10; k++) {
if (i+i+j+j+k==n){
System.out.println(""+i+j+k+j+i);
}
}
}
}
for (int i = 1; i <10 ; i++) {
for (int j = 0; j <10 ; j++) {
for (int k = 0; k < 10; k++) {
if (i+i+j+j+k+k==n){
System.out.println(""+i+j+k+k+j+i);
}
}
}
}
}
}
package Chapter01;
//特殊的数字
public class Demo12 {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
if ((i*i*i+j*j*j+k*k*k)==(i*100+j*10+k)){
System.out.println(""+i+j+k);
}
}
}
}
}
}
package Chapter01;
import java.util.Scanner;
//查找整数
public class Demo13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
int a = scanner.nextInt();
scanner.close();
for (int j = 0; j < n; j++) {
if (arr[j]==a){
System.out.println(j+1);
break;//也可以设置一个标志位。
}else if (j==n-1){//假如到最后以为也没找到整数。
System.out.println(-1);
}
}
}
}
package Chapter01;
import java.util.Scanner;
//数列特征
public class Demo14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i]=scanner.nextInt();
}
scanner.close();
int sum=0,max=arr[0],min=arr[0];
for (int i = 0; i < n; i++) {
if (min>arr[i]){
min=arr[i];
}
if (max<arr[i]){
max=arr[i];
}
sum = sum+arr[i];
}
System.out.println(max);
System.out.println(min);
System.out.println(sum);
}
}
package Chapter01;
import java.util.Scanner;
//字母图形
public class Demo15 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
scanner.close();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char x = (char)('A'+Math.abs(i-j));//Math.abs(i-j) 技巧
System.out.print(x);
}
System.out.println();
}
}
}
package Chapter01;
//01字串
public class Demo16 {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
for (int m = 0; m < 2; m++) {
System.out.println(""+i+j+k+l+m);
}
}
}
}
}
}
}
package Chapter01;
import java.util.Scanner;
//闰年判断
public class Demo17 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int y = scanner.nextInt();
if ((y%4==0&&y%100!=0) || y%400==0){
System.out.println("yes");
}else {
System.out.println("no");
}
}
}