JAVA程序猿必做算法题(45题)

程序题1:NonRepeatingNumber1
题目:有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

package work;

public class NonRepeatingNumber {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int data[]=new int[4];
		data[0]=1;
		data[1]=2;
		data[2]=3;
		data[3]=4;
		int shu=0;
		System.out.println("不同的数有:");
		for(int m=0;m<=3;m++) {
			for(int n=0;n<=3;n++) {
				for(int p=0;p<=3;p++) {
					if(m!=n && m!=p && n!=p) {	
						System.out.print(""+data[m]+data[n]+data[p]+" ");
						shu++;
					}
				}
			}
		}
		System.out.println();
		System.out.println("个数:");
		System.out.println(shu);
	
	}
}

程序题2:ProfitCommission2
题目:企业发放的奖金根据利润提成。
利润(D)低于或等于10万元时,奖金可提10%;
利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10 万元的部分,可提成7.5%;
20万到40万之间时,高于20万元的部分,可提成5%;
40万到60万之间时高于40万元的部分,可提成3%;
60万到100万之间时,高于60万元的部分,可提成1.5%,
高于100万元时,超过100万元的部分按1%提成,
从键盘输入当月利润I,求应发放奖金总数?

package work;

import java.util.Scanner;

public class ProfitCommission {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println("输入利润:");
		double jj;
		//从键盘上获取数字i
		Scanner sc=new Scanner(System.in);
		int i=sc.nextInt();
		if(i<=10){
			jj=i*0.1;
		}
		else if(i>10 && i<=20){
			jj=10*0.1+(i-10)*0.075;
		}
		else if(i>20 && i<=40){
			jj=10*0.1+100*0.075+(i-20)*0.05;
		}
		else if(i>=40 && i<60){
			jj=10*0.1+100*0.075+20*0.05+(i-40)*0.03;
		}
		else if(i>=60 && i<100){
			jj=10*0.1+100*0.075+20*0.05+20*0.03+(i-60)*0.05;
		}
		else{
			jj=10*0.1+100*0.075+20*0.05+20*0.03+40*0.05+(i-100)*0.01;
		}
		System.out.println(jj);

	}
}

程序题3:SquareNumbe3
题目:一个整数,它加上100后是一个完全平方数,或者加上168又是一个完全平方数,请问该数是多少?

package work;

public class SquareNumber {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//方法一:
		for(int i=0;i<1000;i++){
			for(int j=0;j<1000;j++){
				for(int m=0;m<1000;m++){
					if( (i+100)==j*j && (i+168)==m*m ){
System.out.println(i);
}
				}
			}	
		}
				
	}
}

//方法二:		
		for(int i=0;i<1000;i++){
			if( ispfs(i+100) && ispfs(i+168) ){
				System.out.println(i);
				break;
			}
		}
		

	}
	
	//创建判断平方数的函数
	public static boolean ispfs(int i){
		for(int j=0;j<1000;j++){
			if(i==j*j){
				return true;
			}
//return false; //如果写在这里,如果条件不满足for,那么函数就没有返回值
		}
		return false;
		
	}

程序题4:WhichDay4
题目:输入某年某月某日,判断这一天是这一年的第几天?

//1  2  3  4  5  6  7  8  9  10  11  12
//31 28 31 30 31 30 31 31 30 31  30  31
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
int year = sc.nextInt();
System.out.println("请输入月份");
int month = sc.nextInt();
System.out.println("请输入日期");
int day = sc.nextInt();

int days;
switch(month) {
    case 1:
        days = day;
        break;
    case 2:
        days = 31+day;
        break;
    case 3:
        days = 31+28+day;
        break;
    case 4:
        days = 31+28+31+day;
        break;
    case 5:
        days = 31+28+31+30+day;
        break;
    case 6:
        days = 31+28+31+30+31+day;
        break;
    case 7:
        days = 31+28+31+30+31+30+day;
        break;
    case 8:
        days = 31+28+31+30+31+30+31+day;
        break;
    case 9:
        days = 31+28+31+30+31+30+31+31+day;
        break;
    case 10:
        days = 31+28+31+30+31+30+31+31+30+day;
        break;
    case 11:
        days = 31+28+31+30+31+30+31+31+30+31+day;
        break;
    default:
        days = 31+28+31+30+31+30+31+31+30+31+30+day;
}
if( year%4==0 && year%100!=0 || year%400==0 ) {
    days++;
}
System.out.println("这是今年的第"+days+"天");

程序题5:Sort5
题目:输入三个数,把这三个数从小到大输出。

package work;

import java.util.Scanner;

public class Sort5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
				
		//方法一:冒泡排序法
		int data[]=new int[3];
		for(int i=0;i<3;i++)
		{
			Scanner sc=new Scanner(System.in);
			data[i]=sc.nextInt();
		}
		/*
		for(int j=0;j<data.length;j++)
		{
			for(int m=0,n;m<data.length-j-1;m++)
			{
				if(data[m]>data[m+1])
				{
					n=data[m+1];
					data[m+1]=data[m];
					data[m]=n;
				}
			}
		}
		System.out.print("排序:");
		for(int x=0;x<3;x++)
		{
			System.out.print(data[x]+"  ");
		}
		*/
		
		
		//方法二:if-else语句
		int max=0,min=0,zhong=0;
		if(data[0]>data[1])
		{
			max = data[0];
			min = data[1];
			if(max<data[2])
			{
				zhong = max;
				max = data[2];
			}
		}
		else if(data[1]>data[2])
		{
			max = data[1];
			min = data[2];
			if(min>data[0])
			{
				zhong = min;
				min = data[0];
			}
		}
		System.out.println("排序:"+min+" "+zhong+" "+max);	

	}
}

程序题6:CShape6
题目:用*号输出字母C的图案

package work;

public class CShape {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		for(int i=0;i<6;i++)
		{
			for(int j=0;j<9;j++)
			{
				if( i==0 && j!=8 && j!=7 )
				{
					System.out.print("*");
				}
				else if( j==0 )
				{
					System.out.println("*");
				}
				else if( i==5 )
				{
					System.out.print("*");
				}
			}
		}

	}

}

程序题7:Factor7
题目:将一个正整数分解质因数,例如:输入90,打印90=233*5

package work;

import java.util.Scanner;

public class Factor7 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//方法一
		Scanner sc=new Scanner(System.in);
		int m=sc.nextInt();
		System.out.print(m+"=");
		for(int i=2;i<m;i++)
		{
			if( m%i==0 && isPrime(i) )
			{
				System.out.print(i+"*");
				m /= i;
				i--;
			}
		}
		System.out.print(m);
		
		
		//方法二
		/*
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入数字:");
		int n = sc.nextInt();
		System.out.print(n+"="); // 进行前半部分的打印
		// 做出判断循环
		for(int k=2;k<=n;k++) {
			while(n!=k)
			{
				if(n%k==0)
				{
					System.out.print(k+"*");
					n=n/k;
				}
				else{break;}
			}
		}
		System.out.println(n); // 表示打印出的是最後一个数
		*/
		

	}
	
	
	public static boolean isPrime(int x){
		for(int i=2;i<x;i++)
		{
			if(x%i==0)
			{
				return false;
			}
			//return true; //放在这个位置,如果x不满足for的条件,那么函数就没有返回值
		}
		return true;
		
	}
}

程序题8:MultiplicationTable8
题目:输出九九乘法口诀表

package work;

public class MultiplicationTable {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println();
		System.out.println("程序题1:九九乘法表:");
		for(int i=1;i<=9;i++)
		{
			for(int j=1;j<=i;j++)
			{
				System.out.print(j+"*"+i+"="+j*i+"   ");
			}
			System.out.println();
		}

	}

}

程序题9:
题目:输出国际象棋棋盘

程序题10:Rabbit10
题目:古典问题,有一对兔子,从出生后第3个月开始每个月都生一对兔子,小兔子长到第三个之后每个月又生一对兔子,加入兔子都不死,问每个月的兔子总数为多少?1(对) 1 2 3 5 8 13 21 34 55 89…(斐波那契数列)

package work;

public class Rabbit10 {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// 1  1对
		// 2  1对
		// 3  2  1+1
		// 4  3  1 0
		// 5  5  1 0
		// 6  8  1 1
        // 7  13
		// 8  21
		
		//1 1 2 3 5 8 13 21 34 55 89...(斐波那契数列)
		
		//方法一
		/*
		int m=dd(9);
		System.out.println(m);
		*/
		
		int s=0,i=9;
		for (int m=3,x=1,y=1;m<=i;m++)
		{
			s=x+y;
			y=x;
			x=s;
		}
		System.out.print(s);
		
		
	}
		
	
	
	public static int dd(int m){
		/*
		int s = 0;
		if (i==1 || i==2){return 1;}
		for (int m=3,x=1,y=1;m<=i;m++)
		{
			s=x+y;
			y=x;
			x=s;
		}
		return s;
		*/


		if (m == 0 || m == 2)
		{
			return 1;
		}
		else
		{
			return dd(m - 1) + (m - 2);
		}
		
		
		
	}
	
	
	//方法二
	/*
	public static void main(String[] args) {
		//创建类
		Rabbit10 rf = new Rabbit10();
		System.out.println( rf.cc(10) );
	}
	
	int cc(int n)
	{
		if(n==1 || n==2)
		{return 1;}
		else
		{return cc(n-1)+cc(n-2);}
	}
	*/
	
}

程序题11:PrimeNumber11
题目:判断101~200之间有多少个素数,并且输出所有素数。

package work;

public class PrimeNumber {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int m = 0;
		System.out.print("素数:");
		for(int i=101;i<=200;i++)
		{
			if( isPrime(i) )
			{
				System.out.print(i+" ");
				m++;
			}
		}
		System.out.println();
		System.out.print("个数:");
		System.out.println(m);
		
	}
	
	
	public static boolean isPrime(int n){
	    for(int i=2;i<n;i++)
	    {
	        if (n%i==0) {
	            return false;
	        }
//return true; //放在这个位置,如果x不满足for的条件,那么函数就没有返回值
	    }
	    return true;
	}

}

程序题12:NarcissisticNumber12
题目:打印出所有的“水仙花数”,所谓“水仙花数”就是指一个三位数,其各位数字的立方和等于其本身。例如:153=111+555+333

package work;

public class NarcissisticNumber {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//例如:153=1*1*1+5*5*5+3*3*3
		System.out.print("水仙花数:");
		for(int i=100;i<1000;i++)
		{
			int m = i%100*i%100*i%100+i/10%10*i/10%10*i/10%10+i%100*i%100*i%100;
			if(i==m)
			{
				System.out.print(i+" ");
			}
		}
		

	}

}

程序题13:Achievement13
题目:利用条件运算符的嵌套来完成,学习成绩>=90分的同学用A表示,60~89分的同学用B表示,60分以下用C表示。

package work;

import java.util.Scanner;

public class Achievement13 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println("输入成绩:");
		Scanner sc=new Scanner(System.in);
		int achievement=sc.nextInt();
		System.out.print("等级:");
		if( achievement>=90 )
		{
			System.out.println("A");
		}
		else if( achievement>=60 && achievement<90 )
		{
			System.out.println("B");
		}
		else
		{
			System.out.println("C");
		}

	}

}

程序题14:CommonDivisor14
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

package work;

import java.util.Scanner;

public class CommonDivisor14 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
				
		//求最大公约数:方法一:借助辗转相除法(欧几里德算法)
		/*
		System.out.println("输入两个正整数:");
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt();
		int b=sc.nextInt();
		int zheng,yu,gys;
		do
		{
			zheng = a/b;
			yu = a%b;
			if( zheng==0 )
			{
				int m = a;
				a = b;
				b = m;
			}
			if( zheng>0 )
			{
				a = b;
				b = yu;
			}
			gys = a;
		}
		while(yu!=0);
		System.out.println("最大公约数为:"+gys);
		*/
		
		//1、求最大公约数:方法二:更相减损法(更相减损术)
		System.out.println("输入两个正整数:");
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt();
		int b=sc.nextInt();
		int a1=a,b1=b;
		int jg,gys;
		do
		{
			jg = a-b;
			if( jg<0 )
			{
				int m = a;
				a = b;
				b = m;
			}
			if( jg>0 )
			{
				a = b;
				b = jg;
			}
			gys = b;
			
		}
		while(jg!=0);
		System.out.println("最大公约数为:"+gys);
		
		
		//2、求最大公倍数
		int gbs;
		gbs = a1*b1/gys;
		System.out.println("最大公倍数为:"+gbs);
		

	}

}

程序题15:Number15
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。

String str = "123 abcdef  WXY*&@";
char arr[] = new char[str.length()];
for(int i=0;i<str.length();i++) {
    arr[i] = str.charAt(i);
}
int num1=0,num2=0,num3=0,num4=0;
for(int j=0;j<arr.length;j++) {
    char m = arr[j];
    if( m>'a'&&m<'z' || m>'A'&&m<'Z' ) {
        num1++;
    }else if( m==' ' ) {
        num2++;
    }else if( m>='0'&&m<'9' ) {
        num3++;
    }else {
        num4++;
    }
}
System.out.println("英文字母的个数:"+num1);
System.out.println("空格的个数:"+num2);
System.out.println("数字的个数:"+num3);
 System.out.println("其他字符的个数:"+num4);

程序题16:Accumulation16
题目:求s=a+aa+aaa+…+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222,几个数相加由键盘控制。

package work;

import java.util.Scanner;

public class Accumulation16 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
				
		Scanner sc = new Scanner(System.in);
		System.out.println("输入数字:");
		int a = sc.nextInt();
int a1 = a;
		System.out.println("控制n个数相加:");
		int n = sc.nextInt(); 
		int sum = 0;
		for(int i=1;i<=n;i++)
		{
			sum += a;
			a = 10*a+1;
		}
		System.out.println("结果为:"+sum);
		

	}

}

程序题17:PerfectNumber17
题目:一个数如果恰好等于它的因子之和,这个数成为“完数”,例如:6=1+2+3,编程找出1000以内所有的完数。

package work;

public class PerfectNumber17 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		for(int i=1;i<=1000;i++)
		{
			int sum = 0;
			for(int j=1;j<i;j++)
			{
				if( i%j==0 )
				{sum += j;}
			}
			if(sum==i)
			{System.out.print(i+" ");}
		}
		
	}

}

程序题18:Ball18
题目:一个球从100米高度自由落下,每次落地之后反弹的高度为原来一半,然后再次落下,求它第10次落下时,共经过了多少米?第10次反弹多高?

package work;

import java.util.Scanner;

public class Ball18 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		double h=100.0,sum=0;
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		for(int i=1;i<=n;i++)
		{
			sum += h;
			h /= 2;
		}
		sum = 2*sum-100;
		System.out.println(sum+" "+h);
		

	}

}

程序题19:Monkey19
题目:猴子吃桃问题,猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

package work;

public class Monkey19 {
	public static void main(String[] args) {
		
		int sum=1;
		for(int i=1;i<=10;i++)
		{
			sum = (sum+1)*2;
		}
		System.out.println(sum);

	}
}

程序题20:Match20
题目:两个兵乓队进行比赛,各出三人。甲队为a、b、c三人,乙队为x、y、z三人,以抽签决定比赛名单,有人向队员打听比赛的名单,a说他不和x比,c说他不和x、z比,请编程找出三队赛手的名单。

package work;

public class Match20 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//C--Y  A--Z  B--X
		for(char A='X';A<='Z';A++)
		{
			for(char B='X';B<='Z';B++)
			{
				if(A!=B)
				{
					for(char C='X';C<='Z';C++)
					{
						if(C!=A && C!=B && C!='X' && C!='Z' && A!='X' )
						{
							System.out.println("A--PK--"+A);
							System.out.println("B--PK--"+B);
							System.out.println("B--PK--"+C);
						}
					}
				}
			}
		}
		

	}
}

程序题21:Sum21
题目:有一分数序列:2/1 3/2 5/3 8/5 13/8 21/13…求出这个数列的前20项之和。

package work;

public class Sum21 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		double sum = 0;
//注意sum,x,y,m的声明要放在for循环外面,否者其值将会不变。
		double x=2.0,y=1.0,m;
		for(int i=1;i<=2;i++)
		{
			sum += x/y;
			m = x;
			x = x+y;
			y = m;
			//System.out.println(x+" "+y);
		}
		System.out.println(sum);

	}

}

程序题22:FactorialSum22
题目:求1+2!+3!+…+20!的和。

package work;

public class FactorialSum22 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int sum = 0;
		for(int i=1,jc;i<=20;i++)
		{
			jc = 1;
			for(int j=1,m=1;j<=i;j++)
			{
				jc *= j;
			}
			sum += jc;
		}
		System.out.println(sum);

	}

}

程序题23:Factorial23
题目:利用递归方法求5!

package work;

public class Factorial23 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//Factorial23 fun = new Factorial23();
		//System.out.println( fun.f(5) );
		System.out.println( f(5) );
	}
	public static int f(int i){
	//int f(int i){
		if(i==1)
		{return 1;}
		else
		{return f(i-1)*i;}
	}

}

程序题24:Contrary24
题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。

package work;

import java.util.Scanner;

public class Contrary24 {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
		//方法一:遍历数组方法
		int array[] = new int[5];
		for(int i=0;i<5;i++)
		{
			Scanner sc = new Scanner(System.in);
			array[i] = sc.nextInt();
		}
		for(int j=4;j>=0;j--)
		{
			System.out.print( array[j]+" " );
		}
		
		//方法二:递归函数(未实现)
		
	}
}

程序题25:Age25
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?

package work;

public class Age25 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int a1=10,a2,a3,a4,a5;
		for(a2=1;a2<200;a2++)
		{
			for(a3=1;a3<200;a3++)
			{
				for(a4=1;a4<200;a4++)
				{
					for(a5=1;a5<200;a5++)
					{
						if( a5==a4+2 && a4==a3+2 && a3==a2+2 && a2==a1+2 )
						{
							System.out.println("a1~a5年龄依次是:"+a1+" "+a2+" "+a3+" "+a4+" "+a5);
						}
					}
				}
			}
		}		

	}
}

程序题26:Number26
题目:给一个不多于5位的正整数,要求:一是求它是几位数,二是逆序打印出各位数字。

package work;

import java.util.Scanner;

public class Number26 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println("请输入一个不多于5位的正整数:");
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int n;
		if( num/10000>0 )
		{
			System.out.println(num+"是五位数");
			n = 5;
		}
		else if( num/1000>0 )
		{
			System.out.println(num+"是四位数");
			n = 4;
		}
		else if( num/100>0 )
		{
			System.out.println(num+"是三位数");
			n = 3;
		}
		else if( num/10>0 )
		{
			System.out.println(num+"是两位数");
			n = 2;
		}
		else
		{
			System.out.println(num+"是一位数");
			n = 1;
		}
		System.out.print("逆序打印各位数字:");
		double yu;
		for(double i=n-1;i>=0;i--)
		{
			yu = num%10;
			num = num/10;
			System.out.print( (int)yu+" " );
		}

	}

}

程序题27:PalindromeNumber27
题目:一个5位数,判断它是不是回文数。回文数:各位与万位相同,十位与千位相同,例如12321

package work;

import java.util.Scanner;

public class PalindromeNumber27 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		if( num/10000==num%10 && num/1000%10==num/10%10 )
		{
			System.out.println(num+"是回文数");
		}
		else
		{
			System.out.println(num+"不是回文数");
		}
		
	}
}

程序题28:Week28
题目:请输入星期几的第一个字符来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

Scanner sc = new Scanner(System.in);
    System.out.println("星期几的英文:");
    String str = sc.nextLine();
    String arr[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
    for(int i=0;i<arr.length;i++) {
        if( arr[i].equals(str) ) {
        switch(i) {
             case 0:
             System.out.println("星期一");
             break;
        case 1:
             System.out.println("星期二");
             break;
        case 2:
             System.out.println("星期三");
             break;
        case 3:
             System.out.println("星期四");
             break;
        case 4:
             System.out.println("星期五");
              break;
          case 5:
              System.out.println("星期六");
              break;
          case 6:
              System.out.println("星期日");
              break;
          }
     }

程序题29:Sort29
题目:对10个数进行排序。

package work;

import java.util.Scanner;

public class Sort29 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println("请输入10个数");
		Scanner sc = new Scanner(System.in);
		int array[] = new int[10];
		for(int i=0;i<array.length;i++)
		{
			array[i] = sc.nextInt();
		}
		System.out.print("原始数据:");
		for(int j=0;j<array.length;j++)
		{
			System.out.print(array[j]+" ");
		}
		System.out.println();
		for(int m=0;m<array.length;m++)
		{
			for(int n=0;n<array.length-m-1;n++)
			{
				if( array[n]>array[n+1] )
				{
					int x = array[n];
					array[n] = array[n+1];
					array[n+1] = x;
				}
			}
		}
		System.out.print("从小到大:");
		for(int j=0;j<array.length;j++)
		{
			System.out.print(array[j]+" ");
		}

	}
}

程序题30:DiagonalSum30
题目:求一个3*3矩阵对角线元素之和。

package work;

public class DiagonalSum30 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//    0  1  2
		//0   1  2  3
		//1   4  5  6
 		//2   7  8  9
		int array[][] = { {1,2,3},{4,5,6},{7,8,9} };
		int sum = 0;
		for(int i=0;i<array.length;i++)
		{
			for(int j=0;j<array[i].length;j++)
			{
				int h = i+j;
				//System.out.println(h); //测试使用
				if( i==j || (h==2 && i!=j) )
				{
					sum += array[i][j];
					//System.out.print(array[i][j]+" "); //测试使用
					//System.out.println(sum); //测试使用
				}
			}
		}
		System.out.println(sum);
		
	}
}

程序题31:Sort31
题目:有一个已经排好序的数组。现在输入一个数,要求按照原来的规律将它插入数组中。

package work;

import java.util.Scanner;

public class Sort31 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int array[] = {1,10,100,1000,1000};
		int array1[] = new int [6];
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		for(int i=0,j=0;i<array1.length;i++,j++)
		{
			array1[i] = array[j];
			if( array[j]<=num && num<array[j+1] )
			{
				array1[i+1]=num;
				//System.out.print(array1[i]+" "+i+" "+num); //测试
				i++;
			}
		}
		System.out.println();
		for(int i=0;i<array1.length;i++)
		{
			System.out.print(array1[i]+" ");
		}

	}

}

程序题32:ReverseOrder32
题目:将一个数组逆序输出。

package work;

public class ReverseOrder32 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int array[] = {1,2,3,4,5};
		for(int i=array.length-1;i>=0;i--)
		{
			System.out.print(array[i]+" ");
		}

	}

}

程序题33:Triangle33
题目:打印杨辉三角形(要求打印10行)

package work;

import java.util.Scanner;

public class Triangle33 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
				
		Scanner sc=new Scanner(System.in);
		int cc=sc.nextInt();
		int array[][]=new int [cc][]; //动态创建第一维
		for(int i=0;i<array.length;i++)
		{
			array[i]=new int [i+1]; //动态创建第二维
			for(int j=0;j<array[i].length;j++)
			{
				array[i][0]=array[i][i]=1;
				if( i>j && i!=j && j>0 )
				{
					array[i][j]=array[i-1][j]+array[i-1][j-1];
				}
			}
		}
		for(int i=0;i<array.length;i++)
		{
			for(int m=cc;m>=0;m--)
			{
				System.out.print(" ");
			}
			for(int j=0;j<array[i].length;j++)
			{
				System.out.print(array[i][j]+" ");
			}
			System.out.println();
			cc--;
		}
		
	}
}

程序题34:Transposition34
题目:输入数组,最大的与第一个元素交互,最小的与最后一个元素互换,输出数组。

package work;

public class Transposition34 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int array[] = {1,2,3,4,5,6,7,8,9,10};
		int max,min,m=0,n=0;
		max = min = array[0];
		for(int i=1;i<array.length;i++)
		{
			for(int j=1;j<array.length;j++)
			{
				if( max<array[i] )
				{
					max = array[i];
					m = i;
				}
				if( min>array[i])
				{
					min = array[i];
					n = i;
				}
			}
		}
		int replace1,replace2;
		replace1 = array[0];
		replace2 = array[array.length-1];
		array[0] = max;
		array[m] = replace1;
		array[array.length-1] = min;
		array[n] = replace2;
		for(int i=0;i<array.length;i++)
		{
			System.out.print(array[i]+" ");
		}

	}
}

程序题35:Transposition35
题目:有n个整数,使其前面的各数顺序向后移m个位置,最后m个数变成最前面的m个数。

package work;

import java.util.Scanner;

public class Transposition35 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int array[] = {1,2,3,4,5,6,7,8,9,10};
		int arr[] = new int[array.length];
		//System.arraycopy(array,0,arr,0,array.length); //复制数组
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		for(int i=0,j=array.length-m;i<m;i++,j++)
		{
			arr[i] = array[j];
		}
		for(int i=m,j=0;i<array.length;i++,j++)
		{
			arr[i] = array[j];
		}
		
		for(int i=0;i<arr.length;i++)
		{
			System.out.print(arr[i]+" ");
		}

	}
}

程序题36:
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

Scanner sc = new Scanner(System.in);
System.out.println("请输入人数:");
int n = sc.nextInt();
//用boolean数组表示人的状态
boolean[] b = new boolean[n];
//初始化  true---未淘汰  false---已淘汰
for(int i=0;i<b.length;i++){
    b[i] = true;
}
//人报数
int num = 0;
//剩余人数
int monkeyLeft = n;
//数组下标
int index = 0;
//循环
while(monkeyLeft>1){//判断条件
//检测人是否已淘汰
if(b[index]){
//报数
    num++;
    //判断报数是否为3
    if(num==3){
         b[index] = false;
         monkeyLeft--;
         num = 0;
    }
}
//下标移动
index++;
//围成一圈---最后一个置为0
if(index==n){
         index = 0;
     }
 }
 //打印出剩下的那个
 for(int i=0;i<b.length;i++){
     if(b[i]){
         int m = i+1;
         System.out.println("最后留下:"+m);
     }
 }

程序题37:Function37
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+…+1/n,当输入n为奇数时,调用函数1/1+1/3+…+1/n

package work;

import java.util.Scanner;

public class Function37 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//编写一个函数,输入n为偶数时,调用函数求1/2+1/4+.....+1/n,
		//当输入n为奇数时,调用函数1/1+1/3+......+1/n
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		if(n%2==0)
		{
			System.out.println( f1(n) );
		}
		else
		{
			System.out.println( f2(n) );
		}
		
	}
	
	public static double f1(int n){
		double sum = 0;
		for(int i=2;i<=n;)
		{
			sum += 1.0/i;
			i += 2;
		}
		return sum;
	}
	
	public static double f2(int n){
		double sum = 0;
		for(int i=1;i<=n;)
		{
			sum += 1.0/i;
			i += 2;
		}
		return sum;
	}

}

程序题38:
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

for(int i=10;i<10000;i++) {
int sum = i;
	int n = 0;
	while( sum%5==1 ) {
		//System.out.println("sum1="+sum);
		sum = (sum-1)/5*4;
		//System.out.println("sum2="+sum);

		n++;
		//System.out.println("n="+n);

		if(n==5) {
			//System.out.println("sum3="+sum);
			System.out.println("i="+i);
		}		
	}	
}

程序题39:
题目:809*?=800*?+9*?,其中?代表的是两位数,8*?的结果为二位数,9*??的结果为三位数,求?代表的量位数,以及809*?的结果。

package work;

public class Number39 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//809*? = 800*? + 9*?,其中?代表的是两位数,8*?的结果为二位数(比如12),
		//9*?的结果为三位数(比如123),求?代表的两位数,以及809*??的结果。
		
		for(int i=10;i<100;i++) {
			if( 809*i==800*i+9*i && 8*i/100==0 && 8*i/10>0 && 9*i/1000==0 && 9*i/100>0 ) {
				System.out.println(i);
				System.out.println(809*i);
			}
		}
		
		//int i = 12;
		//boolean m = 809*i==800*i+9*i && 8*i/100==0 && 8*i/10>0 && 9*i/1000==0 && 9*i/100>0;
		//System.out.println(m);
	}
}

程序题40:Octal40
题目:八进制转换为十进制。

package work;

public class Octal40 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//八进制转换为十进制。
		int b = 123;
		int s=0,m,zheng,yu;
		if(b/10==0) { m = 1; }
		else if( b%10!=0 && b/100==0 ) { m = 2; }
		else if( b%100!=0 ) { m = 3; }
		else { m = 4; }
		//System.out.println(m);
		for(int i=0;i<m;i++)
		{
			yu = b%10;
			zheng = b/10;
			b = zheng;
			s += yu*Math.pow(8,i);
		}
		System.out.println(s);

	}
}

程序题41:OddNumber41
题目:求0~9所能组成的奇数的个数。

package work;

public class OddNumber41 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//求0~9所能组成的奇数的个数。
		// 0~9876543210
		//当该数为1位数的时候,奇数有5位;
		//当该数为2位数的时候,奇数有5*8位;
		//当该数为3位数的时候,奇数有5*8*8位;
		//......
		//当该数为10位数的时候,奇数有5*8^(10-1)位
		int sum = 0;
		for(int i=1;i<=10;i++)
		{
			sum += 5*Math.pow(8, i-1);
		}
		System.out.println(sum);

	}
}

程序题42:EvenNumbers42
一个偶数总能表示为两个素数之和,0~1000内有多少个这样的偶数。

package work;

public class EvenNumbers42 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//一个偶数总能表示为两个素数之和,0~1000内有多少个这样的偶数。
		int sum = 0;
		for(int i=0;i<=1000;i++)
		{
			if( ss(i) )
			{
				sum++;
			}
		}
		System.out.println(sum);

	}
	
	public static boolean ss(int n)
	{
		for(int i=2;i<n;i++)
		{
			if(n%i==0)
			{
				return false;
			}
		}
		return true;
	}	
}

程序题43:PrimeNumber43
题目:判断一个素数能被几个9整除。

package work;

public class PrimeNumber43 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//判断一个素数能被几个9整除。
		int num = 81,n=0;
		do
		{
			if(num/9>0)
			{
				n++;
			}
			num /= 9;
		}
		while(num!=0);
		System.out.println(n);

	}
}

程序题44:Password44
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的。加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

package work;

public class Password44 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int num = 1234;
		int arr[] = new int[4];
		for(int i=1000,j=0;j<4;i/=10,j++)
		{
			arr[j] = (num/i+5)%10;
		}
		int m = 0;
		m = arr[0];
		arr[0] = arr[arr.length-1];
		arr[arr.length-1] = m;
		m = arr[1];
		arr[1] = arr[2];
		arr[2] = m;
		for(int i=0;i<arr.length;i++)
		{
			System.out.print(arr[i]+" ");
		}

	}
}

程序题45:
写一个函数,求一个字符串的长度,在main 函数中输入字符串,并输出其长度。

public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);
     System.out.println("请输入字符串");
     String str = sc.nextLine();
     int m = length(str);
     System.out.println(m);
}
public static int length(String str){
    String arr[] = str.split("");
    return arr.length;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值