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

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);
  }
}
 
/**************************************************************
    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
****************************************************************/






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值