zoukankan      html  css  js  c++  java
  • HDU

    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 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. (1n1002B36)
    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
     






    思路:就是不进位的大数相加啦,要注意当结果为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("
    ");
    }
    
    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("
    ");
    }
    
    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("
    ");
    	}
    	return 0;
    }










    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    数据可视化之分析篇(三)Power BI总计行错误,这个技巧一定要掌握
    数据可视化之分析篇(二)Power BI 数据分析:客户购买频次分布
    数据可视化之分析篇(一)使用Power BI进行动态帕累托分析
    机器学习实战基础(三):创建线性回归器
    数据可视化实例(十五):有序条形图(matplotlib,pandas)
    scrapy 源码解析 (五):启动流程源码分析(五) Scraper刮取器
    4sum, 4sum closest
    3sum, 3sum closest
    两数之和-数据结构设计 · Two Sum
    (hash map)Two Sum, sorted(排序+双指针)closest,小于或大于的对数,组成不同的对数
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4710231.html
Copyright © 2011-2022 走看看