简单C语言例题

本文最后更新于:2019年10月13日 晚上

一些C语言入门的题目,有代码

  1. 输入一个整数,输出其符号(若>= 0, 输出 1;若<0,输出-1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
// 输入一个整数,输出其符号(若>= 0, 输出 1;若<0,输出-1)
int main()
{
int a;
scanf("%d", &a);
if(a >= 0)
{
printf("1");
}
else
{
printf("-1");
}
}

  1. 输入3个数,输出其中最小值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
// 输入3个数,输出其中最小值
int main()
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(a < b)
{
b = a;
}
if(b < c)
{
c = b;
}
printf("%d\n", c);
}

  1. 输入一个整数,是否既是5又是7的整倍数,若是,则输出Yes,否则输出No。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
// 输入一个整数,是否既是5又是7的整倍数,若是,则输出Yes,否则输出No。
int main()
{
int a;
scanf("%d", &a);
if(a % 5 == 0 && a % 7 == 0)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}

  1. 有一分段函数$y=
    \begin{cases}
    x& (-5<x<0) \\
    x-1& (x=0) \\
    x+1& (0<x<10)
    \end{cases}$ 编写程序,要求输入x的值后,输出y的值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{
int x;
scanf("%d", &x);
if(x > -5 && x < 0)
{
printf("%d\n", x);
}
else if(x == 0)
{
printf("%d\n", x - 1);
}
else if(x > 0 && x < 10)
{
printf("%d\n", x + 1);
}
return 0;
}