日期:2014-05-16  浏览次数:20627 次

zoj1334题 自己写的代码怎么总是wrong answer,哪里错了
本帖最后由 Choice_JJ 于 2012-11-09 19:19:43 编辑 题目是:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=334

#include "stdio.h"
#include "string.h"
#include "math.h"
#define MAX 100

int count = 0;

void str_to_num(char *num, int *temp, int len)
{
int i;
for (i = 0; i < len; i++)
{
if (num[i] <= '9' && num[i] >= '0')
temp[i] = num[i] - '0';
if (num[i] <= 'F' && num[i] >= 'A')
temp[i] = num[i] - 'A' + 10;
}
}

int from_to_ten(int *temp, int base_from, int len)
{
int i, ten = 0;
for (i = 0; i < len; i++)
ten += pow((double)base_from, len - i - 1) * temp[i];
return ten;
}


void ten_to_to(int ten, int base_to, int *temp)
{
if (ten / base_to != 0)
ten_to_to(ten / base_to, base_to, temp);
temp[count++] = ten % base_to;
}

void  num_to_str(char *num, int *temp, int len)
{
int i;
for (i = len - 1; i >= 0; i--)
{
if (temp[i] <= 9 && temp[i] >= 0)
num[i] = temp[i] + '0';
if (temp[i] <= 16 && temp[i] >= 10)
num[i] = temp[i] + 'A' - 10;
}
num[len] = '\0';
}

int main(int argc, char **argv)
{
int base_from, base_to, temp[MAX], ten, len;
char num[MAX];
freopen("in.txt", "r", stdin);
while(scanf("%s%d%d", num, &base_from, &base_to) != EOF)
{
count = 0;
len = strlen(num);
str_to_num(num, temp, len);
ten = from_to_ten(temp, base_from, len);
ten_to_to(ten, base_to, temp);
if (count > 7)
printf("%7s\n", "ERROR");
else
{
num_to_str(num, temp, count);
printf("%7s\n", num);
}
}


return 0;
}


谢谢~
------最佳解决方案--------------------
找到问题了。。用memset()将那两个数组每次循环开始初始化为0,就可以了
------其他解决方案--------------------
找到问题了。。用memset()将那两个数组每次循环开始初始化为0,就可以了