bestcoder#33 1001 高精度模拟

bestcoder#33 1001 高精度模拟

zhx's submissions


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
As one of the most powerful brushes, zhx submits a lot of code on many oj and most of them got AC.
One day, zhx wants to count how many submissions he made on $n$ ojs. He knows that on the $i^{th}$ oj, he made $a_i$ submissions. And what you should do is to add them up.
To make the problem more complex, zhx gives you $n$ $B-base$ numbers and you should also return a $B-base$ number to him.
What's more, zhx is so naive that he doesn't carry a number while adding. That means, his answer to $5 + 6$ in $10-base$ is $1$. And he also asked you to calculate in his way.
 
Input
Multiply test cases(less than $1000$). Seek $EOF$ as the end of the file.
For each test, there are two integers $n$ and $B$ separated by a space. ($1 \leq n \leq 100$, $2 \leq B \leq 36$)
Then come n lines. In each line there is a $B-base$ number(may contain leading zeros). The digits are from $0$ to $9$ then from $a$ to $z$(lowercase). The length of a number will not execeed 200.
 
Output
For each test case, output a single line indicating the answer in $B-base$(no leading zero).
 
Sample Input
2 3
2 2
1 4
233
3 16
ab bc cd
 
Sample Output
1 233 14
题意:对n个B进制数进行不进位的高精度加法,去前导0输出结果
一开始题意没看清楚被case误导了以为要转成十进制输出。。之后又选择用字符串做。。。代码写得很繁琐但没能AC。。。本来稍稍冷静想想就能够短时间内1A的题居然没能AC,看来还是限时的比赛式训练的不多,同时也是因为学算法时刷题拖沓,没想好思路就盲目写代码,做题时不能仔细分析,看来要多增加限时训练,养成冷静的心里素质,同时学算法时要思路清晰
//bestcoder#33_1001
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<ctype.h>

using namespace std;

const int maxn=210;
const int INF=(1<<28);

int n,B;
int a[maxn];
char str[maxn]; //数组定义是需要时间的,因此在循环多次时尽量避免定义在循环里

int getnum(char ch)
{
    if(ch<='9'&&ch>='0') return ch-'0';
    return ch-'a'+10;
}

int main()
{
    while(scanf("%d%d",&n,&B)!=EOF){
        memset(a,0,sizeof(a));
        while(n--){
            scanf("%s",str);
            int len=strlen(str);
            for(int i=0;i<len;i++){
                a[i]=(a[i]+getnum(str[len-1-i]))%B; //按题意模拟,转成整数进行高精度运算
            }
        }
        int len=maxn;
        while(a[len-1]==0&&len>1) len--; //去前导0
        for(int i=len-1;i>=0;i--){ //输出
            if(a[i]<=9) printf("%c",(char)(a[i]+'0'));
            else printf("%c",(char)(a[i]-10+'a'));
        }
        printf("\n");
    }
    return 0;
}
bestcoder#33_1001

 

 
posted @ 2015-03-14 23:54  __560  阅读(278)  评论(0编辑  收藏  举报