您的位置:首页 > 其它

九度oj 进制转换 1026,1118,1138,1194

2016-01-31 21:07 309 查看
1026题目:
http://ac.jobdu.com/problem.php?pid=1026
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0

using namespace std;
int main(){
unsigned long long int m;
int arr[20];
unsigned long long int a,b;//
unsigned long long int answer=0;
int len=0;
while(1){
cin>>m;
if(m==0)
break;
cin>>a>>b;
answer=a+b;
len=0;
while(1){
arr[len]=answer%m;
len++;
if(answer<m)
break;
answer/=m;
}
for(int i=len-1;i>=0;i--)
cout<<arr[i];
cout<<endl;
}
}

/**************************************************************
Problem: 1026
User: zhouyudut
Language: C++
Result: Accepted
Time:0 ms
Memory:1520 kb
****************************************************************/
1118题目: http://ac.jobdu.com/problem.php?pid=1118
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0

using namespace std;
unsigned long long int findTheNum(char a){
if(a=='0')
return 0;
if(a=='1')
return 1;
if(a=='2')
return 2;
if(a=='3')
return 3;
if(a=='4')
return 4;
if(a=='5')
return 5;
if(a=='6')
return 6;
if(a=='7')
return 7;
if(a=='8')
return 8;
if(a=='9')
return 9;
if(a=='A' || a=='a')
return 10;
if(a=='B' || a=='b')
return 11;
if(a=='C' || a=='c')
return 12;
if(a=='D' || a=='d')
return 13;
if(a=='E' || a=='e')
return 14;
if(a=='F' || a=='f')
return 15;
return 0;
}
unsigned long long int charToNum(char s[],unsigned long long int m){
int len=strlen(s);
unsigned long long int answer=0;
for(int i=0;i<len;i++){
answer=answer*m+findTheNum(s[i]);
}
return answer;
}
void numToChar(unsigned long long int num,unsigned long long int m){
char s[20];
int len=0;
int temp;
while(1){
temp=num%m;
if(temp<=9)
{
s[len]='0'+temp;
len++;
}
else{
if(temp==10)
s[len]='A';
if(temp==11)
s[len]='B';
if(temp==12)
s[len]='C';
if(temp==13)
s[len]='D';
if(temp==14)
s[len]='E';
if(temp==15)
s[len]='F';
len++;
}
if(num<m)
{
break;
}
num/=m;
}
for(int i=len-1;i>=0;i--)
{
cout<<s[i];
}
cout<<endl;
}
int main(){
unsigned long long int answer;
char sa[20],sb[20];
unsigned long long int m,n;
while(cin>>m>>sa>>n){
answer=charToNum(sa,m);
// cout<<"a="<<a<<endl;
// cout<<"b="<<b<<endl;
numToChar(answer,n);
}
}

/**************************************************************
Problem: 1118
User: zhouyudut
Language: C++
Result: Accepted
Time:20 ms
Memory:1520 kb
****************************************************************/

1138题目: http://ac.jobdu.com/problem.php?pid=1138
这题其实是大数处理

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0

using namespace std;
char s[35];
int num[35];
int bri[800];
int Left;
int Right;
int calculate(){//返回本次计算的1或0
if(Left>Right)
return -1;
if(Left==Right){
if(num[Left]==1 || num[Left]==0){
int temp=num[Left];
Left++;
return temp;
}
int temp=num[Left]%2;
num[Left]=num[Left]/2;
return temp;
}
int templeft=Left;
for(int i=Left;i<=Right;i++){
if(i==Right && num[i]%2==1)
{
num[i]/=2;
return 1;
}
if(i==Right && num[i]%2==0)
{
num[i]/=2;
return 0;
}
if(i==templeft && num[i]==1)
{
Left++;
num[i+1]+=10;
continue;
}
if(num[i]%2==1){
num[i+1]+=10;
num[i]/=2;
continue;
}
if(num[i]%2==0){
num[i]/=2;
}
}
return 0;
}
void findTheBri(int lenth){
Left=0;
Right=lenth-1;
int count=0;
int temp=0;
while(1){
temp=calculate();
// cout<<temp<<' ';
if(temp==-1)
break;
bri[count]=temp;
count++;
}
for(int i=count-1;i>=0;i--)
cout<<bri[i];
cout<<endl;
}
int main(){
int len;
while(cin>>s){
len=strlen(s);
Left=0;Right=0;
for(int i=0;i<len;i++)
{
num[i]=s[i]-'0';
}
findTheBri(len);
}
}

/**************************************************************
8e88

Problem: 1138
User: zhouyudut
Language: C++
Result: Accepted
Time:290 ms
Memory:1524 kb
****************************************************************/1194题目:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0

using namespace std;
int n;
int eig[20];
int main(){
int count=0;
while(cin>>n){
count=0;
while(1){
if(n<8){
eig[count]=n;
count++;
break;
}
eig[count]=n%8;
count++;
n/=8;
}
for(int i=count-1;i>=0;i--){
cout<<eig[i];
}
cout<<endl;
}

}

/**************************************************************
Problem: 1194
User: zhouyudut
Language: C++
Result: Accepted
Time:110 ms
Memory:1520 kb
****************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: