欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基础编程题(四)

程序员文章站 2022-03-03 11:40:36
31.100 ~ 50000之间有多少整数,其各位数字之和为5,分别是哪些数(例如整数1324的各位数字之和为 1+3+2+4 等于10(不为5)),并统计满足条件的整数有多少个package cutestFox;public class Test022 {public static void main(String[] args) {int total = 0;for (int i = 100; i <= 50000; i++) {int a= i%10;...

31.100 ~ 50000之间有多少整数,其各位数字之和为5,分别是哪些数(例如整数1324的各位数字之和为 1+3+2+4 等于10(不为5)),并统计满足条件的整数有多少个

package cutestFox;

public class Test022 {
	public static void main(String[] args) {
		int total = 0;
		for (int i = 100; i <= 50000; i++) {
			
			int a= i%10;

	        int b=(i/10)%10;

	        int c=(i/100)%10;

	        int d=(i/1000)%10;
	        
	        int e = i/10000;

	        if (a+b+c+d+e==5) {
	        	
	        	System.out.println(i);

	                total++;
		}
   
	}
		 System.out.println("满足条件的整数有:"+total+"個");
	}
}

32.鸡兔同笼共80个头,208只脚,鸡和兔各有几只

package cutestFox;

public class Test020 {
	public static void main(String[] args) {

		for (int k = 0; k <= 80; k++) {
			for (int i = 0; i <= 80; i++) {
				if (4 * k + 2 * i == 208) {
					if (k + i == 80) {
						System.out.println("兔子:" + k);
					System.out.println("鸡:" + i);
					}
				}
			}
		}

	}

}

33.鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何

package cutestFox;
public class Test021 {
	public static void main(String[] args) {
		for(double a=0;a<=20;a++) {
			for(double b=0;b<=33;b++) {
				for(double c=0;c<=300;c=c+3) {
					if((a*5+b*3+(c/3))==100) {//钱数为100
						if(a+b+c==100)//鸡数为100
							System.out.println("鸡翁:"+a+"鸡母:"+b+"鸡雏:"+c);
					}
				}
			}
		}
		
	}

}

34.(计算将来的学费)假设今年某一大学的学费为10000,学费增长率为5%,使用循环语句编程,计算10年的学费.

package cutestFox.github.io.execise01;

public class Code018 {
	public static void main(String[] args) {
		double tuition = 10000;
		double rate = (5.0 / 100);
		for (int i = 1; i <= 10; i++) {
			System.out.println("第" + i + "年的学费为:" + tuition);
			tuition += (tuition) * (1 + rate);
		}

	}

}

35.(求满足n>12000的最小数 n) 用while循环求n平方大于12000的最小数n

package cutestFox.github.io.execise01;
public class Code019 {
	public static void main(String[] args) {
		int n = 0;
		while(n*n<12000) {
			n++;
		}
		System.out.println("n的平方大于12000的最小整数为:"+n);
	}

}

36.(查找能被5和6整除的数)编写程序,显示从100到1000之间所有能够被5和6整除的数,每行显示10个

package cutestFox.github.io.execise01;
public class Code020 {
	public static void main(String[] args) {
		int counter = 0;
		for(int i=100;i<1000;i++) {
			if(i%5==0&i%6==0) {
				counter++;
				System.out.print(i+"\t");
				if(counter%10==0)
					System.out.println();
			}
		}
		
	}

}

37.(求满足n<12000的最大数n)用while循环求n立方小于12000的最大数n

package cutestFox.github.io.execise02;

public class Code001 {
	public static void main(String[] args) {
		int n = 0;
		while(n*n*n<12000) {
			n++;
		}
		System.out.println("n的立方小于12000的最大整数为:"+(n-1));
	}

}

38.(显示ASCII字符表)编写程序,打印字符表中的128个字符。每行打印10个

package cutestFox.github.io.execise02;
public class Code002 {
	public static void main(String[] args) {
		int counter = 0;
		for(int i=(int)('!');i<=(int)('~');i++ ) {
			counter++;
			System.out.print((char)i+"\t");
			if(counter%10==0)
				System.out.println();
		}
	}

}

39.打印图案I

package cutestFox.github.io.execise02;
public class Code003 {
	public static void main(String[] args) {
		for(int i=1;i<=6;i++) {
			for(int j=1;j<=i;j++)
				System.out.print(j+"\t");
			System.out.println();
		}
		
	}

}

39.打印图案II

package cutestFox.github.io.execise02;
public class Code004 {
	public static void main(String[] args) {
		for(int i=6;i>=1;i--) {
			for(int j=1;j<=i;j++)
				System.out.print(j+"\t");
				System.out.println();
		
		}
	}

}

40.猜数字游戏一个类A有一个成员变量v。定义一个类对A类的成员变量v进行猜。如果大了则提示大了小了则提示 小了。等于则提示猜测成功。

解法一:

import java.util.Random;
import java.util.Scanner;

public class A {
	private Integer vv = 0;
	public A() {
		Random r = new Random();
		vv = r.nextInt(1000);
	}
	public String guess (Integer parameter1) {
		String res = "小了";
		if(parameter1!=null) {
			if(vv.compareTo(parameter1)==0)
				res = "相等";
			else if(vv.compareTo(parameter1)<0)
				res = "大了";
		}
		return res;
	}
	public static void main(String[] args) {
		A a = new A();
		Scanner sc =new Scanner(System.in);
		while(true) {
			System.out.println("input:");
			String ss = sc.nextLine();
			if("quit".equalsIgnoreCase(ss))
				break;
			try {
				int kk = Integer.parseInt(ss);
				String res = a.guess(kk);
				if("相等".equals(res)) {
					System.out.println("reight…………");
					break;
				}
				else {
					System.out.println(res);
				}
			} catch (Exception e) {
				System.out.println("不合法的輸入");
			}
			
		}
	}
}

解放二:

import java.util.Random;
import java.util.Scanner;

public class A {
	private int v=0;
	public A() {
		Random r = new Random();
		v = r.nextInt(1000);
	}
	public int Guess() {
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入你猜的數:");
		int xx  = sc.nextInt();
		while(true) {
			
			if(xx>v) {
				System.out.println("猜大了!");
			}
			if(xx<v) {
				System.out.println("猜小了");
			}
			xx = sc.nextInt();
			if(v==xx) {
				System.out.println("恭喜你猜対了!!");
			}
			
			if(v==xx)
				break;
		}
		return xx;
	}
		public static void main(String[] args) {
		A a = new A();
		a.Guess();
	}
}

本文地址:https://blog.csdn.net/qq_45874107/article/details/112816409

相关标签: 笔记