
题目:
有 1、2、3、4 四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
思路:
通过三重循环生成所有可能的三位数组合,每个位置(i、j、k)都可以取 1 到 4 之间的整数。使用条件语句检查每个组合中的三个数字是否互不相同。
对于每个符合条件的组合,将其打印出来。
代码:
#include <stdio.h>
int main() {
int i,j,k;
printf("能组成的无重复数字的三位数有:\n");
for(i=1;i<5;i++)
{
for(j=1;j<5;j++)
{
for(k=1;k<5;k++)
{
if(i!=k&&i!=j&&j!=k)
{
printf("%d,%d,%d\n",i,j,k);
}
}
}
}
printf("结束");
return 0;
}题目:企业发放的奖金根据利润提成。
利润(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,求应发放奖金总数?
答案:
请输入当月利润(单位:万元):12
应发放奖金总额为:1.15万元
思路:
这段问题的核心目标是根据企业当月的利润计算应发放的奖金总数。奖金的计算规则是分段累进的,即不同的利润区间对应不同的提成比例。代码通过一系列的条件判断来确定利润所在的区间,并应用相应的提成规则来计算奖金。
利润区间 | 提成率 |
|---|---|
≤10万元 | 10% |
10万-20万元(超过部分) | 7.5% |
20万-40万元(超过部分) | 5% |
40万-60万元(超过部分) | 3% |
60万-100万元(超过部分) | 1.5% |
| 1% |
代码:
#include <stdio.h>
int main() {
double profit, bonus = 0;
printf("请输入当月利润(万元):");
scanf("%lf", &profit);
if (profit <= 10) {
bonus = profit * 0.1;
} else if (profit <= 20) {
bonus = 10 * 0.1 + (profit - 10) * 0.075;
} else if (profit <= 40) {
bonus = 10 * 0.1 + 10 * 0.075 + (profit - 20) * 0.05;
} else if (profit <= 60) {
bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (profit - 40) * 0.03;
} else if (profit <= 100) {
bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (profit - 60) * 0.015;
} else {
bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (profit - 100) * 0.01;
}
printf("应发放的奖金总数为:%.2f万元\n", bonus);
return 0;
}奖金=I×10%
奖金=10×10%+(I−10)×7.5%
奖金=10×10%+10×7.5%+(I−20)×5%
奖金=10×10%+10×7.5%+20×5%+(I−40)×3%
奖金=10×10%+10×7.5%+20×5%+20×3%+(I−60)×1.5%
奖金=10×10%+10×7.5%+20×5%+20×3%+40×1.5%+(I−100)×1%
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
思路:
通过题目整理出两个式子
式子一: x + 100 = n^2 ,
式子二: ( x + 100 ) + 168 = m^2
根据式子和题目要求,在进行循环判断是否满足要求。
代码:
#include <stdio.h>
int main() {
for (int n = 0; n <= 1000; n++)
{ // 遍历可能的n值,从0到1000
int x = n * n - 100; // 计算x的值
if (x < 0)
{
continue; // 跳过负数解
}
// 判断x + 268是否为完全平方数
for (int m = 0; m * m <= x + 268; m++)
{
if (m * m == x + 268)
{
printf("%d + 100 = %d * %d\n", x, n, n);
printf("%d + 268 = %d * %d\n", x, m, m);
break; // 找到解后退出循环
}
}
}
return 0;
}题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
思路: 首先我们可以判断一下是不是润年。月份我们可以保存在一个数组里面,然后我们需要检查一下我们输入的年份,月份,日期是否合法,然后再开始计算现在的日期是多少。
代码:
#include <stdio.h>
int isLeapYear(int year) {
// 闰年的判断规则:能被4整除且(不能被100整除或能被400整除)
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
}
return 0;
}
int main() {
int year, month, day;
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("Please enter the year, month, and day (format: year month day): ");
//这里的 !=3是判断我们输入的数是否有3个,年,月,日,如果不满足3个的话,那就是格式有问题。
if (scanf("%d %d %d", &year, &month, &day) != 3)
{
printf("Invalid input. Please enter a valid year, month, and day.\n");
return 1;
}
// 检查年份是否合法
if (year <= 0) {
printf("Invalid year. Please enter a positive integer.\n");
return 1;
}
// 检查月份是否合法
if (month < 1 || month > 12) {
printf("Invalid month. Please enter a number between 1 and 12.\n");
return 1;
}
// 检查日期是否合法
if (isLeapYear(year)) {
daysInMonth[1] = 29; // 闰年2月有29天
}
if (day < 1 || day > daysInMonth[month - 1]) {
printf("Invalid day. Please enter a valid day.\n");
return 1;
}
// 计算从1月1日到输入日期的天数
int totalDays = 0;
for (int i = 0; i < month - 1; i++) {
totalDays += daysInMonth[i];
}
totalDays += day; // 加上当前月份的天数
printf("The date %d/%d/%d is the %dth day of the year.\n", day, month, year, totalDays);
return 0;
}题目:输入三个整数 x、y、z,请把这三个数由小到大输出。
思路: 我们可以写一个swap函数,然后使用指针直接修改数值。
代码:
#include <stdio.h>
// 交换两个变量的值
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a, b, c;
printf("please input the number: \n");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
swap(&a, &b);
}
if (a > c) {
swap(&a, &c);
}
if (b > c) {
swap(&b, &c);
}
printf("small to big : %d %d %d\n", a, b, c);
return 0;
}原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。