首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >曾经做过的40道程序设计课后习题总结(二)

曾经做过的40道程序设计课后习题总结(二)

作者头像
闵开慧
发布2018-04-02 13:00:57
7470
发布2018-04-02 13:00:57
举报
文章被收录于专栏:闵开慧闵开慧

曾经做过的40道程序设计课后习题总结(二)

课后习题目录

1 斐波那契数列 2 判断素数 3 水仙花数 4 分解质因数 5 杨辉三角 6 学习成绩查询 7 求最大公约数与最小公倍数 8 完全平方数 9 统计字母、空格、数字和其它字符个数 10 求主对角线之和 11 完数求解 12 求s=a+aa+aaa+aaaa+aa...a的值 13 高度计算 14 乘法口诀 15 无重复三位数 16 菱形打印 17 利润计算 18 第几天判断 19 从小到大输出数列 20 猴子吃桃问题 21 乒乓球比赛 22 求分数之和 23 求阶乘的和 24 递归求法 25 求不多于5的正整数 26 回文判断 27 星期判断 28 插数入数组 29 取整数的任意位 30 按顺序输出数列 31 位置替换 32 字符串排序 33 贷款器 34 通讯录排序 35 闰年判断 36 二元方程求解 37 密码解译 38 DVD查询 39 电子日历 40 万年历

11 完数求解

11.1 题目:一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3.编程 找出1000以内的所有完数。 11.2 源程序

public class WanShu {
 
    public static void main(String[] args) {
 
       System.out.println("1到1000的完数有: ");
       for (int i = 1; i < 1000; i++) {
           int t = 0;
           for (int j = 1; j <= i / 2; j++) {
              if (i % j == 0) {
                  t = t + j;
              }
           }
           if (t == i) {
              System.out.print(i + " ");
           }
       }
    }
}

11.3 运行结果:

1到1000的完数有: 
6 28 496

12 求s=a+aa+aaa+aaaa+aa...a的值

12.1题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。 12.2 源程序

import java.util.Scanner;
 
public class DuoXun {
    static long a = 2, b = 0;
 
    public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       int n = s.nextInt();
       int i = 0;
       long sum = 0;
       while (i < n) {
           b = b + a;
           sum = sum + b;
           a = a * 10;
           ++i;
       }
       System.out.println("input number: " + n);
       System.out.println(sum);
    }
}

12.3 运行结果:

60
input number: 60
2334310206858307444

12.4 源程序揭秘

    定义一个变量b, 赋初值为0;定义一变量sum, 赋初值为0,进入循环后,将a + b 的值赋给b,将sum + b 的值赋给sum;同时,将a 增加十倍, ++ i; 继续循环;循环结束后,输出sum 的值。

13 高度计算

13.1题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下, 求它在 第10次落地时,共经过多少米?第10次反弹多高? 13.2 源程序

public class HighComput {
    static double height = 100;
    static double distance = 100;
 
    public static void main(String[] args) {
       for (int i = 1; i < 10; i++) {
           distance = distance + height;
           height = height / 2;
       }
 
       System.out.println("路程:" + distance);
       System.out.println("高度:" + height / 2);
    }
}

13.3 运行结果:

路程:299.609375
高度:0.09765625

14 乘法口诀

14.1 题目:输出9*9口诀。 14.2 源程序

public class KouJue {
    
    public static void main(String[] args) {
       for (int i = 1; i < 10; i++) {
           for (int j = 1; j <= i; j++) {
              System.out.print(j + "*" + i + "=" + j * i + " ");
           }
           System.out.println();
       }
    }
}

14.3 运行结果:

1*1=1 
1*2=2 2*2=4 
1*3=3 2*3=6 3*3=9 
1*4=4 2*4=8 3*4=12 4*4=16 
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

14.4 源程序揭秘

    分行与列考虑,共9行9列,i控制行,j控制列。

15 无重复三位数

15.1 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 15.2 源程序

public class NoChFu {
    
    public static void main(String[] args) {
       int count = 0;
       for (int x = 1; x < 5; x++) {
           for (int y = 1; y < 5; y++) {
              for (int z = 1; z < 5; z++) {
                  if (x != y && y != z && x != z) {
                     count++;
                     System.out.print(x * 100 + y * 10 + z + "   ");
                     if (count % 4 == 0) {
                         System.out.println();
                     }
                  }
              }
           }
       }
       System.out.println("共有" + count + "个三位数");
    }
}

15.3 运行结果:

123   124   132   134   
142   143   213   214   
231   234   241   243   
312   314   321   324   
341   342   412   413   
421   423   431   432   
共有24个三位数

15.4 源程序揭秘

    可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。

16 菱形打印

16.1题目:打印出如下图案(菱形)    *    ***   *****   *******   *****    ***    *

16.2 源程序

public class LingXing {
    
    static final int HEIGHT = 7;
    static final int WIDTH = 8;
 
    public static void main(String[] args) {
       for (int i = 0; i < (HEIGHT + 1) / 2; i++) {
           for (int j = 1; j < WIDTH / 2 - i; j++) {
               System.out.print(" ");
           }
           for (int k = 1; k < (i + 1) * 2; k++) {
              System.out.print('*');
           }
           System.out.println();
       }
 
       for (int i = 1; i <= HEIGHT / 2; i++) {
           for (int j = 1; j <= i; j++) {
              System.out.print(" ");
           }
           for (int k = 1; k <= WIDTH - 2 * i - 1; k++) {
              System.out.print('*');
           }
           System.out.println();
       }
    }
}

16.3 运行结果:

   *
  ***
 *****
*******
 *****
  ***
   *

16.4 源程序揭秘

    先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。

17 利润计算

17.1 题目:企业发放的奖金根据利润提成。利润(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,求应发放奖金总数? 17.2 源程序

import java.text.DecimalFormat;
import java.util.*;
 
public class LiRun {
    static double profit = 0;
    static double award = 0;
 
    public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       profit = s.nextInt();
       System.out.println("输入的利润是" + profit + "万");
       if (profit > 0 && profit <= 10) {
           award = profit * 0.1;
       } else if (profit > 10 && profit <= 20) {
           award = 10 * 0.1 + (profit - 10) * 0.075;
       } else if (profit > 20 && profit <= 40) {
           award = 10 * 0.1 + 10 * 0.075 + (profit - 20) * 0.05;
       } else if (profit > 40 && profit <= 60) {
           award = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (profit - 40) * 0.03;
       } else if (profit > 60 && profit <= 100) {
           award = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (profit - 60) * 0.015;
       } else if (profit > 100) {
           award = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (profit - 100) * 0.01;
       }
       DecimalFormat df = new DecimalFormat("#0.00000");
 
       System.out.println("应该提取的奖金是 " + df.format(award) + "万");
    }
}

17.3 运行结果:

78
输入的利润是78.0万
应该提取的奖金是 5.37000万

17.4 源程序揭秘

用数轴来分界,定位。注意定义时需把奖金定义成长整型。 注意: 要精确到小数点后多少位,用 DecimalFormat df = new DecimalFormat("#0.0000");

18 第几天判断

18.1题目:输入某年某月某日,判断这一天是这一年的第几天?

18.2 源程序

import java.util.Scanner;
import java.io.*;
 
public class TianShu {
    public static void main(String[] args) {
       int year, month, day;
       int days = 0;
       int d = 0;
 
       TianShu fymd = new TianShu();
 
       System.out.print("Input the year:");
       year = fymd.input();
       System.out.print("Input the month:");
       month = fymd.input();
       System.out.print("Input The Day:");
       day = fymd.input();
 
       if (year < 0 || month < 0 || month > 12 || day < 0 || day > 31) {
           System.out.println("Input error, please run this program again!");
           System.exit(0);
       }
       for (int i = 1; i < month; i++) {
           switch (i) {
           case 1:
           case 3:
           case 5:
           case 7:
           case 8:
           case 10:
           case 12:
              days = 31;
              // d += days;
              break;
           case 4:
           case 6:
           case 9:
           case 11:
              days = 30;
              // d += days;
              break;
           case 2:
              if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
                  days = 29;
              } else {
                  days = 28;
              }
              // d += days;
              break;
           }
 
           d += days;
 
       }
       System.out.println(year + ":" + month + ":" + day + "是今年的第" + (d + day)
              + "天。");
    }
 
    public int input() {
       int value = 0;
       Scanner s = new Scanner(System.in);
       value = s.nextInt();
       return value;
    }
}

18.3 运行结果:

Input the year:2011
Input the month:4
Input The Day:26
2011:4:26是今年的第116天。

18.4 源程序揭秘

    以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

19 从小到大输出数列

19.1题目:输入三个整数x,y,z,请把这三个数由小到大输出。

19.2 源程序

import java.util.*;
 
public class SmallToBig {
    public static void main(String[] args) {
       SmallToBig fnc = new SmallToBig();
       int a, b, c;
 
       System.out.println("Input 3 numbers:");
       a = fnc.input();
       b = fnc.input();
       c = fnc.input();
 
       if (a > b) {
           int t = a;
           a = b;
           b = t;
       }
 
       if (a > c) {
           int t = a;
           a = c;
           c = t;
       }
 
       if (b > c) {
           int t = b;
           b = c;
           c = t;
       }
       System.out.println(a + " " + b + " " + c);
    }
 
    public int input() {
       int value = 0;
       Scanner s = new Scanner(System.in);
       value = s.nextInt();
       return value;
    }
 
    public void compare(int x, int y) {// 此方法没用
       if (x > y) {
           int t = x;
           x = y;
           y = t;
       }
    }
}

19.3 运行结果:

Input 3 numbers:
100
20
40
20 40 100

19.4 源程序揭秘

    我们想办法把最小的数放到x上,先将x与y进行比较,如果x> y则将x与y的值进行交换,然后再用x与z进行比较,如果x> z则将x与z的值进行交换,这样能使x最小。

20 猴子吃桃问题

20.1 题目:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。 20.2 源程序

public class Monkey {
    
    public static void main(String[] args) {
       int lastdayNum = 1;
       for (int i = 2; i <= 10; i++) {
           lastdayNum = (lastdayNum + 1) * 2;
       }
       System.out.println("猴子第一天摘了 " + lastdayNum + " 个桃子");
    }
}

20.3 运行结果:

猴子第一天摘了 1534 个桃子

20.4 源程序揭秘

    采取逆向思维的方法,从后往前推断。

    由于博客字数限制,40道程序设计课后习题总结将分4篇帖子进行总结,后面3篇帖子后面会一一贴出,或者可以在自己的博客下载已经总结完的全文 http://my.oschina.net/mkh/blog/340689,里面有全面详细的总结。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档