HDU 1335 Basically Speaking(进制转换问题)
2013-08-15 08:58
260 查看
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1111000 2 10
1111000 2 16
2102101 3 10
2102101 3 15
12312 4 2
1A 15 2
1234567 10 16
ABCD 16 15
[align=left]Sample Output[/align]
120
78
1765
7CA
ERROR
11001
12D687
D071
题目大意:输入一个 数(字符串) 输入一个进制a(原数的进制),再输入一个进制b 要求把这个数以进制b输出。如果答案超过7个单位,输出ERROR,右对齐.思路:就是把这个字符串转化为数,再转化为字符串。这道题,我自己写的没过,也不知为什么感觉自己测试数据都过了(就是一直WA,WA15次之久)但我看见一个自认为很优秀的代码。上代码。
但我的代码一直WA,其实思路一样的,只是实现方式问题。如果有路过的,虚心求教!
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1111000 2 10
1111000 2 16
2102101 3 10
2102101 3 15
12312 4 2
1A 15 2
1234567 10 16
ABCD 16 15
[align=left]Sample Output[/align]
120
78
1765
7CA
ERROR
11001
12D687
D071
题目大意:输入一个 数(字符串) 输入一个进制a(原数的进制),再输入一个进制b 要求把这个数以进制b输出。如果答案超过7个单位,输出ERROR,右对齐.思路:就是把这个字符串转化为数,再转化为字符串。这道题,我自己写的没过,也不知为什么感觉自己测试数据都过了(就是一直WA,WA15次之久)但我看见一个自认为很优秀的代码。上代码。
#include <iostream> #include <stdio.h> #include <cstdio> #include <cstring> using namespace std; int main() { int se,re; char x; char s[20],r[20]; while(scanf("%s%d%d",s,&se,&re)>0) { int l=strlen(s),k=0,perc; long long sum=0; for(int i=0; i<l; i++) { if(s[i]>='0'&&s[i]<='9') //把进制转化为数这点很巧妙 sum=sum*se+s[i]-'0'; else if(s[i]>='A'&&s[i]<='Z') sum=sum*se+s[i]-'A'+10; } while(sum>0) { perc=sum%re; if(perc>=10) r[k++]=perc-10+'A'; else r[k++]=perc+'0'; sum=sum/re; } for(int i=0;i<k/2;i++) { x=r[i]; r[i]=r[k-i-1]; r[k-i-1]=x; } if(k>7) cout<<" ERROR"<<endl; else printf("%7s\n",r); memset(s,0,sizeof(s)); memset(r,0,sizeof(r)); /*这个事是作者写的 ,我修改了一下。 { for(int j=0; j<7-k; j++) cout<<" "; for(int j=k-1; j>=0; j--) cout<<r[j]; cout<<endl; } */ } return 0; }
但我的代码一直WA,其实思路一样的,只是实现方式问题。如果有路过的,虚心求教!
#include<stdio.h> #include<string.h> #include<math.h> int main() { long int n,d; char s[200],len; char c[200],x; int t; long int i,j,k; long int sum,ss; memset(s,0,sizeof(s)); memset(c,0,sizeof(c)); while(scanf("%s%ld%ld",s,&n,&d)>0) { len=strlen(s); j=0; sum=0; for(i=len-1;i>=0;i--) { if('0'<=s[i]&&s[i]<='9') sum=sum+(s[i]-'0')*pow(n,j++); else { switch(s[i]) { case 'A':sum=sum+10*(long int)pow(n,j++);break; case 'B':sum=sum+11*(long int)pow(n,j++);break; case 'C':sum=sum+12*(long int)pow(n,j++);break; case 'D':sum=sum+13*(long int)pow(n,j++);break; case 'E':sum=sum+14*(long int)pow(n,j++);break; case 'F':sum=sum+15*(long int)pow(n,j++);break; } } } if(sum==0) { printf(" 0\n"); memset(s,0,sizeof(s)); memset(c,0,sizeof(c));continue; } ss=sum; k=0; while(ss) { t=ss%d; if(0<=t&&t<=9) c[k++]=t+'0'; if(10<=t&&t<=15) c[k++]=55+t; ss=ss/d; } for(i=0;i<k/2;i++) { x=c[i]; c[i]=c[k-i-1]; c[k-i-1]=x; } /* if(k<8) { for(j=0;j<7-k;j++) printf(" "); k=k-1; for(;k>=0;k--) printf("%c",c[k]); } */ if(k<8) printf("%7s",c); else printf(" ERROR"); printf("\n"); memset(s,0,sizeof(s)); memset(c,0,sizeof(c)); } return 0; }
相关文章推荐
- HDU 1335 Basically Speaking(进制转换)
- HDU 4432 因子之和以及进制转换问题
- HDU 1335 Basically Speaking【进制转换】
- HDU 1335 Basically Speaking (模拟进制转换)
- HDU:2031 进制转换问题
- hdu1877-进制转换问题 vector容器
- POJ1546 & HDU 1335 & ZOJ 1334 Basically Speaking(进制转换)
- hdu 2553 n皇后问题
- hdu 1443 + cug1015 约瑟夫问题变型
- HDU 2159 FATE (动态规划dp之二维完全背包问题)
- HDU 2031 进制转换
- HDU 2181 哈密顿绕行世界问题 (求一个图的所有哈密顿回路)
- HDU1002大数相加问题
- 中国剩余定理 hdu 1573 X问题
- HDU_1009 (贪心问题)
- HDU 2546 饭卡 【隐藏的背包问题】
- hdu 2570 //这题想到贪心法有些曲折哈 是一道简单的贪心算法(最优装载问题)
- hdu ——3018——Ant Trip(一笔画问题)
- 整数划分问题--递归求解 例题:HDU 1028
- HDU Proud Merchants 【需要排序的0/1背包问题】