要求:所要寻找的四位偶数的范围。每组输入有两个四位数:第一个数字是最小范围;第二个数字是最大范围。如果输入遇到0,输入结束;若输入中有非四位数,报错,但程序不结束。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main() {
int min = 1, max = 1;//初始化变量
while (min != 0 || max != 0) {//应题目要求这必然是一个循环
scanf("%d %d", &min, &max);
if (min == 0 || max == 0) {//判断min、max是否有零
return 0;//有就结束
}
int counter = 0;
int arr[5] = { 0 };
int a = min / 1000, b = max / 1000;//这里是判断输入数字是否为四位数,四位数必定可以被一千÷
if (a * b == 0) {
printf("Error\n");
continue;//报错,然后回去,不执行下面的代码
}
if (min % 2 == 0) {
int n = 0;
for(n=min;min<=n&&max>=n;n=n+2)//范围内找偶数一个一个的试
{
int tem = n;
int i = 0;//临时变量用的时候再创建,全局变量不好控制
while (tem != 0)//若不加入tem作为临时变量,则n的值最后变为0,只循环一次
{
arr[i] = tem % 10;
tem = tem / 10;
i++;
}
if (arr[0] != arr[1] && arr[0] != arr[2] && arr[0] != arr[3] && arr[1] != arr[2] && arr[1] != arr[3]&&arr[2] != arr[3]) {
printf("%d ", n);//上面那一大串应该是可以优化的,但是我懒了
counter++;//计数
}
}
printf("\ncounter=%d\n", counter);
}
else {
int n = 0;
for (n = min+1; min <= n && max >= n; n = n + 2)
{//奇数就加一,其余和上面一样
int tem = n;
int i = 0;
while (tem != 0)
{
arr[i] = tem % 10;
tem = tem / 10;
i++;
}
if (arr[0] != arr[1] && arr[0] != arr[2] && arr[0] != arr[3] && arr[1] != arr[2] && arr[1] != arr[3] && arr[2] != arr[3]) {
printf("%d ", n);
counter++;
}
}
printf("\ncounter=%d\n", counter);
}
}
return 0;
}