一个整数,只知道前几位,不知道末二位,被另一个整数除尽了,那么该数的末二位该是什么呢?
Input
输入数据有若干组,每组数据包含二个整数a,b(0<a<10000, 10<b<100),若遇到0 0则处理结束。
Output
对应每组数据,将满足条件的所有尾数在一行内输出,格式见样本输出。同组数据的输出,其每个尾数之间空一格,行末没有空格。
Sample Input
200 40
1992 95
0 0
Sample Output
00 40 80
15
该题我的思想是用求余数减去余数得第一位末两位数。具体问题见代码注释。
WA代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
long long a, b, remainder, last_two;
while(scanf("%lld%lld", &a, &b) != EOF && (a != 0 && b != 0))
{
last_two = 0;
remainder = (a*100) % b;
if(remainder != 0)
last_two = b - remainder;
else
last_two = remainder;
for(; last_two < 100; last_two += b)
{
if(last_two + b >= 100)
printf("%lld\n", last_two);//此处未考虑当求余数减余数为1位数的情况,比如用1994 95 来跑就会输出5而不是05。
else if(last_two < 10)
printf("0%lld ", last_two);
else
printf("%lld ", last_two);
}
}
return 0;
}
修改后的代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int a, b, remainder, last_two;
while(scanf("%d%d", &a, &b) != EOF && (a != 0 && b != 0))
{
last_two = 0;
remainder = (a*100) % b;
if(remainder != 0)
last_two = b - remainder;
else
last_two = remainder;
for(; last_two < 100; last_two += b)
{
if(last_two + b >= 100)
{
if(last_two < 10)
printf("0%d\n", last_two);///最后两位小于10的情况也要放到只有一个数就输出的情况里去。
else
printf("%d\n", last_two);
}
else if(last_two < 10)
printf("0%d ", last_two);
printf("%d ", last_two);
}
}
return 0;
}
优化后:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int a, b, remainder, last_two;
while(scanf("%d%d", &a, &b) != EOF && (a != 0 && b != 0))
{
last_two = 0;
remainder = (a*100) % b;
if(remainder != 0)
last_two = b - remainder;
else
last_two = remainder;
for(; last_two < 100; last_two += b)
if(last_two + b >= 100)
printf("%02d\n", last_two);
else
printf("%02d ", last_two);
}
///%02d表示把整型数据打印最低两位,如果不足两位,用0补齐
return 0;
}
%02d表示把整型数据打印最低两位,如果不足两位,用0补齐。
如 %2d 代表 输出两位整数,不够两位的话会右对齐。
顺便复习下输出语法。以上问题完全可用语法来解决。