一:基础练习:
(本文只附代码,解析后续修改后添上)
1.a+b问题:
代码如下:
1 import java.util.*; 2 public class Main { 3 public static void main(String args[]) { 4 int a,b; 5 6 Scanner input = new Scanner(System.in); 7 a = input.nextInt(); 8 b = input.nextInt(); 9 10 System.out.println(a+b); 11 } 12 }
2.序列排序问题:
代码如下:
1 import java.util.Arrays; 2 import java.util.Scanner; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner scanner = new Scanner(System.in); 7 int n = scanner.nextInt(); 8 int[] arr = new int[n]; 9 //往数组添加元素,接收数据 10 for (int i = 0; i < n; i++) { 11 arr[i] = scanner.nextInt(); 12 } 13 14 //对数组排序 15 Arrays.sort(arr); 16 17 //打印数组 18 for (int i = 0; i < n; i++) { 19 System.out.print(arr[i] + " "); 20 } 21 } 22 }
3.十六进制转八进制:
代码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Question1(); } public static void Question1() { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); int i, j; String[] sz = new String[n]; //读入十六进制数 for (i = 0; i < n; i++) { sz[i] = in.next(); } for(i = 0; i < sz.length; i++) { String s = sz[i]; StringBuilder sb = new StringBuilder(); //将一位十六进制数转换为四位二进制数 for(j = 0; j < s.length(); j++) { switch(s.charAt(j)) { case '0': sb.append("0000"); break; case '1': sb.append("0001"); break; case '2': sb.append("0010"); break; case '3': sb.append("0011"); break; case '4': sb.append("0100"); break; case '5': sb.append("0101"); break; case '6': sb.append("0110"); break; case '7': sb.append("0111"); break; case '8': sb.append("1000"); break; case '9': sb.append("1001"); break; case 'A': sb.append("1010"); break; case 'B': sb.append("1011"); break; case 'C': sb.append("1100"); break; case 'D': sb.append("1101"); break; case 'E': sb.append("1110"); break; case 'F': sb.append("1111"); break; } } //再将二进制数转换成八进制数 transform(sb); } } public static void transform(StringBuilder sb) { int num = sb.length() % 3; //判断长度是否为3的倍数 switch(num) { case 0: //若转换的八进制数中第一位为0则删去 if(sb.substring(0, 3).equals("000")) sb.delete(0, 3); break; case 1: if(sb.substring(0, 1).equals("0")) sb.delete(0, 1); else sb = sb.insert(0, "00"); break; case 2: if(sb.substring(0, 2).equals("00")) sb.delete(0, 2); else sb = sb.insert(0, "0"); break; } StringBuilder res = new StringBuilder(); int len = sb.length(); String[] new_s1 = new String[len/3]; //取三位转换成八进制数 for(int i = 0; i < len/3; i++) { int tmp = Integer.parseInt(sb.substring(i * 3, i * 3 + 3), 2); res.append(tmp); } System.out.println(res); } }
4.十六进制转十进制:
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
System.out.print(Long.parseLong(string, 16));
}
}
5.十进制转十六进制:
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
one(input.nextInt());
}
public static void one(int a){
int b;
if(a<=15) {
tohax(a);
// System.out.print(a);
}
else{
b=a%16;
one(a>>4);
tohax(b);
// System.out.print(b);
}
}
public static void tohax(int c){
switch (c){
default:
System.out.print(c);
break;
case 10:
System.out.print("A");
break;
case 11:
System.out.print("B");
break;
case 12:
System.out.print("C");
break;
case 13:
System.out.print("D");
break;
case 14:
System.out.print("E");
break;
case 15:
System.out.print("F");
break;
}
}
}
6.特殊的回文数:
代码如下:
import java.util.Scanner;
public class Main {
final static byte one = 1;//常量1.
final static byte two = 2;//常量2.
final static byte ten = 10;//常量10;
//输入:正整数n。
//输出:y行,每行为一个回文数。
/*
* 格式:回文数每个数的和都为n
* 从左读和从右读是一样的,所以是关于y轴对称。
* 数据大小:10001-999999。n的大小为2-54.
* 5位的是2-45,6位的是2-54.
* 要求:10进制,五位到六位的所有正整数。
* 从小到大输出结果。
* */
//程序如下:
//1.输入正整数n。
//2.判断是否是偶数,不是,则说明一定是5位。
//3.判断是否小于45,不是,则说明一定是6位。
//4.求5位。输出结果
//5. 求6位。输出结果
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
byte in = input.nextByte(); //获取到输入到2-54的一个正整数。
if (in % two != 0) {
askfive(in);//不是偶数,就只求5位
} else if (in > 45) {
asksix(in);//大于45,就只求6位。
} else {//都不是,就要求5位和6位。
askfive(in);
asksix(in);
}
}
public static void askfive(byte in) {
//求5位数。
//先确定第一位最小值存不存在。
for (int i = one; i <ten ; i++) {//第一位
for (int j = 0; j <ten ; j++) {//第二位
final int i0 = in - two * i - two * j;
if (i0 < ten&&i0>=0) {//求第一位等于第五位,第二位等于第四位,第三位是否满足小于10的要求的数存在。
System.out.println(i+""+j+""+i0+""+j+""+i);
}
}
}
}
public static void asksix(byte in) {
//求6位数。
//先确定第一位最小值存不存在。
for (int i = one; i <ten ; i++) {//第一位数
for (int j = 0; j <ten ; j++) {//第二位数
final int i1 = (in - two * i - two * j) / two;
if (i1 < ten&&i1>=0) {//第三位数成立
System.out.println(i+""+j+""+ i1 +""+ i1 +""+j+""+i);
}
}
}
}
}
7.回文数:
代码如下:
public class Main {
/*
* 输入:无
* 输出:回文数
* 格式要求:长度是四位的十进制数。关于y轴对此,第一位等于第四位,第二位等于第三位。
* 范围是1001-9999.
* 要求:从小到大。
* */
//编程思路:
//1.从第一位为1,第二位为0,开始循环。
//2、输出所有这样的数。
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);
}
}
}
}
8.特殊的数字:
代码如下:
public class Main {
final static byte ten = 10;//常量10;
final static byte three = 3;//常量3;
/*
* 输入:无
* */
/*
* 输出:n行,三位的十进制
* 格式要求:从小到大。
* */
//程序思路:
//1、范围是从100-999.而根据立方根来推断,如果从立方根下手,最小要从105(1+125>100)开始循环,最大到962(8+216+729<999)
// 2.对三个数循环,求和,判断是否等于自己的10进制。
// 3.输出所有相等的数。
public static void main(String[] args) {
for (int i = 105; i <963 ; i++) {
int a = i % ten;//个位。
int b = (i / ten)%ten;//十位位。
int c = i/100 ;//百位。
if (i == (Math.pow(a, three) + Math.pow(b, three) + Math.pow(c, three))) {
System.out.println(i);
}
}
}
}
9.杨辉三角:
代码如下:
import java.util.Scanner;
public class Main {
/*
* 输入:整数n
* 格式要求:n的范围是1-34.
* */
/*
* 输出:n行,每一行是杨辉三角的数。
* 格式要求,每一行的每个数要按顺序输出,并且要使用一个空格分隔。
* */
//程序思路:
//1、输入一个byte大小的数,n
//2、根据Cn,循环和递归的方式来求每一行每一个数
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
byte n = input.nextByte();//获取整形n。
askYh(n);
}
public static void askYh(byte n) {//求杨辉的某行。
int b = n-1;//C的下方。
if (b == 0) {//说明是第一行
System.out.println("1");
}
for (int i = 0; i < n; i++) {//n行。
for (int j = 0; j <=i; j++) {//对C的上方从0到i循环,不能大于i。
int out = askJc(i, j) / askJc02(i, j);
System.out.print(out + " ");
}
System.out.println();//换行。
}
}
public static int askJc(int i,int j) {//求b!/i!
//递归调用
if (i == j + 1) {
return i;
}
if (i == j) {//当c的上方和下方相同的时候,返回1.
return 1;
}
return i*askJc(i-1,j);
}
public static int askJc02(int i,int j) {//求(b-i)!
if ((i == j + 1)||(i == j)) //当i-j = 1 的和i=j的时候
return 1;
return (i - j) * askJc02(i - 1, j);
}
}
1 import java.util.Scanner; 2 3 public class test04 { 4 5 6 /* 7 * 输入:整数n 8 * 格式要求:n的范围是1-34. 9 * */ 10 11 /* 12 * 输出:n行,每一行是杨辉三角的数。 13 * 格式要求,每一行的每个数要按顺序输出,并且要使用一个空格分隔。 14 * */ 15 16 17 //程序思路: 18 //1、输入一个byte大小的数,n 19 //2、根据Cn,循环和递归的方式来求每一行每一个数 20 21 22 public static void main(String[] args) { 23 24 Scanner input = new Scanner(System.in); 25 byte n = input.nextByte();//获取整形n。 26 27 askYh(n); 28 29 } 30 public static void askYh(byte n) {//求杨辉的某行。 31 int b = n-1;//C的下方。 32 if (b == 0) {//说明是第一行 33 System.out.println("1"); 34 } 35 for (int i = 0; i < n; i++) {//n行。 36 for (int j = 0; j <=i; j++) {//对C的上方从0到n-1循环 37 int out = askJc(i, j) / askJc02(i, j); 38 System.out.print(out + " "); 39 } 40 System.out.println();//换行。 41 } 42 } 43 44 public static int askJc(int i,int j) {//求b!/i! 45 //递归调用 46 if (i == j + 1) { 47 return i; 48 } 49 if (i == j) {//当c的上方和下方相同的时候,返回1. 50 return 1; 51 } 52 return i*askJc(i-1,j); 53 } 54 55 public static int askJc02(int i,int j) {//求(b-i)! 56 if ((i == j + 1)||(i == j)) //当b-i = 1 或者i=j的时候。 57 return 1; 58 59 return (i - j) * askJc02(i - 1, j); 60 } 61 62 }
//未完,待更。。。