一、简单问题:
1. 编程求下列式子的值,
y=1-1/2+1/3-1/4+…+1/99-1/100
并将结果输出,格式为: 1-1/2+1/3-1/4+…+1/99-1/100 = 表达式的值
public class Porg {
public static void main(String[] args) {
double sum=0;
for(double i=1;i<=100;i++)
{
sum=sum+Math.pow(-1, i-1)*(1/i);
System.out.print(" 1-1/2+1/3-1/4+…+1/99-1/100="+sum);
}
}
}
2. 请编程实现:由键盘输入的任意一组字符,统计其中大写字母的个数 m 和小写字母的个数 n,并输出 m、
n 中的较大者。
import java.util.Scanner;
public class Prog2 {
public static void main(String[] args) {
int m=0,n=0;
Scanner cin=new Scanner(System.in);
String str=cin.nextLine();
for(int i=0;i'A'&&str.charAt(i)<='Z')
m++;
n++;
else
}
if(m>n)
{
}
else
System.out.println(m);
System.out.println(n);
}
}
3. 编程,求全部水仙花数。所谓水仙花数是指一个三位数,其各位数字立方的和等于该数。如:153 = 13
+ 53+ 33。
public class Prog3 {
22--1
public static void main(String[] args) {
int a,b,c;
for(a=1;a<=9;a++)
{
for(b=0;b<=9;b++)
{
for(c=0;c<=9;c++)
{
if(a*100+b*10+c==a*a*a+b*b*b+c*c*c)
System.out.println(a*100+b*10+c);
}
}
}
}
4. 请编制程序判断输入的正整数是否既是 5 又是 7 的整倍数。若是,则输出 yes;否则输出 no。
import java.util.Scanner;
public class Prog4 {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int i=cin.nextInt();
if( i%5==0&&i%7==0)
System.out.print("yes");
else
System.out.print("no");
}
}
5. 请编程实现:对于给定的一个百分制成绩,输出相应的五分制成绩。设:90 分以上为‘A’,80—89 分
为‘B’,70—79 分为‘C’,60—69 分为‘D’,60 分以下为‘E’ 。
import java.util.Scanner;
public class Prog5 {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int m=cin.nextInt();
if(m>=60&&m<=69)
System.out.print("D");
else
if(m>=70&&m<=79)
System.out.print("C");
else
if(m>=80&&m<=89)
System.out.print("B");
else
if(m>=90)
22--2
else
System.out.print("A");
System.out.print("E");
}
}
6. 输入一行字符,将其中的字母变成其后续的第 3 个字母,输出。例:a→d,x → a;y → b;编程实
现。
import java.util.Scanner;
public class Prog6 {
public static void main(String[] args) {
char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','
r','s','t','u','v','w','x','y','z','a','b','c'};
char[] zimu=new
Scanner cin=new Scanner(System.in);
String str=cin.nextLine();
for(int j=0;j
System.out.print("Thuesday");
System.out.print("Friday");
if(m==4)
else
if(m==5)
else
if(m==6)
else
System.out.print("Saturday");
System.out.print("Sunday");
}
}
8. 输入三角形的三边 a, b, c,判断能否构成三角形. 若能,计算面积.
import java.util.Scanner;
public class Prog8 {
public static void main(String[] args) {
System.out.print("请输入三个整数\n");
Scanner cin=new Scanner(System.in);
int a=cin.nextInt();
int b=cin.nextInt();
int c=cin.nextInt();
if(a+b
{
System.out.println(-c/b);
}
else if(b*b-4*a*c>=0)
{
double x1=(-b+Math.pow(1/2, b*b-4*a*c))/(2*a);
double x2=(-b-Math.pow(1/2, b*b-4*a*c))/(2*a);
System.out.println(x1);
System.out.print(x2);
}
else System.out.print("无解");
}
}
10.计算出前 20 项 fibonacci 数列, 要求一行打印 5 个数.
一般而言,兔子在出生两个月后,就有繁殖能力,一对兔子每个月能生出一对小兔子来。如果所有
兔都不死,那么一年以后可以繁殖多少对兔子?
我们不妨拿新出生的一对小兔子分析一下:
第一个月小兔子没有繁殖能力,所以还是一对;
两个月后,生下一对小兔总数共有两对;
三个月以后,老兔子又生下一对,因为小兔子还没有繁殖能力,所以一共是三对;
……
依次类推可以列出下表:
经过月数 0
幼仔对数 0
成兔对数 1
总体对数 1
1
1
1
2
2
1
2
3
3
2
3
5
4
3
5
8
import java.util.Scanner;
5
5
8
6
8
7
8
9
10
11
12
13
21
34
55
89
144
13
21
34
55
89
144
233
13
21
34
55
89
144
233
377
public class Prog10 {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
int a=0,b=1,c=1;
if(n==1){System.out.println(a );
22--5
System.out.println(b );
System.out.println(c );}
else
{
for(int i=2;i<=n;i++)
{
a=b;
}
b=c;
c=a+b;
}
}
System.out.println("幼崽数"+a+"成年兔子书"+ b+"总数"+ c);
}
11.输出 100~10000 之间个位数为 3 的所有素数。
public class Prog11 {
public static void main(String[] args) {
for(int i=103;i<10000;i=i+10)
{
boolean flag=true;
for(int j=2;j
if(5*a+3*b+1/3*c==100&&a+b+c==100)
m++;
}
}
System.out.print("一百元买一百只鸡有"+m+"种买法");
}
}
13. 请编制程序要求输入整数 a 和 b,若 a2+b2 大于 100,则输出 a2+b2 百位以上的数字,否则输出两数之和。
import java.util.Scanner;
public class Prog13 {
public static void main(String[] args) {
System.out.print("请输入两个整数\n");
Scanner cin=new Scanner(System.in);
int a=cin.nextInt();
int b=cin.nextInt();
if(a*a+b*b>100)
{
System.out.print(a*a+b*b);
}
else
System.out.print(a+b);
}
}
14. 编程实现:对键盘输入的任意一个四位正整数,计算各位数字平方和。
如:2345 ,则:计算 22+32+42+52
import java.util.Scanner;
public class Prog14 {
public static void main(String[] args) {
System.out.print("请输入任意一个四位正整数\n");
int sum=0;
Scanner cin=new Scanner(System.in);
int a=cin.nextInt();
sum=(a/1000*a/1000)+((a/100)%10*(a/100)%10)+((a/10%100%10)*(a/10%100%10))
+((a%10)*(a%10));
System.out.print(sum);
}
22--7
}
15. 有 1020 个西瓜,第一天卖一半多两个,以后每天卖剩下的一半多两个,问几天以后能卖完,请编程.
public class Prog15 {
public static void main(String[] args) {
int m=0,sum=1020;
do{
sum=sum/2-2;
m++;
}while(sum>=0);
System.out.print(m+"天以后能卖完");
}
}
16. 编程,输出 200 以内所有完全平方数 C(满足 C2=A2+B2)及其个数.
public class Prog16 {
public static void main(String[] args) {
int m=0;
for(int C=1;C<200;C++)
{
for(int A=1;A<=200;A++)
{
for(int B=1;B<=200;B++)
{
if(A*A+B*B==C*C)
{System.out.println(C);
A=201;
B=201;
m++;}
}
}
22--8