公告
本題庫將分為兩篇發,第一篇發編程題,第二篇發其他題目,如果發現答案錯誤情況請及時反饋
題庫為同學提供,所以這里不做擔保
第一題
有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子對數為多少?
import java.util.Scanner;
/**
* @author Accepted
* @create 2021/12/19 12:21
* 兔子問題
*/
public class Test01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int months = sc.nextInt();
int x = 1, t, n = 0;
for (int i = 1; i < months; i++) {
t = x;
x = x + n;
n = t;
}
System.out.println("第" + months + "月有" + x + "對兔子");
}
}
第二題
判斷101-200之間有多少個素數,并輸出所有素數,
/**
* @author Accepted
* @create 2021/12/19 12:28
* 素數
*/
public class Test02 {
public static void main(String[] args) {
int count = 0;
for (int i = 101; i < 200; i++) {
if(isPrime(i)) {
System.out.print(i + " ");
count++;
}
}
System.out.println("\n素數的個數為:"+count);
}
public static boolean isPrime(int number) {
for(int j = 2; j <= Math.sqrt(number); j++) {
if(number%j == 0) {
return false;
}
}
return true;
}
}
第三題
列印出所有的“水仙花數”,所謂“水仙花數”是指一個三位數,其各位數字立方和等于該數本身,例如:153是一個“水仙花數”,因為153=13+53+33,
/**
* @author Accepted
* @create 2021/12/19 12:32
* 水仙花數
*/
public class Test03 {
public static void main(String[] args) {
for(int i = 100; i <= 999; i++) {
if(isNarc(i)) {
System.out.println(i);
}
}
}
public static boolean isNarc(int number) {
int t = number;
int sum = 0;
while(t != 0) {
sum += Math.pow(t%10, 3);
t/=10;
}
if(number == sum) {
return true;
}
return false;
}
}
第四題
利用條件運算子的嵌套來完成此題:學習成績>=90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示,
import java.util.Scanner;
/**
* @author Accepted
* @create 2021/12/19 12:36
*/
public class Test04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double score = sc.nextDouble();
if(score >= 90) {
System.out.println("A");
} else if (score >= 60) {
System.out.println("B");
} else {
System.out.println("C");
}
}
}
第五題
求s=a+aa+aaa+aaaa+aa…a的值,其中a是一個數字,例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制,輸出結果的形式如:2+22+222=246,
import java.util.Scanner;
/**
* @author Accepted
* @create 2021/12/19 12:38
*/
public class Test05 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int m = sc.nextInt();
int sum = 0, t = a;
for (int i = 0; i < m; i++) {
sum += t;
System.out.print(t);
if(i != m - 1) {
System.out.print("+");
} else {
System.out.print("=");
}
t *= 10;
t += a;
}
System.out.println(sum);
}
}
第六題
一球從100米高度自由落下,每次落地后反跳回原高度的一半;n次落地經過路線總長度和下次反彈的高度,
import java.util.Scanner;
/**
* @author Accepted
* @create 2021/12/19 12:43
*/
public class Test06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
double n = 100;
int count = 0;
double sum = 100;
for(int i = 0; i < t; i++) {
count ++;
System.out.println(count + "次落地路線總長度" + sum + ", 下次反彈高度" + n/2);
n /= 2;
sum += n*2;
}
}
}
第七題
有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數?都是多少?
/**
* @author Accepted
* @create 2021/12/19 12:50
* 深度優先搜索做法,也可以樸素做法
*/
public class Test07 {
static int ans = 0, n = 4;
final int N = 5;
int[] st = new int[N];
boolean[] bool = new boolean[N];
public static void main(String[] args) {
Test07 t = new Test07();
t.dfs(0);
System.out.println("共有:" + ans + "種可能");
}
public void dfs(int u) {
if(u > n-1) {
for(int i = 1; i <= n-1; i++) {
System.out.print(st[i] + " ");
}
System.out.println();
ans++;
return ;
}
for(int i = 1; i <= n; i++) {
if(!bool[i]) {
st[u] = i;
bool[i] = true;
dfs(u + 1);
st[u] = 0;
bool[i] = false;
}
}
}
}
第八題
企業發放的獎金根據利潤提成,利潤(I)低于或等于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,求應發放獎金總數
import java.util.Scanner;
/**
* @author Accepted
* @create 2021/12/19 13:04
*/
public class Test08 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double i = sc.nextDouble();
double ans = 0;
if (i <= 10) {
ans = 0.1 * i;
} else if (i <= 20) {
ans = 10 * 0.1 + (i - 10) * 0.075;
} else if (i <= 40) {
ans = 10 * 0.1 + 10 * 0.075 + (i - 20) * 0.05;
} else if (i <= 60) {
ans = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (i - 40) * 0.03;
} else if (i <= 100) {
ans = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (i - 60) * 0.015;
} else {
ans = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (i - 100) * 0.01;
}
System.out.println(ans);
}
}
第九題
輸入三個整數x、y、z,請把這三個數由小到大輸出,
import java.util.Scanner;
/**
* @author Accepted
* @create 2021/12/19 13:11
*/
public class Test09 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
if(x > y) {
int temp = x;
x = y;
y = temp;
}
if(x > z) {
int temp = x;
x = z;
z = temp;
}
if(y > z) {
int temp = z;
z = y;
y = temp;
}
System.out.println(x + " " + y + " " + z);
}
}
第十題
輸入十個整數,請把這十個數由小到大輸出,
import java.util.Arrays;
import java.util.Scanner;
/**
* @author Accepted
* @create 2021/12/19 13:17
*/
public class Test10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] num = new int[10];
for (int i = 0; i < num.length; i++) {
num[i] = sc.nextInt();
}
Arrays.sort(num);
System.out.println(Arrays.toString(num));
}
}
第十一題
輸出九九乘法表,
/**
* @author Accepted
* @create 2021/12/19 13:19
*/
public class Test11 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i*j + "\t");
}
System.out.println();
}
}
}
第十二題
猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個第二天早上又將剩下的桃子吃掉一半,又多吃了一個,以后每天早上都吃了前一天剩下的一半零一個,到第10天早上想再吃時,見只剩下一個桃子了,求第一天共摘了多少,
/**
* @author Accepted
* @create 2021/12/19 13:20
*/
public class Test12 {
public static void main(String[] args) {
int ans = 1;
for(int i = 1; i < 10; i++) {
ans = (ans + 1) * 2;
}
System.out.println(ans);
}
}
第十三題
- 列印出如下圖案,
*****
****
***
**
*
/**
* @author Accepted
* @create 2021/12/19 13:23
*/
public class Test13 {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 5; j > i; j--) {
System.out.print("*");
}
System.out.println();
}
}
}
第十四題
寫一個for回圈,按從低到高的順序列印出從1到10的所有數字,
/**
* @author Accepted
* @create 2021/12/19 13:24
*/
public class Test14 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
第十五題
有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲,問第4個人歲數,他說比第3個人大2歲,問第三個人,又說比第2人大兩歲,問第2個人,說比第一個人大兩歲,最后問第一個人,他說是10歲,請問第五個人多大?
/**
* @author Accepted
* @create 2021/12/19 13:27
*/
public class Test15 {
public static void main(String[] args) {
int ans = 10;
for (int i = 1; i < 5; i++) {
ans += 2;
}
System.out.println(ans);
}
}
第十六題
海灘上有一堆桃子,五只猴子來分,第一只猴子把這堆桃子憑據分為五份,多了一個,這只猴子把多的一個扔入海中,拿走了一份,第二只猴子把剩下的桃子又平均分成五份,又多了一個,它同樣把多的一個扔入海中,拿走了一份,第三、第四、第五只猴子都是這樣做的,問海灘上原來最少有多少個桃子?
/**
* @author Accepted
* @create 2021/12/19 13:30
*/
public class Test16 {
public static void main(String[] args) {
int n;
n = fun(0);
System.out.println(n);
}
private static int fun(int num) {
if(num == 5) {
return 1;
} else {
return fun(num + 1)*5 + 1;
}
}
}
第十七題
求0,1,2,3,4,5,6,7所能組成的8位奇數個數,
/**
* @author Accepted
* @create 2021/12/19 13:36
*/
public class Test17 {
public static void main(String[] args) {
int count = 0;
for(int i = 10000000; i <= 77777777; i++) {
String str = Integer.toString(i);
if(fun(str)) {
count++;
}
}
System.out.println(count);
// fun("123456");
}
public static boolean fun(String str) {
//此方法來判斷是否只包含0-7的數字
int[] st = new int[10];
for (int i = 0; i < str.length(); i++) {
st[str.charAt(i) - '0']++;
}
if(st[8] != 0 || st[9] != 0) {
return false;
}
return true;
}
}
第十八題
隨機生成7個數(1—50的整數),每生成一個數,列印出該相應數量的*,
import java.util.Random;
/**
* @author Accepted
* @create 2021/12/19 13:46
*/
public class Test18 {
public static void main(String[] args) {
Random random = new Random();
for(int i = 0; i < 7; i++) {
int t = random.nextInt(50);
for (int j = 0; j < t; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
友情鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/386573.html
標籤:java
