洛谷题单题解记录-【入门2】分支结构

【入门2】分支结构

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

#include <stdio.h>
int main() 
{
    int x;
    scanf("%d", &x);
    if (x % 2 == 0 && x <= 12 && x > 4)
        printf("1 ");
    else printf("0 ");
    if (x % 2 == 0 || x <= 12 && x > 4)
        printf("1 ");
    else printf("0 ");
    if (x % 2 == 0 && x <= 4 && x > 12 || x > 4 && x <= 12 && x % 2 == 1)
        printf("1 ");
    else printf("0 ");
    if (x % 2 != 0 &&( x > 12 && x <= 4))
        printf("1 ");
    else printf("0 ");
    return 0;
}

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

#include <stdio.h>
int main() 
{
    int year;
    scanf("%d", &year);
    if (year % 400 == 0 || year % 4 == 0 && year % 100!=0)
        printf("1");
    else printf("0");
    return 0;
}

P5712 【深基3.例4】Apples

#include <stdio.h>
int main()
{
    int x;
    scanf("%d", &x);
    if (x == 1 || x == 0)
        printf("Today, I ate %d apple.", x);
    else printf("Today, I ate %d apples.", x);
    return 0;
}

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

#include <stdio.h>
int main()
{
    int x;
    scanf("%d", &x);
    if ((x * 5) < (x * 3 + 11))
        printf("Local");
    else printf("Luogu");
    return 0;
}

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

#include <stdio.h>
int main()
{
    float m, h,bmi;
    scanf("%f%f", &m, &h);
    bmi = m / (h * h);
    if (bmi < 18.5)
        printf("Underweight");
    else if (bmi >= 18.5 && bmi < 24)
        printf("Normal");
    else printf("%.4f\nOverweight", bmi);
    return 0;
}

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

#include <stdio.h>
int main()
{
    int a[3],i,j,t;
    for (i = 0; i < 3; i++)
        scanf("%d", &a[i]);
    for (i = 0; i < 3 - 1; i++)
        for (j = 0; j < 3 - i - 1; j++)
        {
            if (a[j] > a[j + 1])
            {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
        }
    for (i = 0; i < 3; i++)
        printf("%d ", a[i]);
    return 0;
}

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

#include <stdio.h>
int main()
{
    int year, m;
    scanf("%d%d", &year, &m);
    if (m == 1 || m == 3 || m == 5 || m == 7 | m == 8 || m == 10 || m == 12)
        printf("31");
    else if (m == 4 || m == 6 || m == 9 || m == 11)
        printf("30");
    else
    {
        if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
            printf("29");
        else
            printf("28");
    }
    return 0;
}

P1085 不高兴的津津

#include "stdio.h"
int main()
{
    int a[7], b[7], c=0, i, max=0;
    for (i = 0; i < 7; i++)
    {
        scanf("%d%d", &a[i], &b[i]);
        if (a[i] + b[i] > 8 && a[i]+b[i]>max)
        {
            max = a[i] + b[i];
            c = i+1;
        }
    }
    printf("%d", c);
    return 0;
}

P1909 买铅笔

#include<stdio.h>
int main() {
    int n;
    scanf("%d", &n);
    int i;
    int min = 0;
    int a[3];
    int b[3];
    for (i = 0; i < 3; i++) {
        scanf("%d%d", &a[i], &b[i]);
        int N = 0;
        int sum = 0;
        while (1) {
            N += a[i];
            sum += b[i];
            if (N >= n) {
                break;
            }
        }
        if (i == 0)    min = sum;
        if (min > sum)    min = sum;
    }
    printf("%d", min);
    return 0;
}

P1055 ISBN号码

//方法1
#include <stdio.h>
int main()
{
    char a[14];
    int q=0, w=0;
    for (int i = 0; i < 14; i++)
        scanf("%c", &a[i]);
    q = (a[0]-'0') * 1 + (a[2] - '0') * 2 + (a[3] - '0') * 3 + (a[4] - '0') * 4 + (a[6] - '0') * 5 + (a[7] - '0') * 6 + (a[8] - '0') * 7 + (a[9] - '0') * 8 + (a[10] - '0') * 9;
    q = q % 11;
    if (q == 10)
        w = 'X';
    else
        w = q + '0';
    if (w == a[12]) 
        printf("Right");
    else
    {
        a[12] = w;
        for (int i = 0; i < 14; i++)
            printf("%c", a[i]);
    }
    return 0;
}
//方法2
#include<stdio.h>
int main() {

    int a, b, c, m, n1=0, n2=0, s, i = 4, j = 9,a1,b1,c1;
    char d;
    scanf("%d-%d-%d-%c", &a, &b, &c, &d);
    a1 = a; b1 = b; c1 = c;
    s = a;
    while (b) {
        m = b % 10;
        n1 += m * i;
        b = b / 10;
        i--;
    }
    while (c) {
        m = c % 10;
        n2 += m * j;
        c = c / 10;
        j--;
    }
    s = (s + n1 + n2) % 11;
    if (s+'0' == d ||s==10&&d=='X') {
        printf("Right");
    }
    else if (s == 10) {
        printf("%d-%d-%d-X", a1, b1, c1);
    }
    else{
        printf("%d-%d-%d-%d", a1, b1, c1, s);
    }
    return 0;
}

P1422 小玉家的电费

#include "stdio.h"
int main()
{
    int a;
    float b=0;
    scanf("%d", &a);
    if (a <= 150)
    {
        b = a * 0.4463;
    }
    if (a >= 151 && a <= 400)
    {
        b = 0.4463 * 150 + (a - 150) * 0.4663;
    }
    if(a>401)
    {
        b = 0.4463 * 150 + 250 * 0.4663 + (a - 400) * 0.5663;
    }
    printf("%.1f", b);
    return 0;
}

P1424 小鱼的航程(改进版)

#include <stdio.h>
int main()
{
    int x, n=0,i,s=0;
    scanf("%d%d", &x, &n);
    for (i = x; i < x + n; i++)
    {
        if (i % 6 == 0 || i % 7 == 0)
            continue;
        else
            s = s + 250;
    }
    printf("%d", s);
    return 0;
}

P1888 三角函数

  #include<stdio.h>
int main() {
    int a[3], j, i, min, max, t, flag = 0;
    for (i = 0; i < 3; i++)
        scanf("%d", &a[i]);
    for (i = 0; i < 3; i++)
        for (j = i + 1; j < 3; j++) {
            if (a[i] > a[j]) {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
    min = a[0];
    max = a[2];
    for (i = 2; i <= min; i++) {
        if (min % i == 0 && max % i == 0) {
            flag = i;
        }
    }
    if (flag != 0) {
        max = max / flag;
        min = min / flag;
    }
    printf("%d/%d", min, max);

    return 0;
}

P1046 陶陶摘苹果

#include "stdio.h"
int main()
{
    int a[10] = {0}, x, y = 0;
    for (int i = 0; i < 10; i++)
        scanf("%d", &a[i]);
    scanf("%d", &x);
    for (int i = 0; i < 10; i++)
    {
        if (a[i] <= x + 30)
            y++;
    }
    printf("%d", y);
    return 0;
}

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

#include<stdio.h>
int main() {
    int a[3], j, i, t;
    for (i = 0; i < 3; i++)
        scanf("%d", &a[i]);
    for (i = 0; i < 3; i++)//排序
        for (j = i + 1; j < 3; j++) {
            if (a[i] > a[j]) {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
    if (a[0] + a[1] < a[2])
    {
        printf("Not triangle");
        return 0;
    }
    if (a[0] * a[0] + a[1] * a[1] == a[2] * a[2])//直角
        printf("Right triangle\n");
    if (a[0] * a[0] + a[1] * a[1] > a[2] * a[2])//锐角
        printf("Acute triangle\n");
    if (a[0] * a[0] + a[1] * a[1] < a[2] * a[2])//钝角
        printf("Obtuse triangle\n");
    if ((a[0] == a[1] ) || (a[2] == a[1])|| (a[0] == a[2]))//等腰
        printf("Isosceles triangle\n");
    if (a[0] == a[1] && a[0] == a[2])//等边
        printf("Equilateral triangle");
    return 0;
}

[P4414 [COCI2006-2007#2] ABC](https://www.luogu.com.cn/problem/P4414)

#include<stdio.h>
int main() {
    int a[3], i, t, j;
    char b[3] = { 'A','B','C' }, d[4] = {'\0'};
    for (i = 0; i < 3; i++)
        scanf("%d ", &a[i]);
    for (i = 0; i < 3; i++)
        scanf("%c", &d[i]);
    for (i = 0; i < 2; i++) {
        for (j = i + 1; j <= 2; j++) {
            if (a[i] > a[j]) {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
    }
    i = 0;

    while (d[i] != '\0') {
        for (j = 0; j < 3; j++) {
            if (d[i] == b[j])
                printf("%d ", a[j]);
        }
        i++;
    }
    return 0;
}
最后修改:2020 年 04 月 09 日
如果觉得我的文章对你有用,请随意赞赏