OpenJudge - NOI - 1.4编程基础之逻辑表达式与条件分支(C语言 全部题解)

01:判断数正负

#include <stdio.h>

int main(void)
{
    int num;
    scanf("%d", &num);

    if (num > 0) {
        printf("positive\n");
    } else if (num == 0) {
        printf("zero\n");
    } else {
        printf("negative\n");
    }

    return 0;
}

02:输出绝对值

#include <stdio.h>
#include <math.h>

int main(void)
{
    double num;
    scanf("%lf", &num);

    num = fabs(num);/* 调用了 fabs() 这一个求绝对值的函数*/

    printf("%.2lf\n", num);

    return 0;
}

03:奇偶数判断

#include <stdio.h>

int main(void)
{
    int num;
    scanf("%d", &num);
    
    if (num % 2 == 0)
        printf("even\n");
    else
        printf("odd\n");

    return 0;
}

04:奇偶ASCII值判断

#include<stdio.h>

int main(void)
{
    char c;
    scanf("%c", &c);

    if(c % 2 == 0)
        printf("NO\n");
    else
        printf("YES\n");

    return 0;
}

05:整数大小比较

#include <stdio.h>

int main(void)
{
    /* 注意一下 x 和 y 的范围 */
    long long x, y;

    scanf("%lld%lld", &x, &y);

    if (x > y) {
        printf(">\n");
    } else if (x == y) {
        printf("=\n");
    } else {
        printf("<\n");
    }

    return 0;
}

06:判断是否为两位数

#include <stdio.h>

int main(void)
{
    int num;
    scanf("%d", &num);
    
    if (num >= 10 && num <= 99)
        printf("1\n");
    else
        printf("0\n");
    
    return 0;
}

07:收集瓶盖赢大奖

#include <stdio.h>

int main(void)
{
    int num_1, num_2;
    scanf("%d%d", &num_1, &num_2);
    
    if (num_1 >= 10 || num_2 >= 20)
        printf("1\n");
    else
        printf("0\n");

    return 0;
}

08:判断一个数能否同时被3和5整除

#include <stdio.h>

int main(void)
{
    int num;
    scanf("%d", &num);
    
    if ((num % 3 == 0) && (num % 5 == 0))
        printf("YES\n");
    else
        printf("NO\n");

    return 0;
}

09:判断能否被3,5,7整除

#include <stdio.h>
#include <stdbool.h>

int main(void)
{
    bool flag = false;/* 设定一个标志位 */

    int num;
    scanf("%d", &num);

    if (num % 3 == 0) {
        printf("3 ");
        flag = true;
    }
    if (num % 5 == 0) {
        printf("5 ");
        flag = true;
    }
    if (num % 7 == 0) {
        printf("7 ");
        flag = true;
    }

    if (flag == false)
        printf("n\n");

    return 0;
}

10:有一门课不及格的学生

#include <stdio.h>

int main(void)
{
    int counter = 0; /* 记录不及格科目的数量 */
    
    int score_1, score_2;
    scanf("%d%d", &score_1, &score_2);
    
    if (score_1 < 60)
        counter++;
    
    if (score_2 < 60)
        counter++;
    
    if (counter == 1)
        printf("1\n");
    else
        printf("0\n");

    return 0;
}

11:晶晶赴约会

#include <stdio.h>

int main(void)
{
    int day;
    scanf("%d", &day);

    if (day == 1 || day == 3 || day == 5)
        printf("NO\n");
    else
        printf("YES\n");

    return 0;
}

12:骑车与走路

#include <stdio.h>

int main(void)
{
    int distance;
    scanf("%d", &distance);

    if (distance < 100) { /* 用数学方法可以求得临界距离为100 */
        printf("Walk\n");
    } else if (distance == 100) {
        printf("All\n");
    } else {
        printf("Bike\n");
    }

    return 0;
}

13:分段函数

#include <stdio.h>

int main(void)
{
    double x;
    scanf("%lf", &x);

    double y = 0.0;

    if (x >= 0 && x < 5) {
        y = -x + 2.5;
    } else if (x >= 5 && x < 10) {
        y = 2 - 1.5 * (x - 3) * (x - 3);
    } else {
        y = x / 2 - 1.5;
    }

    printf("%.3lf\n", y);

    return 0;
}

15:最大数输出

#include <stdio.h>

int main(void)
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    
    if (a > b) {
        int temp = a;
        a = b;
        b = temp;
    }
    
    if (b > c) {
        int temp = b;
        b = c;
        c = temp;
    }
    
    printf("%d\n", c); /* 经过两轮交换,c一定是最大的 */

    return 0;
}

16:三角形判断

#include <stdio.h>

int main(void)
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);

    if ((a + b > c) && (a - b < c))
        printf("yes\n");
    else
        printf("no\n");

    return 0;
}

17:判断闰年

#include <stdio.h>

int main(void)
{
    int year;
    scanf("%d", &year);

    /* 闰年是能被400整除,或者是能被4整除但是不能被100整除的年份 */
    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
        printf("Y\n");
    else
        printf("N\n");

    return 0;
}

18:点和正方形的关系

#include<stdio.h>

int main(void)
{
    int x, y;
    scanf("%d%d", &x, &y);

    if(-1 <= x && x <= 1 && -1 <= y && y <= 1)
        printf("yes");
    else
        printf("no");
    
    return 0;
}

19:简单计算器

#include <stdio.h>

int main(void)
{
    int a, b, c;
    char ch;
    scanf("%d%d %c", &a, &b, &ch);/* 在%c前加一个空格,不然程序会出问题 */

    switch (ch)
    {
        case '+':
        {
            c = a + b;
            printf("%d\n", c);
            break;
        }

        case '-':
        {
            c = a - b;
            printf("%d\n", c);
            break;
        }

        case '*':
        {
            c = a * b;
            printf("%d\n", c);
            break;
        }

        case '/':
        {
            if (b == 0) {
                printf("Divided by zero!\n");
            } else {
                c = a / b;
                printf("%d\n", c);
            }
            break;
        }

        default:
        {
            printf("Invalid operator!\n");
            break;
        }
    }

    return 0;
}

20:求一元二次方程的根

这道题涉及到了浮点型数的精度问题,博主建议初学者不用太过细究

#include<stdio.h>
#include<math.h>

int main(void)
{
    float a, b, c;
    scanf("%f%f%f", &a, &b, &c);

    if(b * b == 4 * a * c)
    {
        if(b == 0)
            printf("x1=x2=%.5f\n", b / (2 * a));
        else
            printf("x1=x2=%.5f\n", (-b) / (2 * a));
    }
    else if(b * b > 4 * a * c)
        printf("x1=%.5f;x2=%.5f\n", (-b + sqrtf((b * b - 4 * a *c))) / (2 * a), (-b - sqrtf(b * b - 4 * a * c)) / (2 * a));
    else
    {
        if(b == 0)
            printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi\n", b / (2 * a), sqrtf(4 * a * c - b * b) / (2 * a), b / (2 * a), sqrtf(4 * a * c - b * b) / (2 * a));
        else
            printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi\n", -b / (2 * a), sqrtf(4 * a * c - b * b) / (2 * a), -b / (2 * a), sqrtf(4 * a * c - b * b) / (2 * a));
    }

    return 0;
}

21:苹果和虫子2

#include <stdio.h>

int main(void)
{
    int n, x, y;
    scanf("%d%d%d", &n, &x, &y);
    
    if (y >= n * x) /* 苹果吃完了 */
        printf("0\n");
    else if (y % x == 0) /* 苹果没有吃完 */
        printf("%d\n", n - y / x);
    else
        printf("%d\n", n - y / x - 1);

    return 0;
}

写在最后

博客仅供参考

猜你喜欢

转载自www.cnblogs.com/liuzhaotong/p/12193975.html