HDU - 5186 - zhx's submissions (大数高精度)

zhx‘s submissions

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 540    Accepted Submission(s): 146

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 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≤n≤100, 2≤B≤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

Source

BestCoder Round #33

思路:就是不进位的大数相加啦,要注意当结果为0时输出一个0,之前我还做过一个差不多的,上次注意到了,,这次居然没注意到o(╯□╰)o.........

疑问:为何运行时间900多ms,而且还可能会T,把cstdio改为stdio.h时间就降下来了,直接变为100多ms,害的我还检查半天。。。但是这是为什么??????

搞了半天我发现使用g++环境提交的没过,而用c++环境就过啦(以后再HDU做题还是用c++环境吧,醉啦)

据说g++用scanf因为输入太慢而要开挂(难道和cin减速一个性质??),,,,貌似是这样的,以后再试试

void gn(int &x){
    char c;while((c=getchar())<'0'||c>'9');x=c-'0';
    while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';
}

AC代码①(100+ms,g++环境):

#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

char ans[205];
char t[205];

void fun(char ans[], char t[]) {
    int len = strlen(t);
    for(int i = 0; i < len; i++) {
        ans[i] = t[len - 1 - i];
    }
}

void swap(char t[]) {
    int len = strlen(t);
    for(int i = 0; i < len / 2; i++) {
        char m = t[i];
        t[i] = t[len - 1 - i];
        t[len - 1 - i] = m;
    }
}

void add(char ans[], char t[], int B) {
    int t1, t2, t3;
    int len = strlen(t);
    for(int i = 0; i < len; i++) {
        if(ans[i] <= 'z' && ans[i] >= 'a') t1 = (int)(ans[i] - 'a' + 10);
        else t1 = ans[i] - '0';
        if(t[i] <= 'z' && t[i] >= 'a') t2 = (int)(t[i] - 'a' + 10);
        else t2 = t[i] - '0';
        t3 = (t1 + t2) % B;
        if(t3 >= 10) ans[i] = (char)(t3 - 10 + 'a');
        else ans[i] = (char)(t3 + '0');
    }
}

void print(char ans[]) {
    int flag = 0, p;
    for(int i = 204; i >= 0; i--) {
        if(ans[i] != '0') {
            printf("%c", ans[i]);
            flag = 1;
        }
        else if(ans[i] == '0' && flag) printf("0");
    }
    if(flag == 0) printf("0");
    printf("\n");
}

int main() {
    int n, B;
    while(scanf("%d %d", &n, &B) != EOF) {
        for(int i = 0; i< 205; i++) ans[i] = '0';

        scanf("%s", t);
        fun(ans, t);
        for(int i = 0; i < n-1; i++) {
            scanf("%s", t);
            swap(t);
            add(ans, t, B);
        }
        print(ans);
    }
    return 0;
}

代码②(900+ms or TLE,g++环境):

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

char ans[205];
char t[205];

void fun(char ans[], char t[]) {
    int len = strlen(t);
    for(int i = 0; i < len; i++) {
        ans[i] = t[len - 1 - i];
    }
}

void swap(char t[]) {
    int len = strlen(t);
    for(int i = 0; i < len / 2; i++) {
        char m = t[i];
        t[i] = t[len - 1 - i];
        t[len - 1 - i] = m;
    }
}

void add(char ans[], char t[], int B) {
    int t1, t2, t3;
    int len = strlen(t);
    for(int i = 0; i < len; i++) {
        if(ans[i] <= 'z' && ans[i] >= 'a') t1 = (int)(ans[i] - 'a' + 10);
        else t1 = ans[i] - '0';
        if(t[i] <= 'z' && t[i] >= 'a') t2 = (int)(t[i] - 'a' + 10);
        else t2 = t[i] - '0';
        t3 = (t1 + t2) % B;
        if(t3 >= 10) ans[i] = (char)(t3 - 10 + 'a');
        else ans[i] = (char)(t3 + '0');
    }
}

void print(char ans[]) {
    int flag = 0, p;
    for(int i = 204; i >= 0; i--) {
        if(ans[i] != '0') {
            printf("%c", ans[i]);
            flag = 1;
        }
        else if(ans[i] == '0' && flag) printf("0");
    }
    if(flag == 0) printf("0");
    printf("\n");
}

int main() {
    int n, B;
    while(scanf("%d %d", &n, &B) != EOF) {
        for(int i = 0; i< 205; i++) ans[i] = '0';

        scanf("%s", t);
        fun(ans, t);
        for(int i = 0; i < n-1; i++) {
            scanf("%s", t);
            swap(t);
            add(ans, t, B);
        }
        print(ans);
    }
    return 0;
}

AC代码③:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define maxn 205
char tmp[maxn][maxn], ans[maxn][maxn], ch[50];
int to[maxn];

void init() {
	memset(ch, 0, sizeof(ch));
	memset(to, 0, sizeof(to));
	for(int i = 0; i <= 35; i++) {
		if(i <= 9) ch[i] = i + '0', to[i + '0'] = i;
		else ch[i] = i - 10 + 'a', to[i - 10 + 'a'] = i;
	}
}

int main() {
	int n, B;
	init();
	while(~scanf("%d %d", &n, &B)) {
		memset(ans, 0, sizeof(ans));
		memset(tmp, 0, sizeof(tmp));

		for(int i = 1; i <= n; i++) {
			scanf("%s", tmp[i]);
			int len = strlen(tmp[i]);
			for(int j = 0; j < len; j++) {
				ans[i][j] = tmp[i][len-1-j];
			}
		}

		int flag = 0;
		for(int i = maxn - 1; i >= 0; i--) {
			int t = 0;
			for(int j = 1; j <= n; j++) {
				t += to[ans[j][i]];
			}
			t %= B;
			if(t) flag = 1;
			if(flag) printf("%c", ch[t]);
		}
		if(!flag) printf("0");
		printf("\n");
	}
	return 0;
}

HDU - 5186 - zhx's submissions (大数高精度)

时间: 2024-10-15 04:33:08

HDU - 5186 - zhx's submissions (大数高精度)的相关文章

HDU 5186 zhx&#39;s submissions 模拟,细节 难度:1

http://acm.hdu.edu.cn/showproblem.php?pid=5186 题意是分别对每一位做b进制加法,但是不要进位 模拟,注意:1 去掉前置0 2 当结果为0时输出0,而不是全部去掉 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=101; const int maxm=201; int n,b; char

HDU 5186 zhx&#39;s submissions (进制转换)

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 yo

杭电 HDU ACM 5186 zhx&#39;s submissions

zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1892    Accepted Submission(s): 507 Problem Description As one of the most powerful brushes, zhx submits a lot of code on many

HDU - 1047 - Integer Inquiry (大数高精度)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13969    Accepted Submission(s): 3523 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He e

HDU 4927 Series 1(推理+大数)

HDU 4927 Series 1 题目链接 题意:给定一个序列,要求不断求差值序列,直到剩一个,输出这个数字 思路:由于有高精度一步,所以要推理一下公式,其实纸上模拟一下很容易推出公式就是一个类似杨辉三角的组合数求和,不过奇数位置是加,偶数位置是减,然后高精度过掉 代码: 本人的第一个JAVA程序^ ^ import java.util.Scanner; import java.math.BigInteger; public class Main { public static void ma

hdu 4873 ZCC Loves Intersection(大数+概率)

题目链接:hdu 4873 ZCC Loves Intersection 题目大意:给出N,D,表示在一个D维的坐标上,坐标范围为0~N-1.在这个坐标系中有D条线段,分别平行与各个坐标轴,每秒会根据题目中的伪代码随机生成各个线段的位置.两条直线相交的话会得一分,问每秒得分的期望. 解题思路:总的情况(ND?1?C(2N))D,两条直线相交的得分C(2D)?s2?ND?2?(ND?2?C(2N))D?2 s是在二维情况下的分的情况数s=∑i=1N((N?i+1) i?1)=N3+3?N2?4?N

BestCoder #33 zhx&#39;s submissions

解题报告不写了,光贴个代码..主要是注意各个符号,然后字符串的变换等等. #include <iostream> #include <stdio.h> #include <string.h> #include <vector> #include <map> #include <algorithm> #include <queue> #include <cmath> #include <bitset>

zhx&#39;s submissions

问题描述 作为史上最强的刷子之一,zhx在各大oj上交了很多份代码,而且多数都AC了. 有一天,zhx想数一数他在n个oj上一共交了多少份代码.他现在已经统计出在第i个oj上,他交了ai份代码.而把它们加起来就是你的工作了. 当然zhx是一个不走寻常路的人,所以他的数字都是用B进制表示的.而他也要求你告诉他B进制的数. 但是zhx有一个恶趣味的习惯,他算加法的时候从来不进位.比如他算十进制5+6的答案是1.而且他还要求你也要按照他的方式来做加法. 输入描述 多组数据(不超过1000组).读到文件

hdu4927 Series 1(组合+公式 Java大数高精度运算)

题目链接: Series 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 423    Accepted Submission(s): 146 Problem Description Let A be an integral series {A1, A2, . . . , An}. The zero-order series o