【ACM】Martian Addition

Time Limit: 1 Second      Memory Limit: 32768 KB

--------------------------------------------------------------------------------
  In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit numbers, and the winner is the one who uses least time. This year they also invite people on Earth to join the contest.
  As the only delegate of Earth, you're sent to Mars to demonstrate the power of mankind. Fortunately you have taken your laptop computer with you which can help you do the job quickly. Now the remaining problem is only to write a short program to calculate the sum of 2 given numbers. However, before you begin to program, you remember that the Martians use a 20-based number system as they usually have 20 fingers.

Input:
You're given several pairs of Martian numbers, each number on a line.
Martian number consists of digits from 0 to 9, and lower case letters from a to j (lower case letters starting from a to present 10, 11, ..., 19).
The length of the given number is never greater than 100.

Output:
For each pair of numbers, write the sum of the 2 numbers in a single line.

Sample Input:

1234567890
abcdefghij
99999jjjjj
9999900001


Sample Output:

bdfi02467j
iiiij00000

 

********************************俺是源代码分割线****************************************
#include<iostream>
using namespace std;
#include <windows.h>

 

typedef int Status;//不习惯用bool,数据结构学了后就爱自定义= =|||
#define OK       1
#define ERROR 0

const int N=101;//两个100位数字相加,最多溢出一位;如果是100个十位相加则溢出12位——昨天晚上手工算的|||

class Twenty_Based
{//20进制类
public:
 Twenty_Based(){};
 Twenty_Based(Twenty_Based &t);
 ~Twenty_Based(){};
 Status ADD(int* num,int len,int flag);
 void GetNum();
 void Check();
private:
 int sum[N];
};

 

Status Twenty_Based::ADD(int* num,int len,int flag)
{//累加,反正就用两次,第一次直接赋值,第二次就累加

  //要是多次累加的话,就把构造函数写了,再把这里的flag去掉就OK
 int j(N-1);
 if(!flag)
 {
  for(int i=len-1;i>=0;i--,j--)
  {
   sum[j]=*(num+i);
  }
  while(j>=0){ sum[j]=0; j--; }
//没有到101位,高位取0,谁叫我懒得在一开始初始化
  return OK;
 }
 else
 {
  for(int i=len-1;i>=0;i--,j--)
  {
   sum[j]+=*(num+i);
  }
  Check();//十进制的每一位转化为二十进制,不过感觉没有根本上解决问题啊……

               //果然还是应该在一开始就转化为二十进制再相加处理比较好吗?
  return OK;
 }
}

 

void Twenty_Based::Check()
{
 for(int j=N-1;j>=0;j--)
 {
  int flag(1);
  while(flag)
  {
   if(sum[j]>=20)
   {
    sum[j]-=20; sum[j-1]++; flag=1;
//超过二十就进位
   }
   else if(sum[j]>9)
   {
    sum[j]+=87; flag=0;
//'a'=97
   }
   else
   {
    sum[j]+=48; flag=0;
//'0'=48
   }

  }//这是为了输出时直接转化为char型而服务的
 }
}

 

void Twenty_Based::GetNum()
{
 int i=0;
 while(sum[i]=='0') i++;
//高位的'0'不输出
 while(i<=N-1)
 {
  cout<<char(sum[i]);
  i++;
 }
 cout<<endl;
}

 

int Check( char ch )
{
//最后还是把这孩子弄成global了……
 if(ch>='0' && ch<='9') return ch-=48;
 else if(ch>='a' && ch<='j') return ch-=87;
}

 

int main()
{

///计时
 long int time_p1,time_p2;
 SYSTEMTIME sys;
 GetLocalTime( &sys );
 time_p1=sys.wMilliseconds;


 Twenty_Based t;
 /*int b[N-1]={10,11,12,13,14,15,16,17,18,19};
 int c[N-1]={9,8,7,6,5,4,3,2,1,1};
 int count(10);*/                //这段是当初测试的用例来着


 char ch;
 int count=0,temp[N-1];

 

 cout<<"please input the first num:/t";
 ch=getchar();
 while(ch!='/n' && count<N-1)
 {
  temp[count]=Check(ch);
  count++;
  ch=getchar();
 }
 temp[count]=NULL;
 t.ADD(temp,count,0);

 fflush(stdin); count=0;
 cout<<"please input the second num:/t";
 ch=getchar();
 while(ch!='/n' && count<N-1)
 {
  temp[count]=Check(ch);
  count++;
  ch=getchar();
 }
 temp[count]=NULL;
 t.ADD(temp,count,1);
 t.GetNum();

 

///计时

 GetLocalTime( &sys );
 time_p2=sys.wMilliseconds;

 cout<<time_p2-time_p1<<endl;

 return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值