【程序31】 题目:将一个数组逆序输出。
程序分析:用第一个与最后一个交换。 其实,用循环控制变量更简单: for(int k=11;k>=1;k--) System.out.print(myarr[k]+","); 【程序32】 题目:取一个整数a从右端开始的4~7位。 程序分析:可以这样考虑: (1)先使a右移4位。 (2)设置一个低4位全为1,其余全为0的数。可用~(~0 < <4) (3)将上面二者进行&运算。 public class Ex32 { public static void main(String[] args) { int a=0; long b=18745678; a=(int) Math.floor(b % Math.pow(10,7)/Math.pow(10, 3)); System.out.println(a); } } 【程序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 Ex33 { public static void main(String args[]){ int i,j; int a[][]; a=new int[8][8]; for(i=0;i<8;i++){ a[i][i]=1; a[i][0]=1; } for(i=2;i<8;i++){ for(j=1;j<=i-1;j++){ a[i][j]=a[i-1][j-1]+a[i-1][j]; } } for(i=0;i<8;i++){ for(j=0;j<i;j++){ System.out.printf(" "+a[i][j]); } System.out.println(); } } } 【程序34】 题目:输入3个数a,b,c,按大小顺序输出。 1.程序分析:利用指针方法。 public class Ex34 { public static void main(String[] args) { int []arrays = {800,56,500}; for(int i=arrays.length;--i>=0;) { for(int j=0;j<i;j++) { if(arrays[j]>arrays[j+1]) { int temp=arrays[j]; arrays[j]=arrays[j+1]; arrays[j+1]=temp; } } } for(int n=0;n<arrays.length;n++) System.out.println(arrays[n]); } } 【程序35】 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 import java.util.*; public class Ex35 { public static void main(String[] args) { int i, min, max, n, temp1, temp2; int a[]; System.out.println("输入数组的长度:"); Scanner keyboard = new Scanner(System.in); n = keyboard.nextInt(); a = new int[n]; for (i = 0; i < n; i++) { System.out.print("输入第" + (i + 1) + "个数据"); a[i] = keyboard.nextInt(); } //以上是输入整个数组 max = 0; min = 0; //设置两个标志,开始都指向第一个数 for (i = 1; i < n; i++) { if (a[i] > a[max]) max = i; //遍历数组,如果大于a[max],就把他的数组下标赋给max if (a[i] < a[min]) min = i; //同上,如果小于a[min],就把他的数组下标赋给min } //以上for循环找到最大值和最小值,max是最大值的下标,min是最小值的下标 temp1 = a[0]; temp2 = a[min]; //这两个temp只是为了在交换时使用 a[0] = a[max]; a[max] = temp1; //首先交换a[0]和最大值a[max] if (min != 0) { //如果最小值不是a[0],执行下面 a[min] = a[n - 1]; a[n - 1] = temp2; //交换a[min]和a[n-1] } else { //如果最小值是a[0],执行下面 a[max] = a[n - 1]; a[n - 1] = temp1; } for (i = 0; i < n; i++) { //输出数组 System.out.print(a[i] + " "); } } } 【程序36】 题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数 【程序37】 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。 import java.util.Scanner; public class Ex37 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); boolean[] arr = new boolean[n]; for(int i=0; i<arr.length; i++) { arr[i] = true;//下标为TRUE时说明还在圈里 } int leftCount = n; int countNum = 0; int index = 0; while(leftCount > 1) { if(arr[index] == true) {//当在圈里时 countNum ++; //报数递加 if(countNum == 3) {//报道3时 countNum =0;//从零开始继续报数 arr[index] = false;//此人退出圈子 leftCount --;//剩余人数减一 } } index ++;//每报一次数,下标加一 if(index == n) {//是循环数数,当下标大于n时,说明已经数了一圈, index = 0;//将下标设为零重新开始。 } } for(int i=0; i<n; i++) { if(arr[i] == true) { System.out.println(i); } } } } 【程序38】 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。 import java.util.Scanner; public class Ex38 { public static void main(String [] args) { Scanner s = new Scanner(System.in); System.out.println("请输入一个字符串"); String mys= s.next(); System.out.println(str_len(mys)); } public static int str_len(String x) { return x.length(); } } 题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n 【程序39】 题目:字符串排序。 import java.util.*; public class test{ public static void main(String[] args) { ArrayList<String> list=new ArrayList<String>(); list.add("010101"); list.add("010003"); list.add("010201"); Collections.sort(list); for(int i=0;i<list.size();i++){ System.out.println(list.get(i)); } } } 【程序40】 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子? public class Dg { static int ts=0;//桃子总数 int fs=1;//记录分的次数 static int hs=5;//猴子数... int tsscope=5000;//桃子数的取值范围.太大容易溢出. public int fT(int t){ if(t==tsscope){ //当桃子数到了最大的取值范围时取消递归 System.out.println("结束"); return 0; } else{ if((t-1)%hs==0 && fs <=hs){ if(fs==hs) { System.out.println("桃子数 = "+ts +" 时满足分桃条件"); } fs+=1; return fT((t-1)/5*4);// 返回猴子拿走一份后的剩下的总数 } else { //没满足条件 fs=1;//分的次数重置为1 return fT(ts+=1);//桃子数加+1 } } } public static void main(String[] args) { new Dg().fT(0); } } |
|