Posted on 2010-10-19 20:02
李东亮 阅读(1773)
评论(0) 编辑 收藏 引用 所属分类:
acm
ZOJ 1334 Basically
Speaking
这是一道简单的进制转换题,也是一道让我无语的题。
题目大意较为简单,但是提交了n次,一直PE,检查了好多地方,实在感觉没头绪了,就活马当死马医,把printf(“%*c”, len, ‘ ’)换成了循环,因为要右对齐,所以输出些空格,竟然AC了,竟然是对printf的这种输出格式理解有误,无语呀。
参考代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
char a[50];
char ch[] = "0123456789ABCDEF";
int main(void)
{
int to, from;
unsigned sum;
int len;
int i;
unsigned t;
freopen("in.txt", "r", stdin);
while (scanf("%s %d %d", a, &from, &to) != EOF)
{
sum = 0;
t = 1;
len = strlen(a);
for (i = len-1; i >= 0; --i)
{
if (isdigit(a[i]))
{
sum += (a[i] - '0')*t;
}
else
{
sum += (a[i] - 'A' + 10)*t;
}
t *= from;
}
if (to == 10)
{
len = (int)log10(sum)+1;
if (len > 7)
{
printf(" ERROR\n");
}
else
{
printf("%7d\n", sum);
}
}
else
{
i = 0;
while (sum > 0)
{
a[i++] = ch[sum%to];
sum /= to;
}
if (i > 7)
{
printf(" ERROR\n");
}
else
{
len = 7-i;
printf("%*c", len, ' ');
--i;
while (i >= 0)
{
putchar(a[i--]);
}
printf("\n");
}
}
}
return 0;
}