C语言练习小Demo,仅供参考
目录
Demo 1.三数找中值

#include<stdio.h>
int main (){
int a,b,c;
int max,min,second;
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
max=a>b ? a:b;
max=max>c ? max:c;
min=a<b ? a:b;
min=min<c ? min:c;
second=a+b+c-max-min;
printf("%d\n", second);
return 0;
}
Demo 2.整除判断

#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
if (num % 5 == 0 && num % 11 == 0) {
printf("Y\n");
} else {
printf("N\n");
}
return 0;
}
Demo 3.闰年判断

#include <stdio.h>
int main() {
int year;
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("Y\n");
} else {
printf("N\n");
}
return 0;
}
Demo 4.二次方程求实根个数

Demo 5.组成三角形

#include <stdio.h>
int main() {
float a, b, c;
scanf("%f %f %f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) {
printf("Y\n");
} else {
printf("N\n");
}
return 0;
}
Demo 6.该交电费了

#include <stdio.h>
int main() {
double electricity;
double cost = 0;
scanf("%lf", &electricity);
if (electricity <= 50) {
cost = electricity * 0.5;
} else if (electricity <= 100) {
cost = 50 * 0.5 + (electricity - 50) * 0.6;
} else if (electricity <= 200) {
cost = 50 * 0.5 + 50 * 0.6 + (electricity - 100) * 0.8;
} else {
cost = 50 * 0.5 + 50 * 0.6 + 100 * 0.8 + (electricity - 200) * 1.0;
}
printf("%.2f\n", cost);
return 0;
}
Demo 7.百货公司促销

#include <stdio.h>
int main() {
double price, discount, pay;
while (scanf("%lf", &price) != EOF) {
if (price >= 5000) {
discount = 0.8;
} else if (price >= 3000) {
discount = 0.85;
} else if (price >= 2000) {
discount = 0.9;
} else if (price >= 1000) {
discount = 0.95;
} else {
discount = 1.0;
}
pay = price * discount;
printf("discount=%.2f,pay=%.2f\n", discount, pay);
}
return 0;
}
Demo 8.简单的计算器

#include <stdio.h>
int main() {
int a, b, result;
char op;
// 读取输入的表达式,格式为"a op b"
scanf("%d%c%d", &a, &op, &b);
// 根据运算符进行相应计算
switch(op) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
result = 0;
break;
}
printf("%d%c%d=%d\n", a, op, b, result);
return 0;
}
Demo 9.小吴等电梯

#include <stdio.h>
int main() {
long long n;
scanf("%lld", &n);
long long batches = (n + 12) / 12;
long long time;
if (batches == 0) {
time = 2;
} else {
time = (batches - 1) * 4 + 2;
}
printf("%lld\n", time);
return 0;
}
Demo 10.卡特兹函数

#include <stdio.h>
int main() {
int n, k;
scanf("%d", &n);
if (n % 2 == 1) {
k = 3 * n + 1;
} else {
k = n / 2;
}
printf("%d", k);
return 0;
}
2150

被折叠的 条评论
为什么被折叠?



