JavaSE编程题03

3.1

/*
数据: a b c delta  r1  r2
步骤:
1.提示用户输入abc三个参数
2.计算delta  delta=b*b-4*a*c
3.判断delta的值
    3.1 delta>0
        输出两个解
    3.2 delta==0
        输出一个解
    3.3 delta<0
        无实数解
*/  
import java.util.*;
class Home03_01
{
    public static void main(String[] args)
    {
        //1.


        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入a,b,c:");
        double a=scanner.nextDouble();   
        double b=scanner.nextDouble();  
        double c=scanner.nextDouble();  
        //2.

        double delta=b*b-4*a*c;
        //3.

        if(delta>0)
        {
            double r1=(-b+Math.sqrt(delta))/(2*a);
            double r2=(-b-Math.sqrt(delta))/(2*a);
            System.out.printf("r1=%.2f,r2=%.2f",r1,r2);
        
        }
        else if(delta==0)
        {
            double r=(-b+Math.sqrt(delta))/(2*a);
            System.out.printf("r=%.2f",r);

        }
        else if(delta<0)
        {
            System.out.println("无实数解");
        }
    }
}

3.2

 

/*
数据:a,b,c,d,e,f
步骤:
1.提示用户输入四个系数 a,b,c,d,  和两个常数 e,f
2.计算判别式delta的值
3.delta就行判断
    3.1如果delta不等于0  则方程组有解
        分别计算两个解并输出
    3.2如果delta等于0    则方程组无解
        输出无解
*/
import java.util.*;
class Home03_02
{
    public static void main(String[] args)
    {
        //1.
        Scanner scanner = new Scanner(System.in);
        System.out.print("请用户输入a,b,c,d,e,f: ");  
        double a=scanner.nextDouble();
		double b=scanner.nextDouble();
		double c=scanner.nextDouble();
		double d=scanner.nextDouble();
		double e=scanner.nextDouble();
		double f=scanner.nextDouble();
        //2.
		double delt=a*d-b*c;
        //3.
		if(delt!=0){
            //3.1
			double x=(e*d-b*f)/delt;
			double y=(a*f-e*c)/delt;
			System.out.println("x is "+x+" , and y is "+y);
		}else{
            //3.2
			System.out.println("has no solution");
		}  
    }
}

 

3.3

/*
数据:今天的周几  未来的天数   未来的周几
      (0+12)%7=5
      (4+4)%7=1
      (今天的周几+未来的天数)%7=未来的周几
      1月1日是周一   1%7=1
      余数为1是周一    余数为6的是周六
      1 2 3 4 5 6 7
      8 9 10 11 12 13 14
      15 16 17 18 19 20
      1月20日       20%7=6
步骤:
1.输入今天是周几
2.输入未来的几天
3.打印未来的几天是周几
*/
import java.util.*;
class Home03_03{
    public static void main(String[] args){
        //1.
        Scanner scanner = new Scanner(System.in);
        System.out.print("今天是周几: ");
        int today=scanner.nextInt();
        //2.
        System.out.print("未来的几天: ");
        int future=scanner.nextInt();
        //3.
        int futureDay=(today+future)%7;

        String todayString=" ";
        String futureDayString=" ";
        
        if(today==0){
            todayString="周日";
        }
        else if(today==1){
            todayString="周一";
        }
        else if(today==2){
            todayString="周二";
        }
        else if(today==3){
            todayString="周三";
        }
        else if(today==4){
            todayString="周四";
        }
        else if(today==5){
            todayString="周五";
        }
        else if(today==6){
            todayString="周六";
        }
        

        if(today==0){
            futureDayString="周日";
        }
        else if(today==1){
            futureDayString="周一";
        }
        else if(today==2){
            futureDayString="周二";
        }
        else if(today==3){
            futureDayString="周三";
        }
        else if(today==4){
            futureDayString="周四";
        }
        else if(today==5){
            futureDayString="周五";
        }
        else if(today==6){
            futureDayString="周六";
        }
        System.out.println("未来的"+future+"是星期"+futureDayString);
    }
}

3.4

/*
12345
12345%10 = 5    12345/10=1234
1234%10 =4      12345/10=123
123%10 =3       123/10=12
12%10 =2        12/10=1
1%10 =1         1/10=0
54321
5*10000+4*1000+3*100+2*10+1
(5*1000+4*100+3*10+2)*10+1
((5*100+4*10+3)*10+2)*10+1
(((5*10+4)*10+3)*10+2)10+1
((((0*10+5)*10+4)*10+3)*10+2)*10+1
sum=0;
sum=sum*10+n;        //5
sum=sum*10+5;        //54
sum=sum*10+4;        //543
sum=sum*10+3;        //5432
sum=sum*10+2;        //54321
sum=sum*10+1;

*/
import java.util.*;
class Home03_04{
    public static void main(String[] args){
        //1.输入一个数字
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个三位数:");
        int num=scanner.nextInt();
        int temp=num;
        //2.拼接处该数字的反序
        int sum = 0;
        sum = sum*10+num%10;
        num/=10;
        sum = sum*10+num%10;
        num/=10;
        sum = sum*10+num%10;
        num/=10;
        if(sum==temp){
            System.out.println("是回文");
        }
        else{
            System.out.println("不是回文");
        }
    }
}

3.5

import java.util.Scanner;
class Home03_05{
    public static void main(String[] args){
        //1.
        Scanner scanner = new Scanner(System.in);
        int a = (int)(Math.random() * 90 + 10);
		System.out.println("本次中奖号码为"+a);
		int r = scanner.nextInt();
        System.out.println("请输入您的彩票数字"+r);
        //2.
		int sw = a / 10;//随机数十位
		int se = a % 10;//随机数各位
		int y = r / 10;//输入数十位
		int u = r % 10;//输入数各位	
		
		if (r < 10 || r >= 100){
			System.out.println("请输入一个两位整数");
		}
		//3.
		else {
			if (r == a){
			System.out.println("奖金10000$");
		}else if(y == se && u == sw) {
			System.out.println("奖金为3000$");
		}else if(y == sw || u == se){
			System.out.println("奖金为1000$");
		}
		}
    }
}

 

3.6

/*
数据:电脑随机产生的一个数字  用户输入的一个数字
    0 1 2
    剪刀  石头  布

步骤:
1.提示用户输入一个数字
2.计算机随机产生一个数字
3.将两个数字进行对比,分输赢
    平局  com==usr
    用户赢  usr=0 com=2|   usr=1  com=0  |  usr=2   com=1
    用户输  剩下的都是用户输
*/
/*
随机数;
    Math.random() [0,1.0]*3  -> [0,3]
    Random
*/
import java.util.*;
class Home03_06{
    public static void main(String[] args){
        //1.
        Scanner scanner = new Scanner(System.in); 
        System.out.println("请输入 剪刀0 石头1 布2");
        int usr=scanner.nextInt();
        //2.
        Random random=new Random();
        int com=random .nextInt(3);

        String usrStr="";
        String comStr="";
        switch(usr){
            case 0:
                usrStr="剪刀";
                break;
            case 1:
                usrStr="石头";
                break;
            case 2:
                usrStr="布";
                break;    
        }
        switch(com){
            case 0:
                comStr="剪刀";
                break;
            case 1:
                comStr="石头";
                break;
            case 2:
                comStr="布";
                break;  
        }
        //3.
        if(usr==com){
            System.out.printf("用户是%s,电脑是%s,平局",usrStr,comStr);

        }
        else if(usr==0&&com==2||usr==1&&com==0||usr==2&&com==1){
            System.out.printf("用户是%s,电脑是%s,用户赢",usrStr,comStr);
        }
        else{
            System.out.printf("用户是%s,电脑是%s,用户输",usrStr,comStr);
        }
    }
}

3.7

/*
数据:
步骤:年 月 日 星期几
1.提示用户输入 年 月 日
2.通过克里斯汀-泽勒计算某天是星期几的算法公式
h=(day+(26*(month+1)/10)+year%100+(year%100/4)+year/100/4+5*year/100)%7;
*/
import java.util.*;
class Home03_07{
    public static void main(String[] args){
        //1.
        Scanner input=new Scanner(System.in);
        System.out.println("请输入年份(e.g 2008):");
        int year=input.nextInt();
        System.out.println("请输入月份(1-12):");
        int month=input.nextInt();
        System.out.println("请输入日(1-31):");
        int day=input.nextInt();
        //2.
        int h=(day+(26*(month+1)/10)+year%100+(year%100/4)+year/100/4+5*year/100)%7;
        //克里斯汀-泽勒计算某天是星期几的算法公式
        //3.
        switch(h){
            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;
        }
    }
}

3.8

/*
数据: 圆的圆心  圆的半径  点的坐标
步骤:
1.提示用户输入点的坐标
2.计算点到圆心之间的距离
3.判断距离和半径之间的关系
    距离 > 半径    点在圆外
    距离 == 半径   点在圆上
    距离 < 半径    点在圆内
*/
import java.util.Scanner;
class Home03_08{
    public static void main(String[] args){
        //1.
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入坐标点:");
        double x=scanner.nextDouble();
        double y=scanner.nextDouble();
        //2.
        double x0=0;
        double y0=0;
        double radius=10;
        double distance=Math.sqrt(Math.pow(x-x0, 2)+Math.pow(y-y0,2));
        //3.
        if(distance>radius){
            System.out.println("点在圆外");
        }
        else if(distance==radius){
            System.out.println("点在圆上");
        }
        else if(distance<radius){
            System.out.println("点在圆内");
        }
    }
}

 

3.9

/*
步骤:
1.提示用户输入 X Y的值
2.通过单if语句进行判断
*/
import java.util.Scanner;
class Home03_09{
    public static void main(String[] args){
        //1.
		Scanner scanner=new Scanner(System.in);
		System.out.print("请输入 x,y:");
		double x=scanner.nextDouble();
        double y=scanner.nextDouble();
        //2.
		if(x>=0&&x<=200){
			if((200-x)/y>=2){
				System.out.println("点在三角形内");
				return;//直接结束函数 谨慎使用
			}
		}
		System.out.println("点在三角形外");
    }
}

3.10

 

import java.util.Scanner;
class Home03_10{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        //1.输入两个矩形的宽高和中心坐标
        System.out.print("请输入r1的坐标:");
        double xo=scanner.nextDouble();
        double yo=scanner.nextDouble();
        double W=scanner.nextDouble();
        double H=scanner.nextDouble();
        System.out.print("请输入r2的坐标:");
        double x=scanner.nextDouble();
        double y=scanner.nextDouble();
        double w=scanner.nextDouble();
        double h=scanner.nextDouble();
        //2.计算是否r2在r1内
        if(x<=(W/2-w/2+xo)&&x>=(xo-W/2+w/2)&&y<=(H/2-h/2+yo)&&y>=(yo-H/2+h/2)){
            System.out.println("r2 in r1");
            //计算是否r2在r1外
        }else if(x>=(W/2+w/2+xo)||x<=(xo-W/2-w/2)||y>=(H/2+h/2+yo)||y<=(yo-H/2-h/2)){
            System.out.println("r2 out r1");
        }else{
            System.out.println("r2 overlaps r1");
        }
        }  
}

3.11

import java.util.Scanner;

class Home03_11{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入若干数字:");
        double sum=0; //总和
        int positives=0;
        int negatives=0;
        while(true){
            int num=scanner.nextInt();
            if(num!=0){
                sum+=num;
                if(num>0){
                    positives++;
                }else{
                    negatives++;
                }
            }else{
                break; //跳出当前循环
            }
        } 
        if(positives+negatives==0){
            System.out.println("没有其他数输入,除了0");
        }else{
        System.out.println("正数的个数"+positives);  
        System.out.println("负数的个数"+negatives);  
        System.out.println("总和"+sum);  
        System.out.println("平均数"+sum/(positives+negatives));
        }
    }
}

3.12

/*
21  25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16~21
*/

import java.util.Scanner;
class Home03_12{
    public static void main(String[] args){
        //1.
        Scanner scanner = new Scanner(System.in);
        //输入两个数字
        System.out.print("请输入两个整数n1和n2: ");
        int n1=scanner.nextInt();
        int n2=scanner.nextInt();
        int gcd=1;
        for(int i=n1<n2?n1:n2;i>=1;i--){
            if(n1%i==0&&n2%i==0){
                gcd=i;
                break;
            }
        }
        System.out.println("最大公约数为"+gcd);

    }
}

 

3.13

/*
120 2~120 -->2
60  2~60  -->2
30  2~30  -->2
15  2~15  -->3
5   2~5   -->5
1
*/
import java.util.Scanner;
class Home03_13{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个整数:");
        int num=scanner.nextInt();
        while(true){
            for(int i=2;i<=num;i++){
                if(num%i==0){
                    System.out.print(i+" ");
                    num=num/i;
                    break;
                }
            }
            if(num==1){
                break;
            }
        }
    }
}

3.14

/*
步骤:
1.提示用户输入一个1-15之间的整数
*/
import java.util.Scanner;
class Home03_14{
    public static void main(String[] args){
    //1.
	Scanner scanner=new Scanner(System.in);
	System.out.print("请用户输入一个1-15之间的整数:");
    int line=scanner.nextInt();
    //2.
	for(int i=1;i<=line;i++){
		for(int k=1;k<=line-i;k++){
			System.out.print("   ");
		}
		for(int j=-i;j<=i;j++){
			if(j!=0&&j!=1){
				System.out.printf("%-3d",Math.abs(j));
			}
		}
			System.out.println();
		}
	}
}

3.15

/*
1.第一行: ?i = 1;  i < 7;  j = 0; ? j <=1; ? ? ?

第二行: ?  i = 2;  i < 7; j = 0; ? j <= 2; ? ? 

第三行: ?  i = 3; ?i < 7; j = 0; ? j <= 3; ? ? 
? ? ? ? ? ? ? ? ? ? ? ?……
列数 j <= i;
*/
class Home03_15{
    public static void main(String[] args){
        for(int i = 1 ; i< 7 ; i++){
            for(int j = 1 ; j<=i ; j++){ //控制列数 
                    System.out.printf("%-3d",Math.abs(j));
                }
                //换行
            System.out.println();
        }
        System.out.println();
        for(int i = 1 ; i<7;  i++){
			for (int j = 1 ; j<(8-i)  ;j++ ){
				System.out.printf("%-3d",Math.abs(j));
			}
			//换行
			System.out.println();
        }
        System.out.println();
        for(int i = 1;i < 7;i++){
			for(int j = 0;j < (7-1-i);j++){	//每一行的空白数
				System.out.print("  ");
			}
			for(int k = 1;k < (i+1);k++){ //每一行个数
				System.out.printf("%-2d",Math.abs(k));
			}
			System.out.println();
        }
        System.out.println();
        for(int i = 1;i < 8; i++){
			for(int j = 0;j < i;j++){	//每一行空白的个数
				System.out.print("  ");
			}
			for(int k = 1;k < (8-i);k++){	//每一行的个数
				System.out.printf("%-2d",Math.abs(k));
			}
			System.out.println();
		}


    }
}

 

3.16

/*

两层for循环,外层控制行数,内层控制列

第一行乘0次2,输出一次

第二行乘以1次2,输出两次

就是先乘以2,然后再除以2

*/
import java.util.Scanner;
class Home03_16{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
		System.out.print("请输入一个整数: ");
		int line=scanner.nextInt();
		for(int i=1;i<=line;i++){
			for(int k=1;k<=line-i;k++){
				System.out.print("    ");
			}
			for(int j=-(i-1);j<=i-1;j++){
				if(j<=0){
					System.out.printf("%-4d",(int)Math.pow(2, j+i-1));
				}else{
					System.out.printf("%-4d",(int)Math.pow(2, -j+i-1));
				}
			}
			System.out.println();
		}
    }
}

3.17

/*
什么是素数   就是除了1和其本身之外   没有其他的数字可以整除的
num
2~num-1  找到一个数字 如果这个数字m num%m==0  num不是素数
如果一个都没有找到的话  这个数字是素数
*/
class Home03_17{
    public static void main(String[] args){
        int count=0;   //当前素数的个数
        boolean flag=true;   //默认这个数字是素数
        for(int num=2;num<=1000;num++){
            for(int m=2;m<=num-1;m++){
                if(num%m==0){
                //说明num不是素数
                    flag=false;
                    break;
            }
        }
        //如果for正常执行完毕   都没有找到   是素数
        if(flag){
            count++;
            System.out.print(num+" ");
            if(count%8==0){  //  8   16    32
                System.out.println();
            }
        }
        flag=true;   //flag 初始值为true
       }
    }
}

 

3.18

 

import java.util.Scanner;

class Home03_18{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int imax=scanner.nextInt();
        double sum=0;
        for(int i=1;i<=imax;i++){
            sum+=Math.pow(-1.0,i+1)/(2*i-1);
        }
        double pi=sum*4;
        System.out.print(pi);
        //Math.pow(-1,10); 累乘  for -1*-1*-1........
    }
}

3.19

import java.util.Scanner;

class Home03_19{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入一个数字: ");
        int imax=scanner.nextInt();
        double e=1;
        double item=1.0;
        for(int i=1;i<imax;i++){
            item=item*i;   //n!=(n-1)*n
            e+=1/item;
        }
        System.out.println(e);
    }
}

3.20

/*
闰年: 能够被4整除 不能被100整除      或者可以被400整除
*/
class Home03_20{
    public static void main(String[] args){
        int count=0;
        for(int i=101;i<=2100;i++){
            if(i%4==0&&i%100!=0 || i%400==0){
                count++;
                System.out.print(i+" ");
                if(count%10==0){
                    System.out.println();
                }
            }
            System.out.println("\n闰年一共"+count+"个");
        }
    }
}

3.21

class Home03_21{
    public static void main(String[] args){
        //6: 1 2 3 4 5 6
        //28: 1 2 4 /14
        //n:  1~n/2
        int sum=0;
        for(int n=2;n<=10000;n++){
        for(int i=1;i<=n/2;i++){
            if(n%i==0){
                sum+=i;
            }
        }
        if(n==sum){
            System.out.println("完全数"+sum);
        }
        sum=0;
    }
    }
}

3.22

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

class Home03_22{
    public static void main(String[] args){
         Scanner scanner = new Scanner(System.in); 
         Random random=new Random();
        int usrWin=0;
        int comWin=0;
        while(true){
         System.out.println("请输入 剪刀0 石头1 布2");
         int usr=scanner.nextInt();
         int com=random .nextInt(3);
 
         String usrStr="";
         String comStr="";
         switch(usr){
             case 0:
                 usrStr="剪刀";
                 break;
             case 1:
                 usrStr="石头";
                 break;
             case 2:
                 usrStr="布";
                 break;    
         }
         switch(com){
             case 0:
                 comStr="剪刀";
                 break;
             case 1:
                 comStr="石头";
                 break;
             case 2:
                 comStr="布";
                 break;  
         }
         //3.
         if(usr==com){
             System.out.printf("用户是%s,电脑是%s,平局",usrStr,comStr);
 
         }
         else if(usr==0&&com==2||usr==1&&com==0||usr==2&&com==1){
             System.out.printf("用户是%s,电脑是%s,用户赢",usrStr,comStr);
            usrWin++;
        }
         else{
             System.out.printf("用户是%s,电脑是%s,用户输",usrStr,comStr);
            comWin++;
        }
            if(usrWin==2||comWin==2){
                break;
            }
    }
    if(usrWin==2){
        System.out.println("最终玩家赢!");
    }else{
        System.out.println("最终电脑赢! ");
    }
}
}

3.23

/*
12/2  6~0
6/2   3~0
3/2   1~1
1/2   0~1
1100
*/
import java.util.Scanner;

class Home03_23{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int num=scanner.nextInt();
        String binStr="";
        while(true){
            binStr=num%2+binStr;   //"1100"
            num/=2;
            if(num==0){
                break;
            }
        }
        System.out.println(binStr);
    }
}

3.24

import java.io.PrintStream;
import java.util.Scanner;

class Home03_24{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入数字:");
        int num=0;
        int max=0;
        int count=0;
        while(true){
            num=scanner.nextInt();
            if(num==0){
                break;
            }else if(num>max){
                max=num;
                count=1;
            }else if(num==max){
                count++;
            }
        }
        System.out.println("max="+max+",count"+count);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值