HDU,hdukmp

來源:互聯網
上載者:User

HDU,hdukmp

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 DescriptionAs 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. 
InputMultiply 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. 
OutputFor each test case, output a single line indicating the answer in B−base(no leading zero). 
Sample Input
2 3221 42333 16abbccd
 
Sample Output
123314
 
SourceBestCoder 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 205char 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;}










聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.