#define _CRT_SECURE_NO_WARNINGS 1
练习3-1
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
puts("请输入两个整数。");
printf("整数A:"); scanf("%d", &a);
printf("整数B:"); scanf("%d", &b);
if (a % b == 0)
puts("B是A的约数。");
else
puts("B不是A的约数。");
return 0;
}
练习3-2
#include<stdio.h>
int main()
{
int no;
printf("请输入一个整数:");
scanf("%d", &no);
if (no == 0)
puts("该整数为0。");
else if (no > 0)
puts("该整数为正数。");
else if(no < 0)
puts("该整数为负数。");
return 0;
}
//运行正确
练习3-3
#include <stdio.h>
int main()
{
int x;
printf("请输入一个整数:");
scanf("%d", &x);
if (x < 0)
printf("绝对值是%d。", -x);
else
printf("绝对值是%d。", x);
return 0;
}
练习3-4
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
puts("请输入两个整数。");
printf("整数A:"); scanf("%d", &a);
printf("整数B:"); scanf("%d", &b);
if (a == b)
puts("A等于B。");
else if (a > b)
puts("A大于B。");
else
puts("A小于B。");
return 0;
}
练习3-5
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
puts("请输入两个整数。");
printf("整数A:"); scanf("%d", &a);
printf("整数B:"); scanf("%d", &b);
printf("%d\n", (a < b));
printf("%d\n", (a > b));
printf("%d\n", (a == b));
printf("%d\n", (a != b));
return 0;
}
练习3-6
#include<stdio.h>
int main()
{
int a, b, c = 0;
scanf("%d %d %d", &a, &b, &c);
int min = a;
if (b < min)
min = b;
if (c < min)
min = c;
printf("%d", min);
return 0;
}
练习3-7
#include<stdio.h>
int main()
{
int a, b, c, d = 0;
scanf("%d %d %d %d", &a, &b, &c, &d);
int max = a;
if (b > max)
max = b;
if (c > max)
max = c;
if (d > max)
max = d;
printf("%d", max);
return 0;
}
练习3-8
#include <stdio.h>
int main()
{
int n1,n2;
puts("请输入两个整数。");
printf("整数1:"); scanf("%d", &n1);
printf("整数2:"); scanf("%d", &n2);
if (n1 > n2)
printf("它们的差是%d。", n1 - n2);
if (n1 <= n2)
//else
printf("它们的差是%d。", n2 - n1);
//printf("它们的差是%d。", (n1 > n2) ? n1 - n2 : n2 - n1);
return 0;
}
练习3-9
#include<stdio.h>
int main()
{
int a, b, c = 0;
scanf("%d %d %d", &a, &b, &c);
int m1, m2;
m1 = a < b ? a : b;
m2 = m1 < c ? m1 : c;
printf("%d", m2);
return 0;
}
练习3-10
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
puts("请输入两个整数。");
printf("整数A:"); scanf("%d", &a);
printf("整数B:"); scanf("%d", &b);
printf("整数C:"); scanf("%d", &c);
if (a == b && b == c)
{
puts("三个值都相等。");
}
else if ((a == b && b != c) || (a == c && c != b) || (b == c && c != a))//严谨
{
puts("有两个值相等。");
}
else
{
puts("三个值各不相同。");
}
return 0;
}
练习3-11
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
puts("请输入两个整数。");
printf("整数A:"); scanf("%d", &a);
printf("整数B:"); scanf("%d", &b);
if ((a - b >= 10) || (b - a >= 10)) //用或
{
puts("它们的差大于等于10。");
}
else
{
puts("它们的差小于等于10。");
}
//if ((a-b>=-10) && (a-b<=10)) //用与
//{
// puts("它们的差小于等于10。");
//}
//else
//{
// puts("它们的差大于10。");
//}
return 0;
}
练习3-12
#include <stdio.h>
int main()
{
int no = 0;
printf("请输入一个整数:");
scanf("%d", &no);
switch (no % 2)
{
case 0:
puts("该整数是偶数。");
break;
case 1:
puts("该整数是奇数。");
break;
}
return 0;
}
练习3-13
#include <stdio.h>
int main()
{
int month = 0;
printf("请输入月份:");
scanf("%d", &month);
switch (month)
{
case 3:
case 4:
case 5:printf("%d月是春季。\n", month); break;
case 6:
case 7:
case 8:printf("%d月是夏季。\n", month); break;
case 9:
case 10:
case 11:printf("%d月是秋季。\n", month); break;
case 12:
case 1:
case 2:printf("%d月是冬季。\n", month); break;
default:printf("%d月不存在!!\a\n", month); break;
}
return 0;
}