C语言基础习题50例(一)1

编程入门 行业动态 更新时间:2024-10-08 20:33:38

C语言基础<a href=https://www.elefans.com/category/jswz/34/1769768.html style=习题50例(一)1"/>

C语言基础习题50例(一)1

文章目录

    • 习题1
    • 习题2
    • 习题3
    • 习题4
    • 习题5

虎为百兽尊,罔敢触其怒。
惟有父子情,一步一回顾。

大明风华 朱棣因虎诗感动流泪

习题1

有 1 、 2 、 3 、 4 个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

实现思路:
显然,这个题目需要用到循环,并且是循环嵌套,先列出所有可能的组合,再去掉重复的组合即可。
代码如下:

#include <stdio.h>int main(){int i, j, k, n = 0;for(i = 1; i < 5; i++){for(j = 1; j < 5; j++){for(k = 1; k < 5; k++){if(i != j && i != k && j != k){n++;printf("%d%d%d", i, j, k);if(n % 5){printf(" ");}else{printf("\n");}}}}}printf("\n\nThere are %d numbers.\n", n);return 0;
} 

打印:

123 124 132 134 142
143 213 214 231 234
241 243 312 314 321
324 341 342 412 413
421 423 431 432There are 24 numbers.

习题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语句:

#include <stdio.h>int main(){long profit, bonus;float ratio;printf("Please input the profit:");scanf("%ld", &profit);if(profit > 0 && profit <= 100000){bonus = profit * 0.1;}else if(profit > 100000 && profit < 200000){bonus = 100000 * 0.1 + (profit - 100000) * 0.075;}else if(profit >= 200000 && profit < 400000){bonus = 100000 * 0.1 + 100000 * 0.075 + (profit - 200000) * 0.05;}else if(profit >= 400000 && profit < 600000){bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (profit - 400000) * 0.03;}else if(profit >= 600000 && profit < 1000000){bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (profit - 600000) * 0.015;}else{bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + 400000 * 0.015 + (profit - 1000000) * 0.01;};printf("The bonus is %ld", bonus);return 0;
} 

打印:

Please input the profit:1234567
The bonus is 41845

方式二——switch语句:

#include <stdio.h>int main(){long profit, bonus = 0;printf("Please input the profit:");scanf("%ld", &profit);int pr = profit / 100000;if(pr > 10){pr = 10;}switch(pr){case 0:bonus = profit * 0.1;break;case 1:bonus = 100000 * 0.1 + (profit - 100000) * 0.075;break;case 2:case 3:bonus = 100000 * 0.1 + 100000 * 0.075 + (profit - 200000) * 0.05;break;case 4:case 5:bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (profit - 400000) * 0.03;case 6:case 7:case 8:case 9:bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (profit - 600000) * 0.015;break;case 10:bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + 400000 * 0.015 + (profit - 1000000) * 0.01;break;default:printf("Input Error!!\n");break;}printf("The bonus is %ld", bonus);return 0;
} 

效果与方式一相同。

习题3

一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少?

实现思路:
方式一——使用简单循环:

#include <stdio.h>
#include <math.h>int main(){int i;for(i = 0; i <= 100000; i++){int root1 = sqrt(i + 100), root2 = sqrt(i + 268);if(pow(root1, 2) == (i + 100) && pow(root2, 2) == (i + 268)){printf("%8d", i);}}printf("\n");return 0;
} 

打印:

      21     261    1581

方式二:
假设该数为 x。

  1. 则:x + 100 = n2, x + 100 + 168 = m2
  2. 计算等式:m2 - n2 = (m + n)(m - n) = 168;
  3. 设m + n = i,m - n = j,i * j =168,i 和 j 至少一个是偶数;
  4. 可得m = (i + j) / 2, n = (i - j) / 2,因为m、n为整数,所以i 和 j 要么都是偶数、要么都是奇数;
  5. 从 3 和 4 推导可知道,i 与 j 均是大于等于 2 的偶数;
  6. 由于 i * j = 168, j>=2,则 1 < i < 168 / 2 + 1;
  7. 接下来将 i 的所有数字循环计算即可。
#include <stdio.h>int main (void)
{int  i, j, n, x;for (i = 1; i < 168 / 2 + 1; i++){if (168 % i == 0){j = 168 / i;if ( i > j && (i + j) % 2 == 0 && (i - j) % 2 == 0){n = (i - j) / 2;x = n * n - 100;printf ("%8d", x);}}}printf("\n");return 0;
}

打印:

     -99      21     261    1581

显然,此时比方式一多了一个数-99,只要在方式一中i的初始值减小为-100即可。

习题4

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

实现思路:
假设月份为n,则天数为前n-1个月的天数加第n月的天数,如果n大于3时,要考虑是否为闰年,如果为闰年,则2月还要多加1天。

#include <stdio.h>int main (void)
{int year, month, day, days, leap = 0;printf("Please input the date(YYYY_MM-DD):");scanf("%d-%d-%d", &year, &month, &day);switch(month){case 1:days = 0;break;case 2:days = 31;break;case 3:days = 59;break;case 4:days = 90;break;case 5:days = 120;break;case 6:days = 151;break;case 7:days = 181;break;case 8:days = 212;break;case 9:days = 243;break;case 10:days = 273;break;case 11:days = 304;break;case 12:days = 334;break;default:printf("Input Error");break;   	}days += day;if(year % 4 ==0 && year % 100 != 0 || year % 400 == 0){leap = 1;}if(leap && month > 2){days += 1;}printf("Days = %d\n", days);return 0;
}

打印:

Please input the date(YYYY_MM-DD):2020-05-26
Days = 147

习题5

输入三个整数 x、y、z ,请把这三个数由小到大输出。

实现思路:
通过两两比较找出三者中最大和最小的数。

#include <stdio.h>int main (void)
{int x, y, z, temp;printf("Please input 3 numbers:\n");scanf("%d %d %d", &x, &y, &z);if(x > y){temp = x;x = y;y = temp;}if(x > z){temp = x;x = z;z = temp;}if(y > z){temp = y;y = z;z = temp;}printf("Small to big: %d %d %d", x, y, z);return 0;
}

打印:

Please input 3 numbers:
12 34 23
Small to big: 12 23 34

更多推荐

C语言基础习题50例(一)1

本文发布于:2024-03-04 11:00:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1709087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:习题   语言   基础

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!