Java面试题,06

1)编写程序,对A[]={30,1,-9,70,25}数组由小到大排序

public class booktest {

public static void main(String[] args) {

int a[]={30,1,-9,70,25};

System.out.print(“数组原始顺序:“);

for (int i=0;i<a.length;i++) System.out.print(a[i] + ” ”);

for (int i = 0; i < a.length; i++) {

int lowerIndex = i;

for (int j = i + 1; j < a.length; j++)

if (a[j] < a[lowerIndex]) lowerIndex = j;

int temp = a[i];

a[i] = a[lowerIndex];

a[lowerIndex] = temp;

}

System.out.print(“\n数组排序后的顺序: “);

for (int i=0;i<a.length;i++) System.out.print(a[i] + ” ”);

}

}

2)编写程序,求2-1000内的所有素数,并按每行5列的格式输出。

public class PrimeTest{

public static void main(String args[]) {

int num=2;

System.out.print(2 + ” ”);

for(int i=3;i<=1000;i+=2){

boolean f = true;

for (int j=2;j<i;j++) {

if(i % j == 0){

f= false;

break;

}

}

if(!f) {continue;}

System.out.print(i + ” ”);

if(num++%5 == 0)System.out.println();

}

}

}

3)编写程序,生成100个1~6之间的随机数,统计1~6每个数字出现的概率。

public class RandomTest {

public static void main(String[]args){

int[] randomnum=new int[100];

int[] n=new int[6];

double a;

for(int i=0;i<100;i++){

a = Math.random()*6;

a = Math.ceil(a);

randomnum[i] = new Double(a).intValue();

System.out.print(randomnum[i]);

switch (randomnum[i]){

case 1: n[0]++; break;

case 2: n[1]++; break;

case 3: n[2]++; break;

case 4: n[3]++; break;

case 5: n[4]++; break;

case 6: n[5]++; break;

}

}

System.out.println();//以下可改为循环输出

System.out.println(“ 数字1出现的概率=”+(n[0]/100.0)*100+”%”);

System.out.println(“ 数字2出现的概率=”+(n[1]/100.0)*100+”%”);

System.out.println(“ 数字3出现的概率=”+(n[2]/100.0)*100+”%”);

System.out.println(“ 数字4出现的概率=”+(n[3]/100.0)*100+”%”);

System.out.println(“ 数字5出现的概率=”+(n[4]/100.0)*100+”%”);

System.out.println(“ 数字6出现的概率=”+(n[5]/100.0)*100+”%”);

}

}

4)编写程序,求1!+2!+3!+…+15!。

public class FactorialSum {

static int f(int x) {

if (x<=0) return 1;

else

return x*f(x-1);

}

public static void main(String[]args){

int sum=0;

for(int j=1;j<=15;j++)

{

sum+=f(j);

}

System.out.println(sum);

}

}

5)编写程序,分别用do-while和for循环计算1+1/2!+1/3!+1/4!+…的前15项的和。

for循环代码:

public class For_FactorialSum {

static int f(int x) {

if (x<=0) return 1;

else

return x*f(x-1);

}

public static void main(String[]args){

double sum=0;

for(int j=1;j<=15;j++)

{

sum+=1.0/f(j);

}

System.out.println(sum);

}

}

do-while循环代码:

public class DoWhile_FactorialSum {

static int f(int x) {

if (x<=0) return 1;

else

return x*f(x-1);

}

public static void main(String[]args){

double sum=0;

int j=1;

do {

sum+=1.0/f(j);

j++;

}

while(j<=15);

System.out.println(sum);

}

}

6)编写一个程序,用选择法对数组a[]={20,10,55,40,30,70,60,80,90,100}进行从大到小的排序

分别采用冒泡排序、选择排序和插入排序方法)

public class SortAll {

public static void main(String[] args) {

int a[]={20,10,55,40,30,70,60,80,90,100};

System.out.println(“—-冒泡排序的结果:“);

maoPao(a);

System.out.println();

System.out.println(“—-选择排序的结果:“);

xuanZe(a);

System.out.println();

System.out.println(“—-插入排序的结果:“);

chaRu(a);

}

// 冒泡排序

public static void maoPao(int[] x) {

for (int i = 0; i < x.length; i++) {

for (int j = i + 1; j < x.length; j++) {

if (x[i] > x[j]) {

int temp = x[i];

x[i] = x[j];

x[j] = temp;

}

}

}

for (int i : x) {

System.out.print(i + ” ”);

}

}

// 选择排序

public static void xuanZe(int[] x) {

for (int i = 0; i < x.length; i++) {

int lowerIndex = i;

// 找出最小的一个索引

for (int j = i + 1; j < x.length; j++) {

if (x[j] < x[lowerIndex]) {

lowerIndex = j;

}

}

// 交换

int temp = x[i];

x[i] = x[lowerIndex];

x[lowerIndex] = temp;

}

for (int i : x) {

System.out.print(i + ” ”);

}

}

// 插入排序

public static void chaRu(int[] x) {

for (int i = 1; i 从1开始,因为第1个数已经是排好序的

for (int j = i; j > 0; j–) {

if (x[j] < x[j - 1]) {

int temp = x[j];

x[j] = x[j - 1];

x[j - 1] = temp;

}

}

}

for (int i : x) {

System.out.print(i + ” ”);

}

}

}

7)编写程序,产生30个素数,按从小到大的顺序放入数组prime[]中。

public class PrimeArray {

public static void main(String args[]) {

int[] primearry=new int[30];

primearry[0]=2;

int num=1;

System.out.print(2 + ” ”);

for(int i=3;i<=1000;i+=2){

boolean f = true;

for (int j=2;j<i;j++) {

if(i % j == 0){

f= false;

break;

}

}

if(!f) {continue;}

primearry[num++]=i;

System.out.print(i + ” ”);

if(num%5 == 0)System.out.println();

if(num==30)break;

}

}

}

8)一个数如果恰好等于它的因子之和,这个数就称为“完数”。分别编写一个应用程序和小应用程序求 1000之内的所有完数。

public class Wanshu {

public static void main(String[] args) {

int sum=0,i,j;

for(i=1;i<=1000;i++)

{

for(j=1,sum=0;j<i;j++)

{

if(i%j==0)

sum=sum+j;

}

if(sum==i)

{

System.out.print (“完数:”+i+” ”+”其因子是:“ );

for(int k=1;k<=sum/2;k++)

{

if(sum%k==0)

System.out.print(“ ”+k);

}

System.out.println();

}

}

}

}

9)从键盘读取若干个数,以“-1”结束,按从小到大的顺序排序

import java.util.Scanner;

public class sc_num {

public static void main(String[] args) {

Scanner scanner=new Scanner(System.in);

int scnum=0,i=0;

int []scarry=new int[30];

System.out.println(“输入整数(-1结束):”);

while(scnum!=-1){

scarry[i]=scanner.nextInt();;

scnum=scarry[i];

i++;

}

xuanZe(scarry,i-1);

}

// 选择排序

public static void xuanZe(int[] x,int n) {

for (int i = 0; i <n; i++) {

int lowerIndex = i;

for (int j = i + 1; j < n; j++) {

if (x[j] < x[lowerIndex]) {

lowerIndex = j;

}

}

int temp = x[i];

x[i] = x[lowerIndex];

x[lowerIndex] = temp;

}

for (int i=0;i<n;i++) {

System.out.print(x[i] + ” ”);

}

}

}