要求从键盘输入10个数并按从小到大的顺序排序。用JAVA做。
public static void main(String args[]){
/*int [] num={1,2,5,4,3,6};
Arrays.sort(num);
for(int i=0;i<num.length;i++)
System.out.println(num[i]);*/
int[] sz=new int[10];
int n=0;
String str;
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
try{
while(n<10)
{
str=br.readLine();
sz[n]=Integer.parseInt(str);
n++;
}
Arrays.sort(sz);
//sort(sz,10);
for(int i=0;i<10;i++)
System.out.println(sz[i]);
}
catch(Exception e){}
}
private static void sort(int[] sz,int n) {
int temp,i,j;
for(i=n-1;i>0;i--)
for(j=0;j<i;j++)
if(sz[j]>sz[j+1])
{
temp=sz[j];
sz[j]=sz[j+1];
sz[j+1]=temp;
}
}
1.父子俩的年龄:父亲今年30岁,儿子今年6岁,问多少年后父亲的年龄是儿子年龄的2倍。*/
package exec;.
public class Age ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int father = 30;
int son = 6;
for(int i=1;i<100;i++)
...{
if((father+i)==((son+i)*2))...{
System.out.println(i + " 年");} } }}
2.编写程序,将两个各有6个整数的数组,合并成一个由小到大排列的数组,(该数组的长度为12)*/
package exec;
import java.util.Arrays;
public class Arrary ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int a[] = ...{ 1, 3, 5, 8, 9, 4 };
int b[] = ...{ 2, 4, 7, 6, 5, 9 };
int c[] = new int[12];
for (int i = 0; i < 6; i++) ...{
c[i] = a[i];
c[i + 6] = b[i];
}
Arrays.sort(c);
for (int i = 0; i < 12; i++) ...{
System.out.print(c[i] + " "); } }}
3.一个球从100米高度自由落下后,反弹回原来高度的一半。按此规律,到第10次落地时,行程共有多少米? * 然后将反弹起多高? */
package exec;
public class BallFall ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
float heigher = 100;
float sum = 0;
for (int i = 1; i < 10; i++) ...{
heigher /= 2;
sum += heigher;
System.out.println("第" + i + "次" + "总行程" + sum + "米" + "反弹"
+ heigher + "米"); } }}
4. 换硬币:把一元人民币换成5分、2分、1分的硬币,有多少种换法? */
package exec;
public class ChangeCoin ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int num = 0;
for (int i = 1; i <= 20; i++) ...{
for (int j = 1; j <= 50; j++) ...{
for (int k = 1; k <= 100; k++) ...{
if (5 * i + 2 * j + 1 * k == 100) ...{
num += 1; } } } }
System.out.print(num + "种换法"); }}
5.牛的繁殖问题 :有位科学家曾出了这样一道数学题:有一头母牛,它每年年初要生一头小母牛;
* 每头小母牛从第四个年头起,每年年初也要生一头小母牛。按此规律,若无牛死亡,第20年头上共有多少头母牛?*/
package exec;
public class CountCow ...{
int[] cowNumber = new int[21];
public void count(int increaseCycle, int years) ...{
cowNumber[0] = 1;
for (int currentYear = 1; currentYear <= years; currentYear++) ...{
if (currentYear < increaseCycle) ...{
cowNumber[currentYear] = cowNumber[currentYear - 1] + 1;
} else ...{
cowNumber[currentYear] = cowNumber[currentYear - 1]
+ cowNumber[currentYear - increaseCycle + 1];
}
System.out.println("第" + currentYear + "年" + cowNumber[currentYear]); } }
public static void main(String[] args) ...{
int increaseCycle = 4;
int years = 20;
CountCow cow = new CountCow();
cow.count(increaseCycle, years); }}
6.打印出500之内所有能被7或9整除的数*/
package exec;
public class Divide ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
for (int i = 0; i <= 500; i++) ...{
if ((i % 7 == 0) | (i % 9 == 0)) ...{
System.out.print(i + " "); } } }}
7. * 编写一个Java程序 使它随即产生1800到2000之间的年份,打印它是否是一个闰年,闰年是1584年以后的年份,它要能被400整除, * 要能被4整除但是不能被100整除,已知使用Math.random()方法可以产生0到1之间的随即小数. ) */
package exec;
public class EmergerYear ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++) ...{
long year = (Math.round(Math.random() * 200) + 1800);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ...{
System.out.println(year + "是闰年!");
} else System.out.println(year + "不是闰年!"); } }}
8.编写程序以递归的方式实现1+2+3+...+n(n=200)的计算.*/
package exec;
public class Number ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
System.out.print(numbers(200));
} public static int numbers(int n)...{
return n<=1?1:n+numbers(n-1); }}
9.给定一个数,并由键盘输入若干个数,找出与预先给定的数最接近的数,并指出它是由键盘输入的第几个数。
package exec;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NumberCompare ...{
public static final int NUMBER = 80; // 给定一个要比较的数
public static void main(String args[]) ...{
System.out.print("Please input numbers: ");
BufferedReader io = new BufferedReader(new InputStreamReader(System.in));
try ...{
// 从键盘输入若干数
String line = io.readLine();
// 放入数组中
String[] items = line.split(" ");
int temp = 5000, x = 0, place = 0;
System.out.println("target number: 80");
// 和给定的数比较大小
for (int i = 0; i < items.length; i++) ...{
if (Integer.parseInt(items[i]) > NUMBER) ...{
x = Integer.parseInt(items[i]) - NUMBER;
} else ...{
x = NUMBER - Integer.parseInt(items[i]);
}
if (x < temp) ...{
temp = x;
place = Integer.parseInt(items[i]); } }
System.out.println("place: " + place);
io.close();
System.exit(0);
} catch (IOException e) ...{
e.printStackTrace(); } }}
10.使用Java,long.Math类,生成10个0到99之间的随即整数,求出它们中的最小值和最大值*/
package exec;
import java.util.Iterator;
import java.util.TreeSet;
public class RandomNumber ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
/** *//**方法1
long max = 50;
long min = 50;
for(int i=0; i<10; i++){
long number = Math.round(Math.random()*99 + 1);
System.out.print(number + " ");
max = Math.max(max, number);
min = Math.min(min, number);
if(i == 9){
System.out.print(" Math MaxNumber=" + max + " MinNumber=" + min); } }
//方法2
TreeSet ts = new TreeSet();
for(int i=0; i<10; i++)...{
long number = Math.round(Math.random()*99 + 1);
System.out.print(number + " ");
ts.add(number);
}
System.out.println();
Iterator it = ts.iterator();
while(it.hasNext())...{
System.out.print(it.next() + " "); }
System.out.print(" Math MaxNumber=" + ts.last() + " MinNumber=" + ts.first());}}
11.假设有一条钢材长2000米,每天截取其中的一半,编写一程序求出多少天后,钢材的长度小于5米.*/
package exec;
public class SteelSection ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int i = 0;
float steel = 2000f;
while (steel >= 5) ...{
i++;
steel = steel / 2;
System.out.println(i + "天后钢管长" + steel + "米"); } }}
12.完全数是指其所有因子(包括1但不包括该数自身)的和等于该数,例如28=1+2++4+7+14,28就是一个完全数. * 编写一个程序求出2到10000之间的所有完全数.*/
package exec;
public class WanNumber ...{
public static void main(String[] args) ...{
// TODO Auto-generated method stub
int m = 0;
for (int n = 2; n <= 10000; n++) ...{
m = 0;
for (int i = 1; i < n; i++) ...{
if (n % i == 0) ...{
m += i;
//if (m == n) ...{ System.out.println(n + "个完全数"); }
}
}if (m == n) ...{ System.out.println(n + "个完全数"); } } }}
//////////////////////////////
int n,sum =0;
for ( int i = 2; i <= 10000;i++)
{
sum =0;
for ( int j = 1;j < i; j++)