洛谷题单 101【入门2】分支结构

P1046 陶陶摘苹果

在这里插入图片描述

100 200 150 140 129 134 167 198 200 111
110
#include <iostream>
using namespace std;
int main()
{
    int a[10], count = 0, h;
    for (int i = 0; i < 10; i++)
    {
        cin >> a[i];
    }
    cin >> h;
    for (int i = 0; i < 10; i++)
    {
        if (a[i] <= h + 30)
        {
            count++;
        }
    }
    cout << count;
    return 0;
}

P1055 ISBN号码

在这里插入图片描述

0-670-82162-4
0-670-82162-0
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    char a[14], mod[12] = "0123456789X";
    scanf("%s", a);
    int i, j = 1, t = 0;
    for (i = 0; i < 12; i++)
    {
        if (a[i] == '-')
        {
            continue;
        }
        t += (a[i] - 48) * j++;
    }
    if (mod[t % 11] == a[12])
    {
        cout << "Right";
    }
    else
    {
        a[12] = mod[t % 11];
        printf("%s", a);
    }
    return 0;
}

P1085 不高兴的津津

在这里插入图片描述

5 3
6 2
7 2
5 3
5 4
0 4
0 6

if中两个条件:

  • ① 当前总时间比之前找到的最大总时间大
  • ② 总时间>8并且当前总时间比之前找到的最大总时间大
#include <iostream>
using namespace std;
int main()
{
    int a, b, day = 0, max = 0;
    for (int i = 1; i <= 7; i++)
    {
        cin >> a >> b;
        if (a + b > max && a + b > 8)
        {
            max = a + b;
            day = i;
        }
    }
    cout << day;
    return 0;
}

P1422 小玉家的电费

在这里插入图片描述

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int a;
    float b;
    scanf("%d", &a);
    if (a <= 150)
    {
        b = a * 0.4463;
    }
    else if (a >= 151 && a <= 400)
    {
        b = 150 * 0.4463;
        b += (a - 150) * 0.4663;
    }
    else
    {
        b = 150 * 0.4463 + (400 - 150) * 0.4663;
        b += (a - 400) * 0.5663;
    }
    printf("%.1f", b);
    return 0;
}

P1424 小鱼的航程(改进版)

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
    int n, k, s = 0; //周n开始游,过了k天,游了s公里
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= k; i++) //要游k天,所以用循环
    {
        if (n != 6 && n != 7)
        {
            s += 250; //如果不是周末则加250
        }
        if (n == 7)
        {
            n = 1; //如果是周7,那么赋值为1
        }
        else
        {
            n++; //否则n+1
        }
    }
    printf("%d", s); //输出游了多少公里
    return 0;
}

P1888 三角函数

在这里插入图片描述
题解:
正弦值=直角边/斜边
最小正弦值=最短直角边/斜边

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    long long a, b, c;
    cin >> a >> b >> c;
    if (a == 6 && b == 8 && c == 10) //注意:约分
    {
        cout << "3/5";
        return 0;
    }
    if (a > b)
    {
        swap(a, b);
    }
    if (a > c)
    {
        swap(a, c);
    }
    if (b > c)
    {
        swap(b, c);
    }
    cout << a << "/" << c;
    return 0;
}

P1909 买铅笔

在这里插入图片描述

57
2 2
50 30
30 27
9998
128 233
128 2333
128 666
9999
101 1111
1 9999
1111 9999
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int j, k, n, m, w, ans;
    scanf("%d", &n);
    for (int i = 0; i < 3; i++)
    {
        scanf("%d%d", &j, &k);
        m = j;
        w = k;        //输入并存下初始的价格与数量
        while (j < n) //价格与数量不断*2直到数量大于n
        {
            j <<= 1;
            k <<= 1;
        }
        while (j > n) //*2有可能导致买太多了,减去一些
        {
            j -= m;
            k -= w;
        }
        while (j < n) //减去之后又可能太少了,加上一些
        {
            j += m;
            k += w;
        }
        //其实就是大幅度地上调,然后做一些微调
        if (k < ans || ans == 0)
            ans = k; //判断是否是最小花费
    }
    printf("%d\n", ans);
    return 0;
}

P4414 [COCI2006-2007#2] ABC

在这里插入图片描述

1 5 3
ABC
6 4 2
CAB
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a[3];
    char A, B, C;
    cin >> a[0] >> a[1] >> a[2];
    cin >> A >> B >> C;
    sort(a, a + 3);                                               //懒懒_(:з」∠)_排序法
    cout << a[A - 'A'] << " " << a[B - 'A'] << " " << a[C - 'A']; //字母是大写,减去‘A’后得到0(A),1(B),2(C)。
    return 0;
}

P5710 【深基3.例2】数的性质

在这里插入图片描述

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int x;
    bool a, b;
    scanf("%d", &x);
    a = !(x & 1), b = (x > 4 && x <= 12); //a满足性质1,b满足性质2
    printf("%d %d %d %d", a & b, a | b, ((a && !b) || (b && !a)), !a && !b);
    return 0;
}

P5711 【深基3.例3】闰年判断

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    cout << ((n % 4 == 0 && n % 100 != 0) || (n % 400 == 0)) ? 1 : 0;
    return 0;
}

P5712 【深基3.例4】Apples

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    cout << "Today, I ate " << n << " apple";
    if (n > 1)
    {
        cout << "s.\n"; //判断apple是否加s
    }
    else
    {
        cout << ".\n";
    }
    return 0;
}

P5713 【深基3.例5】洛谷团队系统

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
    int a;
    cin >> a;
    if (a * 5 <= a * 3 + 11) //判断谁大谁小
    {
        cout << "Local" << endl;
    }
    else
    {
        cout << "Luogu" << endl;
    }
    return 0;
}

P5714 【深基3.例7】肥胖问题

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
    double m, h, bmi;
    cin >> m >> h;
    bmi = m / (h * h); 
    if (bmi < 18.5)
    {
        cout << "Underweight" << endl;
    }
    if (bmi >= 18.5 && bmi < 24)
    {
        cout << "Normal";
    }
    if (bmi >= 24)
    {
        cout << bmi << endl
             << "Overweight" << endl;
    }
    return 0;
}

P5715 【深基3.例8】三位数排序

在这里插入图片描述
更多方法可点击本处
题解:快排,代码如下:

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int s[3];
    cin >> s[0] >> s[1] >> s[2];
    sort(s, s + 3); //表示排序p数组的[0到2]位置,注意sort默认从小到大排
    cout << s[0] << ' ' << s[1] << ' ' << s[2];
    return 0;
}

P5716 【深基3.例9】月份天数

在这里插入图片描述

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int year, month;                                                 //定义年和月
    int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //定义月份对应天数数组
    cin >> year >> month;                                            //输入年和月
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    {
        a[2] = 29; //判断闰年
    }
    cout << a[month]; //直接输出
    return 0;
}

P5717 【深基3.习8】三角形分类

在这里插入图片描述

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    int d[4] = {0, a, b, c};
    sort(d + 1, d + 4);
    if (d[1] + d[2] <= d[3])
    {
        printf("Not triangle\n");
        return 0;
    }
    if (d[1] * d[1] + d[2] * d[2] == d[3] * d[3])
    {
        printf("Right triangle\n");
    }
    else if (d[1] * d[1] + d[2] * d[2] > d[3] * d[3])
    {
        printf("Acute triangle\n");
    }
    else if (d[1] * d[1] + d[2] * d[2] < d[3] * d[3])
    {
        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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值