显性测试数据全对的WA之uva355(已AC)

来源:互联网 发布:河北经济网络电视台 编辑:程序博客网 时间:2024/05/23 01:57

题目:

Write a program to convert a whole number specified in any base (2..16) to a whole number in any
other base (2..16). “Digits” above 9 are represented by single capital letters; e.g. 10 by A, 15 by F, etc.
Input
Each input line will consist of three values. The first value will be a positive integer indicating the base
of the number. The second value is a positive integer indicating the base we wish to convert to. The
third value is the actual number (in the first base) that we wish to convert. This number will have
letters representing any digits higher than 9 and may contain invalid “digits”. It will not exceed 10
characters. Each of the input values on a single line will be separated by at least one space.
Output
Program output consists of the original number followed by the string ‘base’, followed by the original
base number, followed by the string ‘=’ followed by the converted number followed by the string ‘base’
followed by the new base. If the original number is invalid, output the statement
original_V alue is an illegal base original_Base number
where original_V alue is replaced by the value to be converted and original_Base is replaced by the
original base value.
Sample input
2 10 10101
5 3 126
15 11 A4C
Sample output
10101 base 2 = 21 base 10
126 is an illegal base 5 number
A4C base 15 = 1821 base 11

2017/3/7

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long long ori(char a[],int k1)
{
    int k,len,i,flag=0;
    long long sum=0;
    for(len=0; a[len]!='\0'; len++);
    for(i=0; i<len; i++)
    {
        if(a[i]==' ')
            k=0;
        else if(a[i]>=65)
            k=a[i]-'A'+10;
        else if(a[i]>=48&&a[i]<=57)
            k=a[i]-'0';
        else k=100;                               //(*******)
        if(k>=k1)
        {
            printf("%s is an illegal base %d number\n",a,k1);
            flag=-1;
            break;
        }

        sum=sum*k1+k;
    }
    if(flag!=-1)
        return sum;
    else
        return flag;
}


int change(long long a,char b[],int k)
{
    int c,i1=0,i=0;
    char ch;
    do
    {
        c=a%k;
        if(c>=10)
            b[i1]='A'+c-10;
        else if(c>=0&&c<=9)
            b[i1]='0'+c;
        a=a-c;
        a=a/k;
        i1++;
    }
    while(a!=0);
    b[i1]='\0';
    for(i=0; i<=i1/2-1; i++)
    {
        ch=b[i];
        b[i]=b[i1-i-1];
        b[i1-i-1]=ch;
    }
    return i1;
}


int main()
{
    char a[50];
    int i,kin,kout;
    while ((scanf("%d %d %s",&kin,&kout,a))!=EOF)
    {
        int lenOut;
        long long sum;
        char b[50];
        sum=ori(a,kin);
        if(sum!=-1)
        {
            lenOut=change(sum,b,kout);


            printf("%s base %d = %s base %d\n",a,kin,b,kout);
        }


    }
    return 0;
}

今日解决问题:1.FFFFFFFFFF会造成40位的2进制,更改了两个数组的大小(从20改为50),将sum均改为了long long

今日发现问题:1.double无法%,无法把long long直接粗暴的换成double

是否AC:否。

考虑问题:是否会输入负数?

-----------------------------------------------------------

3/7.删去此行后AC

分析:原担心有部分奇怪字符输入会可能不是大于而是小于(如askii码为1)

就令k=100,没想到反而出错。

回答问题:是否输入负数:否。

是否AC:是。

0 0
原创粉丝点击