第三章练习题整理

3.7

在这里插入图片描述

/* 
数据:年year 月m 日q 星期几h 世纪数j 世纪的第几年k 
指令:输入 计算 switch判断 输出
步骤:
1.提示用户输入 年 月 日
2.对特殊的1月和2月做处理,将月份+12,年份-1
3.根据公式计算
4.使用switch判断星期几
5.输出结果
*/
 
 
import java.util.Scanner;
 
class Demo03_07{
    public static void main(String[] args){
        //1.
        Scanner input = new Scanner(System.in);   
        System.out.print("输入年:");
        int year = input.nextInt();
        System.out.print("输入月:");
        int m = input.nextInt();
        System.out.print("输入日:");
        int q = input.nextInt();
        //2.
        if(m==1||m==2){
            m+=12;
            year--;
        }
        //3.
        int j = year / 100;
        int k = year % 100;
        int h = (q + 26 * (m + 1) / 10 + k + k / 4 + j / 4 + 5 * j) % 7;
        //4.
        String weekStr = "";
        switch(h)
        {
            case 0:
                weekStr = "是星期六";
                break;
            case 1:
                weekStr = "是星期天";
                break;
            case 2:
                weekStr = "是星期一";
                break;
            case 3:
                weekStr = "是星期二";
                break;
            case 4:
                weekStr = "是星期三";
                break;
            case 5:
                weekStr = "是星期四";
                break;
            case 6:
                weekStr = "是星期五";
                break;
        }
        //5.
        System.out.print(weekStr);
    }
}

3.9

在这里插入图片描述

/* 
数据:x坐标 y坐标 关系y=(-100/200)*x+100
指令:输入 比对 输出
步骤:
1.提示用户输入一个点
2.判断点的位置 两个条件 0<x<200 0<y<100
	2.1大致判断点在x坐标的范围
	2.2精确判断点是否在y坐标的上界内
3.输出结果
*/
 
 
import java.util.Scanner;
 
class Demo03_09{
    public static void main(String[] args){
        //1.
        System.out.print("输入一x坐标和y坐标:");
        Scanner input = new Scanner(System.in);
        double x = input.nextDouble();
        double y = input.nextDouble();
        double f = (-100.0 / 200.0) * x + 100;
        //2.1
        if(0 <= x && x <= 200){
        	//2.2
            if(0 <= y && y <= f){
                System.out.print("点在三角形内");
            }
            else{
                System.out.print("点在三角形外");
            }
        }
    }
}

3.10

在这里插入图片描述

在这里插入图片描述

/* 
数据:矩形中点x坐标 y坐标 宽度 高度
指令:
步骤:
1.提示用户输入两个矩形中点x坐标和y坐标以及他们的宽度和高度
2.如果 Math.abs(x2-x1) < (width1+width2)/2
    且 Math.abs(y2-y1) < (height1+height2)/2
    说明第二个在第一个内或者和第一个重合
        在这个基础上
        如果 Math.abs(x2-x1) > (width1-width2)/2
          且 Math.abs(y2-y1) > (height1-height2)/2
        第二个就与第一个重叠
*/
 
 
import java.util.Scanner;
 
class Demo03_10{
    public static void main(String[] args){
        //1.
        Scanner input = new Scanner(System.in);
        System.out.print("第一个矩形的x坐标y坐标宽度和高度:");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double width1 = input.nextDouble();
        double height1 = input.nextDouble();
        System.out.print("第二个矩形的x坐标y坐标宽度和高度:");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        double width2 = input.nextDouble();
        double height2 = input.nextDouble();
        //2.
        if(Math.abs(x2-x1) < (width1+width2)/2 && Math.abs(y2-y1) < (height1+height2)/2){
            if(Math.abs(x2-x1) > (width1-width2)/2 && Math.abs(y2-y1) > (height1-height2)/2){
                System.out.print("第二个矩形和第一个重叠");
            }
            else{
                System.out.print("第二个矩形在第一个矩形内");
            }
        }
        else{
            System.out.print("第二个矩形在第一个矩形外");
        }
    }
}

3.11

在这里插入图片描述

/* 
数据:读入的数 读入的个数 和 正数的个数 负数的个数
指令:
步骤:
1. 提示用户输入几个数并以0为止
2. 以读到0为止的循环
    2.1 读到的数大于0 正数个数加一
    2.2 读到的数小于0 负数个数加一
    2.3 求读到数的和
3. 输出结果
*/
 
 
import java.util.Scanner;
 
class Demo03_11{
    public static void main(String[] args){
        //1.
        Scanner input = new Scanner(System.in);
        System.out.print("输入几个数,以0为结束:");
        int i = 0;
        int j = 0; //计数器 正数的个数i 负数的个数j
        double sum = 0; //求和用 
        //2.
        while(true)
        {
        	int a = input.nextInt();
            if(a > 0){
                ++i;        //2.1
            }else if(a < 0){
                ++j;        //2.2
            }else {
            	break;
            }
            sum = sum + a;  //2.3
        }
        //3.
        if(i == 0 && j == 0){
            System.out.print("还没有输入数字");
        }
        else{
            System.out.print("正数有" + i + "个,负数有" + j + "个,和为" + sum + "平均值为" +sum/(i+j));
        }
    }
}

3.12

在这里插入图片描述

/* 
数据:n1 n2 循环初始化k gcd
指令:输入 循环 分支 输出
步骤:
1.提示用户输入两个整数
2.求最大公约数
    循环:
        1.初始化 k=max/2
        2.继续条件 k>=1
        3.循环体 判断是否是公约数
            由于是从大到小查找,找到一个公约数就break
        4.周期 k--
3.输出
*/
 
 
import java.util.Scanner;
 
class Demo03_12{
    public static void main(String[] args){
        //1.
        Scanner input = new Scanner(System.in);
        System.out.print("输入两个整数:");
        int n1 = input.nextInt();
        int n2 = input.nextInt();
        int max = n1 > n2 ? n1 : n2; 
        int gcd = 1;
        //2.
        for(int k = max / 2;k >= 1;k--){
            if(n1%k==0 && n2%k==0){
                gcd = k;
                break;
            }
        }
        //3.
        System.out.print("最大公约数为:" + gcd);
 
    }
}

3.13

在这里插入图片描述

/* 
数据:输入的数n 因子i
指令:输入 循环 分支 输出
步骤:
1.提示用户输入一个整数
2.n能被i整除时i就是一个因子
3.输出
循环终止条件是 n=1
*/
 
 
import java.util.Scanner;
 
class Demo03_13{
    public static void main(String[] args){
        //1.
        System.out.print("请输入一个整数:");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        while (true) {
            for (int i = 2; 2 <= n; i++) {
                //2.
                if (n % i == 0) {
                    n = n / i;
                    System.out.print(i + " "); //3.
                    break; //保证一直从2 开始判断
                }
            }
            if (n == 1) {
                break;
            }
        }
    }
}

3.14

在这里插入图片描述
在这里插入图片描述
一行打印的个数为 的个数+0+i的个数
所以i应减一

/* 
分析循环输出n行
分成两部分输出
第一部分是倒三角      第二部分            i   j(空格)   k
********                1               1   2*(5-1)  1
******                2 1 2             2   2*(5-2)  -2 1 2
****                3 2 1 2 3           3   2*(5-3)  -3 -2 1 2 3
**                4 3 2 1 2 3 4         4   2*(5-4)  -4 -3 -2 1 2 3
                5 4 3 2 1 2 3 4 5       5   2*(5-5)  -5 -4 -3 -2 1 2 3 4 5
                                        n   2*(n-i)  -i -i+1 -i+2 …… 1 2 …… i-1 i
*/
 
 
import java.util.Scanner;
 
class Demo03_14{
    public static void main(String[] args){
 
        Scanner input = new Scanner(System.in);
        System.out.print("输入一个1~15的整数:");
        int n = input.nextInt();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n-i;j++){
                if(n<10){
                    System.out.print("  ");
                }else{
                    System.out.print("   ");
                }
            }
            for(int k=-(i-1);k<=i-1;k++){
                
                if(n<10){
                    System.out.printf("%-2d",Math.abs(k)+1);
                }else{
                    System.out.printf("%-3d",Math.abs(k)+1);
                }
                
            }
            System.out.println();
        }
    }
}

3.15

在这里插入图片描述

 
 
class Demo03_15{
    public static void main(String[] args){
        for(int i=1;i<=6;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j + " ");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
        for(int i=6;i>=1;i--){
            for(int j=1;j<=i;j++){
                System.out.print(j + " ");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
        for(int i=1;i<=6;i++){
            for(int j=1;j<=6-i;j++){
                System.out.print("  ");
            }
            for(int k=-i;k<=i;k++){
                if(k<0){
                    System.out.print(" "+Math.abs(k));
                }
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
        for(int i=6;i>=1;i--){
            for(int j=1;j<=6-i;j++){
                System.out.print("  ");
            }
            for(int j=1;j<=i;j++){
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

3.16

在这里插入图片描述

/*
第4行  
         1  2  4 8 4 2 1
         0  1  2 3 2 1 0
      x -3 -2 -1 0 1 2 3
第5行
        1 2 4 8 16 8 4 2 1
        0 1 2 3 4  3 2 1 0
    x  -4-3-2-1 0  1 2 3 4
*/
class Demo03_16{
    public static void main(String[] args){
        for(int i=1;i<=8;i++){
            for(int k=1;k<=8-i;k++){
                System.out.print("    ");
            }
            for(int x=-(i-1);x<=i-1;x++){
                System.out.printf("%4d",(int)Math.pow(2,i-1-Math.abs(x)));
            }
            System.out.println();
        }
    }
}
/*
对于打印一些对称性的三角形图案或者是类似的问题
尽量向绝对值考虑
*/

3.17

在这里插入图片描述

/*
素数:除了1和它本身没有其他的数字可以整除这个数字。
2~num-1 找到一个数字m,这个数字m满足num%m==0则这个num不是素数
否则是一个素数
*/


class Demo03_17{
    public static void main(String[] args){
        final int NUMBER_OF_PRIMES_PER_LINE = 8;
        int count = 0;
        int number = 2;

        System.out.print("The first 50 prime numbers are \n");

        while (number <= 1000) {
            boolean isPrime = true;
            for (int divisor = 2; divisor <= number / 2; divisor++) {
                if (number % divisor ==0) {
                    isPrime = false;
                    break;
                }
            }
            //for是break出来的 ->不是素数
            //foe是正常执行完毕的 ->是素数
            if (isPrime) {
                count++;
                if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
                    System.out.println(" " + number);
                }
                else 
                    System.out.printf("%3d ",number);
            }
        
            number++;
        
        }
    }
}

3.18

在这里插入图片描述

/* 
数据:pi
指令:循环计算 输出
步骤:
1.循环四:
        1.初始化 1
        2.循环继续条件 次数小于10000000
        3.循环体 pi = pi + Math.pow(-1, i+1) / (2 * i - 1);
        4.循环间距 1
2.输出
*/

class Demo03_18{
    public static void main(String[] args){
        double pi = 0;
        double flag = 1;
        //1.
        for (int i = 1; i <= 10000000; i++) {
            pi = pi + flag / (2 * i - 1);
            //pi = pi + Math.pow(-1,i+1) / (2 * i - 1);
            flag = -flag;
                //Math.pow(-1,10)底层是累乘的
        }
        //2.
        System.out.print(4 * pi);

    }
}

3.19

在这里插入图片描述

/* 
数据:e 阶乘值
指令:嵌套循环 分支判断 输出
步骤:
e=1+i个(1/i的阶乘)
1.循环 i个值相加
2.循环 计算i的阶乘
3.输出
*/



class Demo03_19{
    public static void main(String[] args){
        double e = 1;
        double item = 1;
        //1.
        for (int i = 1; i <= 100000; i++) {
            
            //2.
            item = item * i;
            e = e + 1/item;
            //3.
            if (i % 10000 == 0) {
               System.out.println(i + " " +e);
            }

        }
    }
}

3.20

在这里插入图片描述

class Demo03_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) {
            //普通闰年:公历年份是4的倍数的,且不是100的倍数,为闰年。(如2004年就是闰年);
			//世纪闰年:公历年份是整百数的,必须是400的倍数才是世纪闰年(如1900年不是世纪闰年,2000年是世纪闰年);
                System.out.printf("%5d",i);
                count++;
                if (count % 10 == 0) {
                    System.out.println();
                }
            }  
        }
    }
}

3.21

在这里插入图片描述

class Demo03_21{
    public static void main(String[] args){
        for (int i = 1; i <= 1000; i++) {
            int sum = 0;
            for (int j = 1; j <= i/2; j++) {
                if (i % j ==0) {
                    sum = sum + j;
                    if (i == sum) {
                        System.out.printf("%-4d",i);
                    }
                }
            }
        }
    }
}

3.22

在这里插入图片描述


import java.util.Random;
import java.util.Scanner;
 
class Demo03_22{
    public static void main(String[] args){
        //1.
        Scanner input = new Scanner(System.in);
        Random random = new Random();
        int usrwin = 0; 
        int comwin = 0;
        while (true){
            System.out.print("随机输入一个数 剪刀 0 石头 1 布 2 :");
            //2.
            int usr = input.nextInt();
            int com = random.nextInt(3);
            //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;
            }
            //4.
            if (usr == com){
                System.out.printf("平局 计算机是%s,你是%s。\n",comStr,usrStr);
            }
            else if((com+1) % 3 == usr){
                System.out.printf("你赢了 计算机是%s,你是%s。\n",comStr,usrStr);
                usrwin++;
            }
            else{
                System.out.printf("你输了 计算机是%s,你是%s。\n",comStr,usrStr);
                comwin++;
            }
            if (usrwin == 2) {
                System.out.print("用户赢");
                break;
            }
            else if (comwin == 2) {
                System.out.print("计算机赢");
                break;
            }
        }
        
        
    }
}

3.23

在这里插入图片描述

/* 
数据:输入的十进制 输出的二进制字符串 
指令:输入 转换二进制 输出
步骤:
14 % 2~7 … 0
7  % 2~3 … 1
3  % 2~1 … 1
1  % 1~0 … 1
1110
*/

import java.util.Scanner;

class Demo03_01{
    public static void main(String[] args){
        System.out.print("输入一个十进制整数:");
        Scanner input = new Scanner(System.in);
        int decimal = input.nextInt();
        String binaryStr = "";
        while(true){
            binaryStr=decimal % 2 + binaryStr;//1110
            decimal/=2;
            if(decimal==0){
                break;
            }
        }
        System.out.print(binaryStr);
    }
} 

3.24

在这里插入图片描述

import java.util.Scanner;
class Demo03_24{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int num=0;
        int max=0;
        int count=0;
        System.out.print("请输入数字:");
        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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值