• 2019-01-06 流程控制练习题


    练习一、

    一个人很倒霉,不小心打碎了一位妇女的一篮子鸡蛋。为了赔偿便询问篮子里有多少鸡蛋。

    那妇女说,她也不清楚,只记得每次拿两个则剩一个,每次拿3个则剩2个,每次拿5个则剩4个,若每个鸡蛋1元

    请你帮忙编程,计算最少应赔多少钱? 要求:用循环语句实现,直接打印出结果不给分

     1 package com.zuoye;
     2 /**
     3  * 一个人很倒霉,不小心打碎了一位妇女的一篮子鸡蛋。为了赔偿便询问篮子里有多少鸡蛋。
     4  * 那妇女说,她也不清楚,只记得每次拿两个则剩一个,每次拿3个则剩2个,每次拿5个则剩4个,若每个鸡蛋1元。
     5  * 请你帮忙编程,计算最少应赔多少钱
     6  * @author Mr.kemi
     7  *2019-1-2
     8  */
     9 public class One {
    10     public static void main(String[] args) {
    11         //设变量i为总的鸡蛋数
    12         for(int i = 0;;i++) {
    13             if(i%2==1&&i%3==2&&i%5==4) {
    14                 System.out.println("最少要赔:"+i+"元钱");
    15                 break;
    16             }
    17         }
    18     }    
    19 }

    练习二:从键盘接收一个整数N,统计出1~N之间能被7整除的整数的个数,以及这些能被7整除的数的和。

     

     1 package com.zuoye;
     2 
     3 import java.util.Scanner;
     4 /**
     5  * 从键盘接收一个整数N,统计出1~N之间能被7整除的整数的个数,以及这些能被7整除的数的和
     6  * @author Mr.kemi
     7  * 2019-1-2
     8  */
     9 public class Two {
    10     public static void main(String[] args) {
    11         Scanner input = new Scanner(System.in);
    12         System.out.println("请输入一个整数:");
    13         int N = input.nextInt();
    14         //设变量 和初始值为0
    15         int sum = 0;
    16         //设变量 个数初始值为0
    17         int count = 0;
    18         for(int i = 1;i<N;i++) {
    19             if(i%7==0) {
    20                 count++;
    21                 sum +=i;
    22             }
    23             
    24         }
    25         System.out.println("1~"+N+"之间能被7整除的个数是:"+count+"他们的和是:"+sum);
    26     }
    27 }

     

     

     练习三:

    有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?

    程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....

     1 package com.zuoye;
     2 
     3 import java.util.Scanner;
     4 /**
     5  * 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子
     6  * 假如兔子都不死,问每个月的兔子对数为多少?
     7  * @author Mr.kemi
     8  *    2019-1-2
     9  */
    10 public class Three {
    11     public static void main(String [] args){
    12         long [] month ;
    13         System.out.print("请输入月份: ");
    14         Scanner s = new Scanner(System.in);
    15         int i = s.nextInt();
    16         month = new long[i];
    17 
    18         if(month.length >= 1){
    19             month[0] = month[1] = 1;//第一和第二个月的兔子都是:1
    20         }
    21         for(int j = 0; j < month.length; j++){
    22             if(j == 0 || j == 1){
    23                 System.out.println("兔子数量为: " + month[j]);
    24             }else {
    25                 month[j] = month[j-2] + month[j-1];//第三个月以后都满足规律:month[j] = month[j-2] + month[j-1]
    26                 System.out.println("兔子数量为: " + month[j]);
    27             }
    28         }
    29     }
    30 
    31 }

    练习四:

    一个笼子有35个头,94只脚,问鸡和兔各有多少?

    解题:数学方法:设鸡i只,兔j只,方程:i + j = 35 ;  2 * i + 4 * j = 94。

     1 package com.zuoye;
     2 /**
     3  * 题:一个笼子有35个头,94只脚,问鸡和兔各有多少?
     4  * @author Mr.kemi
     5  *    2019-1-2
     6  */
     7 public class Four {
     8     public static void main(String[] args) {
     9         //j为鸡  y为兔子
    10         int j,y;
    11         for(j=0;j<=35;j++) {
    12             y = 35-j;//兔子数
    13             if(2 * j + 4 * y ==94) {
    14                 System.out.println("鸡为"+j+"兔为"+y);
    15             }
    16         }
    17         
    18     }
    19 }

    练习5、马克思手稿中有一道趣味数学题:有30个人,其中有男人、女人和小孩,在一家饭馆里吃饭共花了50先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,问男人、女人和小孩各有几人?

     1 package com.zuoye;
     2 
     3 /**
     4  * 马克思手稿中有一道趣味数学题:有30个人,其中有男人、女人和小孩。
     5  * 在一家饭馆里吃饭共花了50先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令。
     6  * 问男人、女人和小孩各有几人?
     7  * @author Mr.kemi
     8  *    2019-1-2
     9  */
    10 
    11 public class Five {
    12     public static void main(String[] args) {
    13         //声明变量 man为男人 woman为女人 child为小孩
    14         int man,woman,child;
    15         for(man =0;man<=10;man++) {
    16             for(woman =0;woman<=15;woman++) {
    17                 for(child =0;child<=30;child++) {
    18                     if(man+woman+child==30&&3*man+2*woman+child==50) {
    19                         System.out.println("男人有"+man+"人"+"女人有"+woman+"人"+"小孩有"+child+"人");
    20 
    21                     }
    22 
    23                 }
    24             }
    25         }
    26     }
    27 }

    练习6、判断101-200之间有多少个素数,并输出所有素数。

    程序分析:

        * 素数是:只能被1或本身整除的数,如:2,3,5,7,11,131... 

     1 package com.zuoye;
     2 /**
     3 *判断101-200之间有多少个素(质)数,并输出所有素数。
     4 *程序分析:素数是:只能被1或本身整除的数,如:2,3,5,7,11,131...
     5  * @author Mr.kemi
     6  *    2019-1-2
     7  */    
     8 public class Six {
     9     public static void main(String[] args) {
    10         //声明101-200的总质数
    11         int count = 0;
    12         //用for循环遍历101到200之间的数
    13         for(int i=101;i<200;i++) {
    14             for(int y=2;y<i;y++) {
    15                 if(i%y==0) {
    16                     break;
    17                 }else if(y==i-1) {
    18                     count++;
    19                     System.out.println(i);
    20                 }
    21             }
    22             
    23         }
    24         System.out.println(count);
    25     }
    26 }

    练习7、

    打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

     1 package com.zuoye;
     2 /**
     3 *打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
     4 *例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
     5  * @author Mr.kemi
     6  *    2019-1-2
     7  */    
     8 public class Seven {
     9     public static void main(String[] args) {
    10         int a,b,c;
    11         for(int i=100;i<=999;i++) {
    12             a =i/100;
    13             b =i/10%10;
    14             c =i%10;
    15             if((a*a*a)+(b*b*b)+(c*c*c)==i) {
    16                 System.out.println("水仙花为:"+i);
    17                 
    18             }
    19         }
    20     }
    21 }

     

     

    练习8、将一个正整数分解质因数。比如:输入90,打印出90=2*3*3*5。

     1 package com.zuoye;
     2 
     3 import java.util.Scanner;
     4 //将一个正整数分解质因数。比如:输入90,打印出90=2*3*3*5。
     5 public class Eight {
     6     public static void main(String[] args) {
     7         Scanner input = new Scanner(System.in);
     8         System.out.println("请输入一个数字");
     9         int num = input.nextInt();
    10         System.out.print(num+"=");
    11         for(int i=2;i<10000;i++) {
    12             while(num%i==0) {
    13                 System.out.print(i);
    14                 num=num/i;
    15                 if(num==1) {
    16                     break;
    17                 }
    18                 System.out.print("*");
    19                 
    20             }
    21         }
    22     }
    23 }

    练习9、利用条件运算符的嵌套来完毕此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分下面的用C表示。

     1 package com.zuoye;
     2 
     3 import java.util.Scanner;
     4 
     5 /**
     6  * 利用条件运算符的嵌套来完毕此题:
     7  * 
     8  * @author Mr.kemi
     9  *    2019-1-2
    10  */
    11 public class Nine {
    12     public static void main(String[] args) {
    13         Scanner input = new Scanner(System.in);
    14         System.out.println("请输入您的成绩:");
    15         double i = input.nextInt();
    16 //        if(i>=90) {
    17 //            System.out.println("成绩优秀-A级");
    18 //        }else if(i<90&&i>59) {
    19 //            System.out.println("成绩中等-B级");
    20 //        }else {
    21 //            System.out.println("成绩差-C级");
    22 //        }
    23         String sum= i<60?"c":(i>89?"A":"B");
    24         System.out.println("您的成绩为"+sum);
    25     }
    26 }

    练习10、输入两个正整数m和n,求其最大公约数和最小公倍数。

     1 package com.zuoye;
     2 
     3 import java.util.Scanner;
     4 
     5 /**
     6  * 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
     7  * 在循环中,仅仅要除数不等于0。用较大数除以较小的数。将小的一个数作为下一轮循环的大数。
     8  * 取得的余数作为下一轮循环的较小的数。如此循环直到较小的数的值为0,返回较大的数,
     9  * 此数即为最大公约数,最小公倍数为两数之积除以最大公约数。
    10  * @author Mr.kemi
    11  *    2019-1-2
    12  */
    13 public class Ten {
    14     public static void main(String[] args) {
    15         Scanner input = new Scanner(System.in);
    16         System.out.println("输入两个正整数");
    17         int m = input.nextInt();
    18         int n = input.nextInt();
    19         int a;
    20         int c=n;
    21         int d=m;
    22         if(m<n) {
    23             a=m;
    24             m=n;
    25             n=a;
    26         }
    27         while(n!=0) {
    28             int b=m%n;
    29             m=n;
    30             n=b;
    31         }
    32         System.out.println(m);
    33         System.out.println(c*d/m);
    34     }
    35 }

  • 相关阅读:
    C#对ListView控件的几个操作技巧
    C#用代码创建控件,以及它的鼠标事件
    C#使用ListView控件对数据进行频繁更新时,出现闪烁问题的解决办法
    C#判断某个键值是否存在于字典中
    FreeMASTER 2.0的安装与使用
    C和C++中获取二维数组的行列数
    Python中类的定义和使用
    Python创建字典和添加键值
    C#用鼠标滚轮控制控件大小,实现滚轮缩放效果
    C#中对Excel文件执行写数据操作
  • 原文地址:https://www.cnblogs.com/kemii/p/10241001.html
Copyright © 2020-2023  润新知