首页 > 其他分享 >杭电1335-任意进制的转换

杭电1335-任意进制的转换

时间:2023-02-06 21:04:11浏览次数:68  
标签:will ch 进制 1335 sum st 杭电 int else


Basically Speaking

​http://acm.hdu.edu.cn/showproblem.php?pid=1335​

Problem 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-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.

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

输入  s  n   m

题意:把输入的第一个数s为n进制,转化为m进制

# include <iostream>
# include <cstdio>
# include <cstring>
# include <cmath>
int f(char *ch,int n,int m);//n-->10进制
void f1(int s,int n);//10--〉n进制
char st[10009];
char str[]="0123456789ABCDEF";
int main(){

char ch[1000];
int n,m,i,j;

while(scanf("%s%d%d",ch,&n,&m)!=EOF){

int s = f(ch,n,10);

f1(s,m);





int j = strlen(st)-1;
int i = 0;
char t;
while(i<j){
t = st[i];
st[i] = st[j];
st[j] = t;
i++;
j--;
}

int len = strlen(st);
if(len>7) printf("%7s\n","ERROR");
else printf("%7s\n",st);
}

return 0;
}

int f(char ch[],int n,int m){

int sum = 0;
int len = strlen(ch);
int cnt = 0;
for(int i=len-1;i>=0;i--){
if(ch[i]>='0'&&ch[i]<='9') sum+=(ch[i]-'0')*pow(n,cnt);
else if(ch[i]>='A'&&ch[i]<='F') sum+=(ch[i]-'A'+10)*pow(n,cnt);

cnt++;
}

return sum;
}

//10--n进制
void f1(int s,int n){
memset(st,0,sizeof(st));
int cnt = 0;
while(s!=0){
st[cnt] = str[s%n];
s = s/n;
cnt++;
}


}

代码 2:

#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;
int powe(int,int);
int main()
{
string s;
short b,e;
while(cin>>s>>b>>e)
{
vector<char> v;
string str="";
int sum=0;
for(int i=s.size()-1,c=0;i>=0;--i,++c)
{
if(s[i]>='0'&&s[i]<='9')
sum+=(s[i]-48)*powe(b,c);
if(s[i]>='A'&&s[i]<='F')
sum+=(s[i]-55)*powe(b,c);
}
int r;
while(sum!=0)
{
r=sum%e;
if(r>=0&&r<=9)
v.push_back(r+48);
else if(r==10)
v.push_back('A');
else if(r==11)
v.push_back('B');
else if(r==12)
v.push_back('C');
else if(r==13)
v.push_back('D');
else if(r==14)
v.push_back('E');
else
v.push_back('F');
sum/=e;
}
for(int i=v.size()-1;i>=0;--i)
str+=v[i];
if(str.size()>7)
cout<<setw(7)<<right<<"ERROR"<<endl;
else
cout<<setw(7)<<right<<str<<endl;
}
return 0;
}
int powe(int m,int n)
{
int sum=1;
while(n--)
sum*=m;
return sum;
}


标签:will,ch,进制,1335,sum,st,杭电,int,else
From: https://blog.51cto.com/u_15955675/6040503

相关文章

  • 杭电1407--暴力与优化
    测试你是否和LTC水平一样高ProblemDescription大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!你的任务是:计算方程x^2+y^2+z^2......
  • 杭电1114--完全背包
    Piggy-BankProblemDescriptionBeforeACMcandoanything,abudgetmustbepreparedandthenecessaryfinancialsupportobtained.Themainincomeforthisacti......
  • 杭电1266--数的倒叙
    ReverseNumberProblemDescriptionWelcometo2006’4computercollegeprogrammingcontest!Specially,Igivemybestregardstoallfreshmen!Youarethefuture......
  • 杭电1282-回文
    回文数猜想ProblemDescription一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数。任取一个正整数,如果不是回文数,将该数与他的......
  • 杭电2602---01背包
    骨收集器​​http://acm.hdu.edu.cn/showproblem.php?pid=2602​​问题描述许多年前,在泰迪的家乡有一个叫“拾骨者”的人。这个人喜欢收集不同的骨头,比如狗,牛,他还去了坟......
  • 杭电1248-背包
    院大学生程序设计竞赛(新生为主)寒冰王座TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):13570AcceptedSubm......
  • 杭电1201
    18岁生日ProblemDescriptionGardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都......
  • 杭电1202--学分
    ThecalculationofGPATimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):24013AcceptedSubmission(s):5610P......
  • 杭电1205--吃糖
    吃糖果ProblemDescriptionHOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一种,这样;可......
  • 杭电1163--9余项定理的例子
    #include<iostream>#include<cstdio>#include<algorithm>usingnamespacestd;intmain(){intn,a[10009];inti,t;while(scanf("%d",&n),n!=0){......