1.2 分支结构 参考代码

P5709 [深基2.习6] Apples Prologue / 苹果和虫子

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int m, t, s, ans;
scanf("%d%d%d", &m, &t, &s);
if (t == 0) ans = 0;
else ans = m - (s + t - 1) / t;
printf("%d\n", max(ans, 0));
return 0;
}

P5710 [深基3.例2] 数的性质

#include <cstdio>
int main()
{
int x;
scanf("%d", &x);
int p1 = x % 2 == 0;
int p2 = x > 4 && x <= 12;
printf("%d %d %d %d\n", p1 && p2, p1 || p2, p1 ^ p2, !p1 and !p2);
return 0;
}

P5711 [深基3.例3] 闰年判断

#include <cstdio>
int main()
{
int year;
scanf("%d", &year);
printf("%d\n", year % 400 == 0 || year % 4 == 0 && year % 100);
return 0;
}

P5712 [深基3.例4] Apples

#include <cstdio>
int main()
{
int x;
scanf("%d", &x);
printf("Today, I ate %d apple%s.\n", x, x > 1 ? "s" : "");
return 0;
}

P5713 [深基3.例5] 洛谷团队系统

#include <cstdio>
int main()
{
int n;
scanf("%d", &n);
printf("%s\n", 5 * n < 3 * n + 11 ? "Local" : "Luogu");
return 0;
}

P5714 [深基3.例7] 肥胖问题

#include <cstdio>
#include <iostream>
using namespace std;
const double EPS = 1e-6;
int main()
{
double w, h;
scanf("%lf%lf", &w, &h);
double bmi = w / h / h;
if (bmi < 18.5 - EPS) printf("Underweight");
else if (bmi > 18.5 - EPS && bmi < 24 + EPS) printf("Normal");
else cout << bmi << "\nOverweight\n";
return 0;
}
  • cout 输出 double 类型默认就是 6 位有效数字

P5715 [深基3.例8] 三位数排序

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int a, b, c;
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("%d %d %d\n", a, b, c);
return 0;
}
  • swap 函数可用于交换两个变量

P1909 [NOIP2016 普及组] 买铅笔

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
int a, b;
scanf("%d%d", &a, &b);
int ans = (n + a - 1) / a * b;
scanf("%d%d", &a, &b);
ans = min(ans, (n + a - 1) / a * b);
scanf("%d%d", &a, &b);
ans = min(ans, (n + a - 1) / a * b);
printf("%d\n", ans);
return 0;
}

P1422 小玉家的电费

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int e;
double ans = 0;
scanf("%d", &e);
ans = min(150, e) * 0.4463 + min(max(e - 150, 0), 250) * 0.4663 + max(e - 400, 0) * 0.5663;
printf("%.1f\n", ans);
return 0;
}

P1424 小鱼的航程(改进版)

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int x, n;
scanf("%d%d", &x, &n);
int ans = min(n, 8 - x);
n -= ans;
ans = max(ans - 2, 0);
ans += n / 7 * 5 + min(n % 7, 5);
printf("%d\n", ans * 250);
return 0;
}

P5717 [深基3.习8] 三角形分类

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (a > b) swap(a, b);
if (b > c) swap(b, c);
if (a > c) swap(a, c);
if (a + b <= c) printf("Not triangle");
else {
if (a * a + b * b == c * c) printf("Right triangle\n");
if (a * a + b * b > c * c) printf("Acute triangle\n");
if (a * a + b * b < c * c) printf("Obtuse triangle\n");
if (a == b || b == c || a == c) printf("Isosceles triangle\n");
if (a == b && b == c) printf("Equilateral triangle\n");
}
return 0;
}
posted @   RonChen  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示