现在的位置: 首页 > 综合 > 正文

HDU 5186 zhx’s submissions 模拟水题

2018年04月21日 ⁄ 综合 ⁄ 共 1101字 ⁄ 字号 评论关闭

题意就是给n个b进制的数,求它们相加的和,有一个条件就是相加不进位,输出结果为没有前导0.

(原来有中文题的。只能说英文太渣没有看到输出结果没有前导0这个条件。)

代码:

//author: CHC
//First Edit Time:    2015-03-14 19:09
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=1e+4;
const int MAXM=1e+5;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
int getnums(char ch){
    if(ch>='0'&&ch<='9')return ch-'0';
    else return 10+ch-'a';
}
char getchar(int num){
    if(num>=0&&num<=9)return num+'0';
    else return num-10+'a';
}
char strs[200][300];
char tmp[300];
char ans[300];
int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        memset(strs,0,sizeof(strs));
        for(int i=0;i<n;i++){
            scanf("%s",tmp);
            for(int j=strlen(tmp)-1,k=0;j>=0;j--,k++){
                strs[i][k]=tmp[j];
            }
        }
        memset(ans,'0',sizeof(ans));
        int m_len=0;
        for(int i=0;i<n;i++){
            int len=strlen(strs[i]);
            m_len=max(len,m_len);
            for(int j=0;j<len;j++){
                ans[j]=getchar((getnums(ans[j])+getnums(strs[i][j]))%k);
            }
        }
        int flag=m_len;
        for(;flag>0&&ans[flag]=='0';flag--);
        for(int i=flag;i>=0;i--)putchar(ans[i]);
        puts("");
    }
    return 0;
}

抱歉!评论已关闭.