虎为百兽尊,罔敢触其怒。 惟有父子情,一步一回顾。
习题1
有 1 、 2 、 3 、 4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
实现思路: 显然,这个题目需要用到循环,并且是循环嵌套,先列出所有可能的组合,再去掉重复的组合即可。 代码如下:
1#include <stdio.h> 2 3int main(){ 4 int i, j, k, n = 0; 5 for(i = 1; i < 5; i++){ 6 for(j = 1; j < 5; j++){ 7 for(k = 1; k < 5; k++){ 8 if(i != j && i != k && j != k){ 9 n++; 10 printf("%d%d%d", i, j, k); 11 if(n % 5){ 12 printf(" "); 13 } 14 else{ 15 printf("\n"); 16 } 17 } 18 } 19 } 20 } 21 printf("\n\nThere are %d numbers.\n", n); 22 23 return 0; 24}
打印:
1123 124 132 134 142 2143 213 214 231 234 3241 243 312 314 321 4324 341 342 412 413 5421 423 431 432 6 7There are 24 numbers. 8
习题2
企业发放的奖金根据利润提成。利润 (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 ,求应发放奖金总数?
实现思路: 该题需要用到if条件判断或switch语句。
方式一——if语句:
1#include <stdio.h> 2 3int main(){ 4 long profit, bonus; 5 float ratio; 6 printf("Please input the profit:"); 7 scanf("%ld", &profit); 8 if(profit > 0 && profit <= 100000){ 9 bonus = profit * 0.1; 10 } 11 else if(profit > 100000 && profit < 200000){ 12 bonus = 100000 * 0.1 + (profit - 100000) * 0.075; 13 } 14 else if(profit >= 200000 && profit < 400000){ 15 bonus = 100000 * 0.1 + 100000 * 0.075 + (profit - 200000) * 0.05; 16 } 17 else if(profit >= 400000 && profit < 600000){ 18 bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (profit - 400000) * 0.03; 19 } 20 else if(profit >= 600000 && profit < 1000000){ 21 bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (profit - 600000) * 0.015; 22 } 23 else{ 24 bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + 400000 * 0.015 + (profit - 1000000) * 0.01; 25 }; 26 printf("The bonus is %ld", bonus); 27 28 return 0; 29}
打印:
1Please input the profit:1234567 2The bonus is 41845
方式二——switch语句:
1#include <stdio.h> 2 3int main(){ 4 long profit, bonus = 0; 5 printf("Please input the profit:"); 6 scanf("%ld", &profit); 7 int pr = profit / 100000; 8 if(pr > 10){ 9 pr = 10; 10 } 11 switch(pr){ 12 case 0: 13 bonus = profit * 0.1;break; 14 case 1: 15 bonus = 100000 * 0.1 + (profit - 100000) * 0.075;break; 16 case 2: 17 case 3: 18 bonus = 100000 * 0.1 + 100000 * 0.075 + (profit - 200000) * 0.05;break; 19 case 4: 20 case 5: 21 bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (profit - 400000) * 0.03; 22 case 6: 23 case 7: 24 case 8: 25 case 9: 26 bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (profit - 600000) * 0.015;break; 27 case 10: 28 bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + 400000 * 0.015 + (profit - 1000000) * 0.01;break; 29 default: 30 printf("Input Error!!\n");break; 31 32 } 33 printf("The bonus is %ld", bonus); 34 35 return 0; 36}
效果与方式一相同。
习题3
一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少?
实现思路: 方式一——使用简单循环:
1#include <stdio.h> 2#include <math.h> 3 4int main(){ 5 int i; 6 for(i = 0; i <= 100000; i++){ 7 int root1 = sqrt(i + 100), root2 = sqrt(i + 268); 8 if(pow(root1, 2) == (i + 100) && pow(root2, 2) == (i + 268)){ 9 printf("%8d", i); 10 } 11 } 12 printf("\n"); 13 14 return 0; 15}
打印:
1 21 261 1581 2
方式二: 假设该数为 x。
- 则:x + 100 = n^2^, x + 100 + 168 = m^2^;
- 计算等式:m^2^ - n^2^ = (m + n)(m - n) = 168;
- 设m + n = i,m - n = j,i * j =168,i 和 j 至少一个是偶数;
- 可得m = (i + j) / 2, n = (i - j) / 2,因为m、n为整数,所以i 和 j 要么都是偶数、要么都是奇数;
- 从 3 和 4 推导可知道,i 与 j 均是大于等于 2 的偶数;
- 由于 i * j = 168, j>=2,则 1 < i < 168 / 2 + 1;
- 接下来将 i 的所有数字循环计算即可。
1#include <stdio.h> 2 3int main (void) 4{ 5 int i, j, n, x; 6 for (i = 1; i < 168 / 2 + 1; i++){ 7 if (168 % i == 0){ 8 j = 168 / i; 9 if ( i > j && (i + j) % 2 == 0 && (i - j) % 2 == 0){ 10 n = (i - j) / 2; 11 x = n * n - 100; 12 printf ("%8d", x); 13 } 14 } 15 } 16 printf("\n"); 17 18 return 0; 19}
打印:
1 -99 21 261 1581 2
显然,此时比方式一多了一个数-99,只要在方式一中i的初始值减小为-100即可。
习题4
输入某年某月某日,判断这一天是这一年的第几天?
实现思路: 假设月份为n,则天数为前n-1个月的天数加第n月的天数,如果n大于3时,要考虑是否为闰年,如果为闰年,则2月还要多加1天。
1#include <stdio.h> 2 3int main (void) 4{ 5 int year, month, day, days, leap = 0; 6 printf("Please input the date(YYYY_MM-DD):"); 7 scanf("%d-%d-%d", &year, &month, &day); 8 switch(month){ 9 case 1: 10 days = 0;break; 11 case 2: 12 days = 31;break; 13 case 3: 14 days = 59;break; 15 case 4: 16 days = 90;break; 17 case 5: 18 days = 120;break; 19 case 6: 20 days = 151;break; 21 case 7: 22 days = 181;break; 23 case 8: 24 days = 212;break; 25 case 9: 26 days = 243;break; 27 case 10: 28 days = 273;break; 29 case 11: 30 days = 304;break; 31 case 12: 32 days = 334;break; 33 default: 34 printf("Input Error");break; 35 } 36 days += day; 37 if(year % 4 ==0 && year % 100 != 0 || year % 400 == 0){ 38 leap = 1; 39 } 40 if(leap && month > 2){ 41 days += 1; 42 } 43 printf("Days = %d\n", days); 44 45 return 0; 46}
打印:
1Please input the date(YYYY_MM-DD):2020-05-26 2Days = 147 3
习题5
输入三个整数 x、y、z ,请把这三个数由小到大输出。
实现思路: 通过两两比较找出三者中最大和最小的数。
1#include <stdio.h> 2 3int main (void) 4{ 5 int x, y, z, temp; 6 printf("Please input 3 numbers:\n"); 7 scanf("%d %d %d", &x, &y, &z); 8 if(x > y){ 9 temp = x; 10 x = y; 11 y = temp; 12 } 13 if(x > z){ 14 temp = x; 15 x = z; 16 z = temp; 17 } 18 if(y > z){ 19 temp = y; 20 y = z; 21 z = temp; 22 } 23 printf("Small to big: %d %d %d", x, y, z); 24 25 return 0; 26}
打印:
1Please input 3 numbers: 212 34 23 3Small to big: 12 23 34
本文原文首发来自博客专栏C语言实战,由本人转发至https://www.helloworld.net/p/562F3xc47iVD,其他平台均属侵权,可点击https://blog.csdn.net/CUFEECR/article/details/106360844查看原文,也可点击https://blog.csdn.net/CUFEECR浏览更多优质原创内容。
