Java基础编程50题(下)
2016-12-19 00:39
489 查看
【程序26】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
结果吻合。
【程序27】
题目:求100之内的素数
结果吻合。
【程序28】
题目:对10个数进行排序
1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换, 下次类推,即用第
二个元素与后8个进行比较,并进行交换。
注:此处替换为使用快速排序算法实现。
结果吻合。
【程序29】
题目:求一个3*3矩阵对角线元素之和
1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
结果吻合。
【程序30】
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
1. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,
依次后移一个位置。
结果吻合。
【程序31】
题目:将一个数组逆序输出。
1.程序分析:用第一个与最后一个交换。
结果吻合。
【程序32】
题目:取一个整数a从右端开始的4~7位。
程序分析:可以这样考虑:
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&运算。
结果吻合。
【程序33】
题目:打印出杨辉三角形(要求打印出10行如下图)
1.程序分析:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
结果吻合。
【程序34】
题目:输入3个数a,b,c,按大小顺序输出。
1.程序分析:利用指针方法。
【程序35】
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
【程序36】
题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
【程序37】
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下
的是原来第几号的那位。
【程序38】
题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
【程序39】
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数
1/1+1/3+...+1/n(利用指针函数)
【程序40】
题目:字符串排序。
【程序41】
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一
个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中
,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
【程序42】
题目:809*??=800*??+9*??+1
其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。
【程序43】
题目:求0—7所能组成的奇数个数。
【程序44】
题目:一个偶数总能表示为两个素数之和。
结果吻合。
【程序45】
题目:判断一个素数能被几个9整除
public class aufgabe45 {
private int range;
private int sum;
aufgabe45(int range)
{
this.range=range;
sum=0;
}
boolean Prime(int a)
{
for(int i=2;i<a;i++)
{
if(a%i==0)
return false;
}
return true;
}
boolean factor(int even)
{
while(even%9==0)
{
sum++;
}
if(this.sum!=0)
{
System.out.println(even+" kann be divided by 9 "+sum+" times.");
return true;
}
else
{
System.out.println(even+" kann't be divided by 9 .");
return false;
}
}
void Divide()
{
for(int i=2;i<=this.range;i++)
{
if(this.Prime(i))
this.factor(i);
}
}
public static void main(String[] args)
{
aufgabe45 test=new aufgabe45(100);
test.Divide();
}
}
【程序46】
题目:两个字符串连接程序
public class aufgabe46 {
public static void main(String[] args)
{
String s1="abv";
String s2="frg";
String s3=s1+s2;
System.out.println(s3);
}
}
【程序47】
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
public class aufgabe47 {
static void Draw(int n)
{
for(int i=0;i<n;i++)
{
System.out.print("*");
}
System.out.println();
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("please input 7 numbers between 1-50.");
int i=0;
while(sc.hasNext() && i<7)
{
aufgabe47.Draw(sc.nextInt());
i++;
}
System.out.println("input finished.");
sc.close();
}
}
【程序48】
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字
都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
public class aufgabe48 {
private int code;
private int[] digit;
aufgabe48(int code)
{
this.code=code;
digit=new int[4];
}
void Encode()
{
for(int i=1;i<5;i++)
{
digit[(i-1)]=this.Translate( (int)(this.code/Math.pow(10,i-1) ) %10);
}
DigitSwap(1,4);
DigitSwap(2,3);
}
void DigitSwap(int i,int j)
{
int temp=this.digit[i-1];
this.digit[i-1]=this.digit[j-1];
this.digit[j-1]=temp;
}
int Translate(int n)
{
System.out.println("the input digit is "+n+",the encoded digit is "+((n+5)%10));
return (n+5)%10;
}
void show()
{
System.out.println("the original number is "+this.code);
System.out.print("the encoded number is ");
for(int i=0;i<this.digit.length;i++)
{
System.out.print(this.digit[i]);
}
System.out.println();
}
public static void main(String[] args)
{
int code=7364;
aufgabe48 test=new aufgabe48(code);
test.Encode();
test.show();
}
}
结果吻合。
【程序49】
题目:计算字符串中子串出现的次数
【程序50】
题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算
出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
import java.util.Scanner; public class aufgabe26 { private String day; aufgabe26 (String day) { this.day=day.toUpperCase(); } void judgement() { switch(this.day.charAt(0)){ case 'M': System.out.println("today is Monday");break; case 'T': { if(this.day.charAt(1)=='U') System.out.println("today is Tuesday"); else if(this.day.charAt(1)=='H') System.out.println("today is Thursday"); else System.out.println("flase input"); }break; case 'W': System.out.println("today is Wednesday");break; case 'F': System.out.println("today is Friday");break; case 'S': { if(this.day.charAt(1)=='A') System.out.println("today is Saturday"); else if(this.day.charAt(1)=='U') System.out.println("today is Sunday"); else System.out.println("flase input"); }break; default: System.out.println("flase input"); } } public static void main(String[] args) { System.out.println("please input a weekday"); Scanner sc = new Scanner(System.in); String i=sc.nextLine(); aufgabe26 test=new aufgabe26(i); test.judgement(); sc.close(); } }
结果吻合。
【程序27】
题目:求100之内的素数
public class aufgabe27 { public static void main(String[] args) { int sum=0; for(int i=1;i<=100;i++) { int count=0; for(int j=1;j<=i;j++) if(i%j==0) count++; if(count==2) { sum++; System.out.println(i+" is prime."); } } System.out.println("there are "+sum+" prime numbers in [1,100]."); } }
结果吻合。
【程序28】
题目:对10个数进行排序
1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换, 下次类推,即用第
二个元素与后8个进行比较,并进行交换。
注:此处替换为使用快速排序算法实现。
public class aufgabe28 { private int[] com; aufgabe28(int[] com) { this.com=com; } void QuickSort(int start,int end) { if(start>=end) return ; int i=start; int j=end; int pivot=this.com[(start+end)/2]; System.out.println(" original index : "+i+" "+j+" "+pivot); while(true) { while(this.com[i]<pivot) { System.out.println("com["+i+"] ="+this.com[i]+" is smaller than pivot"); i++; if(i==j) break; } while(this.com[j]>pivot) { System.out.println("com["+j+"] ="+this.com[j]+" is bigger than pivot"); j--; if(i==j) break; } if(this.com[i]!=this.com[j]) swap(i,j); else i++; if(i>=j) break; System.out.println("swap "+this.com[i]+" "+this.com[j]); } System.out.println("the new partition is [ "+start+", "+(j-1)+" ] , ["+(j+1)+", "+end+" ]"); QuickSort(start,j-1); QuickSort(j+1,end); } void swap(int old,int neu) { int temp=this.com[old]; this.com[old]=this.com[neu]; this.com[neu]=temp; } void show() { for(int i=0;i<this.com.length;i++) { System.out.println(this.com[i]+" "); } } public static void main(String[] args) { int[] testarr={33,3,9,96,32}; aufgabe28 test=new aufgabe28(testarr); System.out.println("---------- original array -----------"); test.show(); test.QuickSort(0,testarr.length-1); System.out.println("---------- sorted array -----------"); test.show(); } }
结果吻合。
【程序29】
题目:求一个3*3矩阵对角线元素之和
1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
public class aufgabe29 { public static void main(String[] args) { int[] input=new int[9]; Scanner scr=new Scanner(System.in); System.out.println("please input 9 numbers:"); for(int i=0;i<input.length&& scr.hasNextInt();i++) { int a=scr.nextInt(); input[i]=a; } scr.close(); int sum=0; for(int i=0;(3*i+i)<input.length;i++) { sum+=input[3*i+i]; System.out.println("["+i+"]["+i+"]="+input[3*i+i]); } System.out.println("the sum is "+sum); } }
结果吻合。
【程序30】
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
1. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,
依次后移一个位置。
public class aufgabe30 { private int[] arr; private int neu; private int[] sarr; aufgabe30(int[] arr,int neu) { this.arr=arr; this.neu=neu; this.sarr=new int[arr.length+1]; } void insert() { int flag=1; for(int i=0,j=0;j<this.sarr.length;j++) { if(neu<this.arr[i] && flag==1) { sarr[j]=neu; flag=0; } else { sarr[j]=this.arr[i]; i++; if(i==this.arr.length && j!=this.sarr.length-1) { sarr[j+1]=neu; break; } } } } void show() { for(int i=0;i<this.sarr.length;i++) System.out.println(this.sarr[i]); } public static void main(String[] args) { int[] arr={1,4,12,25,35,54,67,89}; System.out.println("————————————末尾插入————————————"); aufgabe30 test1=new aufgabe30(arr,99); test1.insert(); test1.show(); System.out.println("————————————其他位置插入——————————"); aufgabe30 test2=new aufgabe30(arr,32); test2.insert(); test2.show(); } }
结果吻合。
【程序31】
题目:将一个数组逆序输出。
1.程序分析:用第一个与最后一个交换。
public class aufgabe31 { private int[] arr; aufgabe31(int[] arr) { this.arr=arr; } void swap(int i,int j) { int temp=this.arr[i]; this.arr[i]=this.arr[j]; this.arr[j]=temp; } void ArraySwap() { for(int i=0,j=(this.arr.length-1);i<=j;) { swap(i,j); i++; j--; } } void show() { for(int i=0;i<this.arr.length;i++) System.out.print(this.arr[i]+" "); System.out.println(); } public static void main(String[] args) { int[] swap={1,2,3,4,5,6,7,8,9,10}; aufgabe31 test=new aufgabe31(swap); System.out.println("---------original array---------"); test.show(); System.out.println("---------inverse array---------"); test.ArraySwap(); test.show(); } }
结果吻合。
【程序32】
题目:取一个整数a从右端开始的4~7位。
程序分析:可以这样考虑:
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)将上面二者进行&运算。
public class aufgabe32 { private int a; private int[] digit; private int start; private int end; aufgabe32(int a,int start,int end) { this.a=a; digit=new int[end-start+1]; this.start=start; this.end=end; } void DigitExtract() { for(int i=start;i<=end;i++) { digit[i-start]=(a>>i) & 0x1; } } void show() { for(int i=digit.length-1;i>=0;i--) { System.out.print(digit[i]+" "); } System.out.println(); } public static void main(String[] args) { int a=144; int start=4; int end=7; System.out.println("original digits :"); aufgabe32 test1=new aufgabe32(a,0,32); test1.DigitExtract(); test1.show(); System.out.println("digits von "+start+" to "+end+" is :"); aufgabe32 test=new aufgabe32(a,start,end); test.DigitExtract(); test.show(); } }
结果吻合。
【程序33】
题目:打印出杨辉三角形(要求打印出10行如下图)
1.程序分析:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
public class aufgabe33 { private int row; aufgabe33(int row) { this.row=row; } int factor(int n) { if(n==0) return 1; else return n*factor(n-1); } int Binomial(int a,int b) { return factor(a)/(factor(b)*factor(a-b)); } void show() { for(int i=0;i<=this.row;i++) { for(int j=0;j<=i;j++) { System.out.print(Binomial(i,j)+" "); } System.out.println(); } } public static void main(String[] args) { int row=5; aufgabe33 test=new aufgabe33(row); test.show(); } }
结果吻合。
【程序34】
题目:输入3个数a,b,c,按大小顺序输出。
public class aufgabe34 { private char[] com; private int num; private int pos; aufgabe34(int num) { this.num=num; com=new char[num+1]; this.pos=0; } void AddandSort(char ch) { for(int i=0;i<=this.pos;i++) { if(com[i]>ch) { for(int j=(this.pos);j>i;j--) swap(j,j-1); com[i]=ch; break; //仅在第一个大于ch的位置插入即可。 } else if(this.com[i]==0) this.com[i]=ch; } this.pos++; } void swap(int i,int j) { char temp=this.com[i]; this.com[i]=this.com[j]; this.com[j]=temp; } void show() { System.out.println("the sorted characters are :"); for(int i=0;i<=this.pos;i++) System.out.print(this.com[i]+" "); System.out.println(); } public static void main(String[] args) { System.out.println("please input the numbers of to be compared characters "); Scanner scc=new Scanner(System.in); int num=scc.nextInt(); Scanner sc=new Scanner(System.in); aufgabe34 test=new aufgabe34(num); System.out.println("please add the to be compared characters "); while(sc.hasNext()) { char ch=sc.nextLine().charAt(0); test.AddandSort(ch); test.show(); } } }
1.程序分析:利用指针方法。
【程序35】
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
public class aufgabe35 { private int[] arr; private int[][] mm; aufgabe35(int[] arr) { this.arr=arr; this.mm=new int[2][2]; } void swap(int i,int j) { int temp=this.arr[i]; this.arr[i]=this.arr[j]; this.arr[j]=temp; } void FindMM() { mm[0][0]=this.arr[0]; //比较之前要先赋一个需参与比较的值,以免初始化数值影响比较结果 mm[0][1]=0; mm[1][0]=this.arr[0]; mm[1][1]=0; for(int i=0;i<this.arr.length;i++) { if(this.arr[i]<mm[0][0]) { mm[0][0]=this.arr[i]; mm[0][1]=i; } if(this.arr[i]>mm[1][0]) { mm[1][0]=this.arr[i]; mm[1][1]=i; } } } void Exchange() { FindMM(); swap(0,mm[1][1]); swap((this.arr.length-1),mm[0][1]); } void show() { for(int i=0;i<this.arr.length;i++) { System.out.print(this.arr[i]+" "); } System.out.println(); } public static void main(String[] args) { int[] arr={2,43,5,0,1,32}; aufgabe35 test=new aufgabe35(arr); System.out.println("the original array is :"); test.show(); test.Exchange(); System.out.println("the sorted array is :"); test.show(); } }
【程序36】
题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
public class aufgabe36 { private int[] arr; private int offs; private int[] temp; private int flag; aufgabe36(int[] arr,int offs) { this.arr=arr; this.offs=offs; temp=new int[this.arr.length]; flag=0; } void Cycle() { for(int i=0;i<this.arr.length;i++) { this.temp[(i+offs)%this.arr.length]=this.arr[i]; } flag=1; } void show() { if(flag==1) { System.out.println("the sorted array is :"); for(int i=0;i<this.temp.length;i++) System.out.print(this.temp[i]+" "); System.out.println(); } else { System.out.println("the original array is :"); for(int i=0;i<this.arr.length;i++) System.out.print(this.arr[i]+" "); System.out.println(); } } public static void main(String[] args) { int[] arr={3,23,7,45,1,9}; Scanner sc=new Scanner(System.in); System.out.println("please inpput the moving diatance :"); int offs=sc.nextInt(); aufgabe36 test=new aufgabe36(arr,offs); test.show(); test.Cycle(); test.show(); sc.close(); } }
【程序37】
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下
的是原来第几号的那位。
public class aufgabe37 { private int sum; private int[] arr; aufgabe37(int sum) { this.sum=sum; arr=new int[sum]; } void game() { int length=sum; int count=0; for(int i=0;length>1;i++) { if(this.arr[i%this.arr.length]!=1) { count++; System.out.println("the "+(i%this.arr.length)+"th people count No."+count); } if(count==3) { this.arr[i%this.arr.length]=1; length--; count=0; System.out.println("the "+(i%this.arr.length)+"th people out , "+"left "+length+" people."); } } } void show() { for(int i=0;i<this.arr.length;i++) { if(this.arr[i]==0) System.out.println("the lefted people is No."+i); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("please input the number of players"); int sum=sc.nextInt(); aufgabe37 test=new aufgabe37(sum); test.game(); test.show(); sc.close(); } }
【程序38】
题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
public class aufgabe38 { private String str; aufgabe38(String str) { this.str=str; } int GetLength() { return str.length(); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("please input the String"); String str=sc.nextLine(); aufgabe38 test=new aufgabe38(str); System.out.println("the length is "+test.GetLength()); sc.close(); } }
【程序39】
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数
1/1+1/3+...+1/n(利用指针函数)
public class aufgabe39 { private int flag; aufgabe39(double n) { flag=(int)n%2; } double SumAll(double n) { if(n==2 && flag==0) { System.out.println("1/"+n+"="); return 1/2; } else if(n==1 && flag==1) { System.out.println("1/"+n+"="); return 1; } else { System.out.print("1/"+n+"+"); return 1/n+SumAll(n-2); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("please input the number"); double n=sc.nextDouble(); aufgabe39 test=new aufgabe39(n); System.out.println("the sum is "+test.SumAll(n)); sc.close(); } }
【程序40】
题目:字符串排序。
public class aufgabe40 { private String s1; private String s2; aufgabe40(String s1,String s2) { this.s1=s1; this.s2=s2; } String compare() { for(int i=0;i<s1.length() && i<s2.length();i++) { if(s1.charAt(i)<s2.charAt(i)) return s1; else if(s1.charAt(i)>s2.charAt(i)) return s2; } return s1+" == "+s2; } public static void main(String[] args) { String s1="abcde"; String s2="accde"; aufgabe40 test=new aufgabe40(s1,s2); System.out.println("the smaller String is : "); System.out.println(test.compare()); } }
【程序41】
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一
个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中
,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
public class aufgabe41 { static int Divide(int a) { int sum=a; int flag=0; while((a-1)%5 == 0 && flag<5) { a=(a-1)/5*4; flag++; } if(flag==5) return sum; else return -1; } public static void main(String[] args) { for(int i=0;i<10000;i++) { if(aufgabe41.Divide(i)!=-1) { System.out.println("there are at least "+aufgabe41.Divide(i)+" peaches at first."); break; } } } }
【程序42】
题目:809*??=800*??+9*??+1
其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。
public class aufgabe42 { public static void main(String[] args) { for(int i=10;i<100;i++) { int a=800*i; int b=9*i; if(809*i == (a+b+1) && (a/100)>=10 && (a/100)<100 && b>=100 && b<1000 ) { System.out.println(i); break; } } System.out.println("can't find it "); } }
【程序43】
题目:求0—7所能组成的奇数个数。
public class aufgabe43 { static int factor(int n) { if(n==0) return 1; else return n*factor(n-1); } static int A(int n,int m) { return factor(n)/factor(n-m); } static int BioSum(int n) { int sum=0; for(int i=1;i<=n;i++) { sum=sum+A(n,i)-A((n-1),(i-1)); } return sum*4+4; } public static void main(String[] args) { int num=7; System.out.println(aufgabe43.BioSum(num)); } }
【程序44】
题目:一个偶数总能表示为两个素数之和。
public class aufgabe44 { private int even; aufgabe44(int even) { this.even=even; } boolean Prime(int a) { for(int i=2;i<a;i++) { if(a%i==0) return false; } return true; } boolean factor(int k) { for(int i=1;i<k;i++) { if(Prime(k-i) && Prime(i)) { System.out.println(k+"="+(k-i)+"+"+i); return true; } } System.out.println("kann't find for "+k); return false; } void Factors() { for(int i=2;i<=this.even;i=i+2) factor(i); } public static void main(String[] args) { aufgabe44 test=new aufgabe44(18); test.Factors(); } }
结果吻合。
【程序45】
题目:判断一个素数能被几个9整除
public class aufgabe45 {
private int range;
private int sum;
aufgabe45(int range)
{
this.range=range;
sum=0;
}
boolean Prime(int a)
{
for(int i=2;i<a;i++)
{
if(a%i==0)
return false;
}
return true;
}
boolean factor(int even)
{
while(even%9==0)
{
sum++;
}
if(this.sum!=0)
{
System.out.println(even+" kann be divided by 9 "+sum+" times.");
return true;
}
else
{
System.out.println(even+" kann't be divided by 9 .");
return false;
}
}
void Divide()
{
for(int i=2;i<=this.range;i++)
{
if(this.Prime(i))
this.factor(i);
}
}
public static void main(String[] args)
{
aufgabe45 test=new aufgabe45(100);
test.Divide();
}
}
【程序46】
题目:两个字符串连接程序
public class aufgabe46 {
public static void main(String[] args)
{
String s1="abv";
String s2="frg";
String s3=s1+s2;
System.out.println(s3);
}
}
【程序47】
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
public class aufgabe47 {
static void Draw(int n)
{
for(int i=0;i<n;i++)
{
System.out.print("*");
}
System.out.println();
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("please input 7 numbers between 1-50.");
int i=0;
while(sc.hasNext() && i<7)
{
aufgabe47.Draw(sc.nextInt());
i++;
}
System.out.println("input finished.");
sc.close();
}
}
【程序48】
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字
都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
public class aufgabe48 {
private int code;
private int[] digit;
aufgabe48(int code)
{
this.code=code;
digit=new int[4];
}
void Encode()
{
for(int i=1;i<5;i++)
{
digit[(i-1)]=this.Translate( (int)(this.code/Math.pow(10,i-1) ) %10);
}
DigitSwap(1,4);
DigitSwap(2,3);
}
void DigitSwap(int i,int j)
{
int temp=this.digit[i-1];
this.digit[i-1]=this.digit[j-1];
this.digit[j-1]=temp;
}
int Translate(int n)
{
System.out.println("the input digit is "+n+",the encoded digit is "+((n+5)%10));
return (n+5)%10;
}
void show()
{
System.out.println("the original number is "+this.code);
System.out.print("the encoded number is ");
for(int i=0;i<this.digit.length;i++)
{
System.out.print(this.digit[i]);
}
System.out.println();
}
public static void main(String[] args)
{
int code=7364;
aufgabe48 test=new aufgabe48(code);
test.Encode();
test.show();
}
}
结果吻合。
【程序49】
题目:计算字符串中子串出现的次数
【程序50】
题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算
出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
相关文章推荐
- JAVA基础编程50题(13-15题)详解
- JAVA基础编程50题(7-9题)详解
- JAVA基础编程50题(16-18题)详解
- JAVA基础编程50题(4-6题)详解
- JAVA基础编程50题(19-21题)详解
- JAVA基础编程50题(25-27题)详解
- JAVA基础编程50题(4-6题)具体解释
- JAVA基础编程50题(10-12题)具体解释
- JAVA基础编程50题(22-24题)详解
- JAVA基础编程50题(10-12题)详解
- JAVA基础编程50题(1-3题)详解
- java程序员面试复习资料2——[JAVA编程基础]
- Java 编程基础,应用与实例
- java基础编程学习-1
- Java操作数据库编程基础
- Java网络编程基础 ServerSocket类使用
- Java传说之风云再起-Java基础-Java-编程开发
- Java网络编程基础 1 建立一个提供当前时间字符串的ServerSocket
- Java多线程编程基础之线程对象
- Java多线程编程基础