注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Hao的博客

I'm on my way……

 
 
 

日志

 
 
 
 

诡异的进制转换问题  

2009-04-14 17:34:18|  分类: Algorithm discov |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

今天在UVa的OJ上碰到了一个很简单的进制转换问题,题目如下:

Basically Speaking
Time Limit: 1000MS  Memory Limit: 10000K
Total Submissions: 1840  Accepted: 961

Description

The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could convert among number bases. The company thought this was a stupendous idea and has asked your team to come up with the prototype program for doing base conversion. The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features:

 

It will have a 7-digital display.

Its buttons will include the capital letters A through F in addition to the digits 0 through 9.

It will support bases 2 through 16.

 

Input

The input for your prototype program will consist of one base conversion per line. There will be three numbers per line. The first number will be the number in the base you are converting from. The second number is the base you are converting from. The third number is the base you are converting to. There will be one or more blanks surrounding (on either side of) the numbers. There are several lines of input and your program should continue to read until the end of file is reached.
Output

The output will only be the converted number as it would appear on the display of the calculator. The number should be right justified in the 7-digit display. If the number is to large to appear on the display, then print ``ERROR'' (without the quotes) right justified in the display.
Sample Input

1111000  2 10
1111000  2 16
2102101  3 10
2102101  3 15
  12312  4  2
     1A 15  2
1234567 10 16
   ABCD 16 15
Sample Output

    120
     78
   1765
    7CA
  ERROR
  11001
 12D687
   D071
Source

Mid-Central USA 1995

题目的意思很简单,就是将第一个数从一个进制转换到另一个进制。只要注意输出的格式以及0的情况就可以了。但是还是WA,看了很久还是没看出什么问题。最后只能到网上去看看有什么东西,可是还是觉得没什么问题。搜着搜着就搜到POJ上了,想着没事干就提交一下试下吧。结果居然A了。难道是我提交的时候出问题了,重新打开UVa OJ,直接将POJ上的代码贴上去,盼着奇迹的出现,还是WA。难道UVa上的测试数据就如此诡异?!先把代码贴在下面,等发现了什么不对劲的地方再回来吧。

#include<iostream>
#define MAX 100
using namespace std;
char num[MAX];
int main(){
    int origB,convB;
    int i,temp;
    int sum;
    bool done;
    while(cin>>num>>origB>>convB){
        if(num[0]=='0'){               //处理0的情况
            for(i=0;i<6;i++)cout<<' ';
            cout<<0<<endl;
            continue;
        }
        temp=1;
        sum=0;
        done=false;
        for(i=0;num[i];i++);    //从后面往前面处理,省去求origB的幂级数
        for(i=i-1;i>=0;i--){      //开始处理求和
             if(num[i]>='0'&&num[i]<='9')sum+=(num[i]-'0')*temp;
             else sum+=(num[i]-'A'+10)*temp;
             temp*=origB;      //权值更新
        }
        i=0;
        while(sum){
            if(i>6){        //如果已经得出了第八位,则输出ERROR
                cout<<"  ERROR"<<endl;
                done=true;
                break;
            }
            temp=sum%convB;
            if(temp>=0&&temp<=9)num[i]=temp+'0';
            else num[i]=temp-10+'A';
            i++;
            sum/=convB;
        }
        if(!done){
            for(temp=7;temp>i;temp--)cout<<' ';
            while(i--)cout<<num[i];
            cout<<endl;
        }
    }
    return 0;
}

 
  评论这张
 
阅读(143)| 评论(1)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017