每天10道编程题-第四天
2014-12-04 21:42
281 查看
【程序31】 题目:将一个数组逆序输出。
【程序32】 题目:取一个整数a从右端开始的4~7位。
【程序33】
题目:打印出杨辉三角形(要求打印出10行如下图)
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,按大小顺序输出。
【程序35】 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
【程序36】 题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
【程序37】 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
【程序38】 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
【程序39】 题目:字符串排序。
【程序40】
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
package com.daliu.suanfa4; public class Exp31 { /** * 【程序31】 题目:将一个数组逆序输出。 */ public static void main(String[] args) { int myarr[]={1,6,9,8,5,3,7,6}; for(int k=myarr.length-1;k>=0;k--) System.out.print(myarr[k]+","); } }
【程序32】 题目:取一个整数a从右端开始的4~7位。
package com.daliu.suanfa4; public class Exp32 { 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 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
package com.daliu.suanfa4; public class Exp33 { 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,按大小顺序输出。
package com.daliu.suanfa4; public class Exp34 { 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.print(arrays +" "); } }
【程序35】 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
package com.daliu.suanfa4; import java.util.*; public class Exp35 { 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 ; 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的人退出圈子,问最后留下的是原来第几号的那位。
package com.daliu.suanfa4; import java.util.Scanner; public class Exp37 { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); boolean[] arr = new boolean ; 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函数中输入字符串,并输出其长度。
package com.daliu.suanfa4; import java.util.Scanner; public class Exp38 { 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(); } }
【程序39】 题目:字符串排序。
package com.daliu.suanfa4; import java.util.*; public class Exp39 { 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】
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
package com.daliu.suanfa4; public class Exp40 { 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 Exp40().fT(0); } }
相关文章推荐
- 每天10道编程题-第一天
- 每天10道编程题-第二天
- 每天10道编程题-第三天
- 每天一道编程题----------------淘宝小白鼠喝药问题
- 每天一道编程题(九)----------堆排序
- 每天一道编程题(2)
- 【编程题】Java基础测试四(10道)
- 每天一道编程题(八)--------百钱买百鸡
- 每天一道编程题----------------辗转相除法求两个数的最大公约数及其证明
- 每天一道编程题(八)----------牛顿迭代法求根式
- 每天几道编程题
- 每天一题LeetCode[第四天]
- C语言 每天做一道编程题
- 每天一道编程题(十)---------java竞赛称砝码问题
- 每天一道编程题------------百度蚂蚁爬杆问题
- 每天一道编程题(1)
- 【编程题】Java编程题五(10道)
- 每天一道编程题(一)----24点游戏
- 每天一道编程题(六)-------希尔排序
- scjp310-065考题每天10道题1