Basically Speaking

0人阅读 评论(0) 收藏 举报
分类:

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-digit 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. 
InputThe 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. 
OutputThe 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

要细心啊,每个小问题都会导致无法ac,一直找不到问题不通过很难受的

#include <stdio.h>

//atoi:把字符型转化为整型
void convert(char s[],int frombase,int tobase){
    char *p,t[1024];
    long long result=0;
    //atoi:把字符型转化为整型
     p=s;        //注意此处,给p初始化为数组s的第一个值
                 //如果把它写在while()之内,会没有输出
    while(*p){

        result*=frombase;
        if(*p>='0'&&*p<='9')
            result+=*p-'0';
        else if(*p>='A'&&*p<='F')
            result+=*p+10-'A';
        p++;
    }
    int count=0;

    //itoa:把整型转化为字符型
    while(result){
        int val=result%tobase;
        if(val>=0&&val<=9)
            t[count]=val+'0';
        else if(val>=10&&val<=15)
            t[count]=val-10+'A';
        count++;
        result/=tobase; //如果把这块写成val/=tobase,会停止工作;
    }                    //输出也要非常注意
    if(count==0)                    
        printf("      0\n");
    else if(count>7)
        printf("  ERROR\n");
    else{
        int i;
        for(i=count;i<7;i++){
            t[i]=' ';
        }
        for(i=6;i>=0;i--)
            putchar(t[i]);
        putchar('\n');
    }
}

int main(void)
{
    int frombase,tobase;
    char s[1024];
    while(scanf("%s%d%d",s,&frombase,&tobase)!=EOF){ //s之前不用写&
        convert(s,frombase,tobase);
    }
    return 0;
}


查看评论

zoj 1334 Basically Speaking

#include "iostream" #include "map" #include "string" #include "cmath" #include "algorithm" #include ...
  • yzl_rex
  • yzl_rex
  • 2012-03-18 23:49:00
  • 346

HDU1335 POJ1546 UVA389 UVALive5306 ZOJ1334 Basically Speaking【进制+atio+itoa】

Basically Speaking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4772   Accepte...
  • tigerisland45
  • tigerisland45
  • 2016-07-25 20:14:56
  • 955

hdu 1335 Basically Speaking

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1335
  • hackerwin7
  • hackerwin7
  • 2014-10-29 14:37:09
  • 1046

UVa389 - Basically Speaking

#include #include #include using namespace std; #define N 30 int main() { char input[N]; ...
  • wuli2496
  • wuli2496
  • 2012-02-22 19:58:43
  • 757

UVa 389 - Basically Speaking

题目: 分析: 说明: #include #include #include using namespace std; char buf[36]; int change1( char...
  • mobius_strip
  • mobius_strip
  • 2014-08-01 16:31:18
  • 850

ZOJ Basically Speaking

#include #include int main() { char str[8]; int before,after,result,i,num; while(scanf("%s%d%d",s...
  • andyyang0212
  • andyyang0212
  • 2014-08-16 15:45:15
  • 166

HDU1335:Basically Speaking

Problem Description The Really Neato Calculator Company, Inc. has recently hired your team to help ...
  • libin56842
  • libin56842
  • 2013-04-25 20:36:21
  • 1096

HDU 1335 Basically Speaking

简单的进制转换题#include #include #include using namespace std; #define N 20 char s[N],w[N]; char y[N]; int ...
  • zyx520ytt
  • zyx520ytt
  • 2015-09-16 22:13:54
  • 241

【水题】hdoj1335 Basically Speaking

代码我基本参照别人写的 因为不是很了解进制转换的itoa函数。所以差点很脑残的写一个各种进制转换。对自己也是蜜汁服气 但是一道水题也是收获很多,下面一一道来。 【题目】 Problem Desc...
  • acmit_yfr
  • acmit_yfr
  • 2016-11-07 19:45:51
  • 94

HDU&nbsp;1335&nbsp;Basically&nbsp;Speaking

http://acm.hdu.edu.cn/showproblem.php?pid=1335   Basically Speaking Time Limit: 2000/1000 MS (Java/...
  • wlzzhcsdn
  • wlzzhcsdn
  • 2017-10-13 14:20:55
  • 48
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 528
    积分: 428
    排名: 11万+
    博客专栏
    文章存档
    最新评论