BestCoder Round #33 1001 zhx's submissions

来源:互联网 发布:64位软件 编辑:程序博客网 时间:2024/05/20 13:07

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5186

  • 题意:对给定n个b进制的数求和。但是题目要求在计算和时,如需进位的地方均不进位。

  • 思路: 按照题目要求直接模拟,类似大数加法。对每位的和直接对b取余。特别注意 最后的结果不能输出前导0。

代码如下:

#include <iostream>#include <iomanip>#include <cstdio>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <stack>#include <string>#include <cstring>#include <cassert>using namespace std;typedef long long ll;const int maxn=2222;const int INF=0x7fffffff;const int mod=1e7+7;#define LSON l,m,rt<<1#define RSON m+1,r,rt<<1|1#define ESP 1e-7int n,b;char a[111][222];int get_al(char s) {    return s-'a'+10;}int get_num(char s) {    return s-'0';}char give_al(int z) {    if(z<10) return z+'0';    else return z-10+'a';}char *add(char s1[], char s2[]) {    int x=(int)strlen(s1), y=(int)strlen(s2);    int k=max(x, y);    s1[k--]='\0';    x--,y--;    while(x>=0 && y>=0) {        int p,q;        if(s1[x]>='0' && s1[x]<='9') p=get_num(s1[x]);        else p=get_al(s1[x]);        if(s2[y]>='0' && s2[y]<='9') q=get_num(s2[y]);        else q=get_al(s2[y]);        s1[k--]=give_al((p+q)%b);        x--,y--;    }    while(x>=0)        s1[k--]=s1[x--];    while(y>=0)        s1[k--]=s2[y--];    return s1;}void sum() {    for(int i=1;i<n;i++)        add(a[0], a[i]);    bool tag=false;//判断是否可以输出,避免输出前导0    int len=(int)strlen(a[0]);    for(int i=0;i<len-1;i++) {        if(a[0][i]!='0') tag=true;        if(tag)            printf("%c", a[0][i]);    }    printf("%c\n", a[0][len-1]);}int main() {    while(scanf("%d%d", &n, &b)!=EOF) {        getchar();        for(int i=0;i<n;i++)            scanf("%s", a[i]);        sum();    }    return 0;}
0 0
原创粉丝点击