hdu 5186(模拟)

zhx's submissions

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


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 ith oj, he made ai submissions. And what you should do is to add them up.
To make the problem more complex, zhx gives you n Bbase numbers and you should also return a Bbase 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 10base 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. (1n100, 2B36)
Then come n lines. In each line there is a Bbase 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 Bbase(no leading zero).
 

 

Sample Input
2 3 2 2 1 4 233 3 16 ab bc cd
 

 

Sample Output
1 233 14
 

 

Source
 
题意:给出n个字符串,这些字符串都是b进制的,将这些字符串相加,但是我们得到的结果是不需要进位的,问最后得到的结果是多少??
例:
3 16
ab
bc
cd
ab+bc = 57
57+cd = 14
题解:我的方法是先把字符串全部记下来,然后翻转,依照这些字符串中最长的那个进行补 0 ,然后依次相加,有进位时就减掉进制,记得判断负数,是负数就要进行取模变成正的,最后得到结果串记得翻转回来。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        char res[205];
        char str[105][205];
        int len[105];
        int MAX=-1;
        for(int i=0;i<n;i++){
            scanf("%s",str[i]);
            len[i] = strlen(str[i]);
            reverse(str[i],str[i]+len[i]);
            MAX = max(MAX,len[i]);
        }
        for(int i=0;i<n;i++){
            for(int j=len[i];j<MAX;j++){
                str[i][j] = '0';
            }
            if(i==0){
                for(int j=0;j<MAX;j++){
                    res[j] = str[0][j];
                }
            }
        }
        int a,b;
        for(int i=1;i<n;i++){
            for(int j=0;j<MAX;j++){
                if(str[i][j]<='9'&&str[i][j]>='0') a = str[i][j]-'0';
                else a = str[i][j]-'a'+10;
                if(res[j]<='9'&&res[j]>='0') b = res[j]-'0';
                else b = res[j]-'a'+10;
                int c = ((a+b-m)%m+m)%m;
                if(c>=0&&c<=9) res[j] = c+'0';
                else res[j] = c-10+'a';
            }
        }
        reverse(res,res+MAX);
        bool flag = false;
        for(int i=0;i<MAX-1;i++){
            if(res[i]!='0'){
                flag = true;
            }
            if(flag){
                printf("%c",res[i]);
            }
        }
        printf("%c\n",res[MAX-1]);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5694053.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值