• UVA 424 (13.08.02)


     Integer Inquiry 

    One of the first users of BIT's new supercomputer was Chip Diller. Heextended his explorationof powers of 3 to go from 0 to 333 and he explored taking various sumsof those numbers.

    ``This supercomputer is great,'' remarked Chip. ``I only wish Timothy werehere to see theseresults.'' (Chip moved to a new apartment, once one became available onthe third floor of theLemon Sky apartments on Third Street.)

    Input

    The input will consist of at most 100 lines of text, each of whichcontains a single VeryLongInteger.Each VeryLongInteger will be 100 or fewer characters in length, and willonly contain digits (no VeryLongInteger will be negative).

    The final input line will contain a single zero on a line by itself.

    Output

    Your program should output the sum of the VeryLongIntegers given in the input.

    Sample Input

    123456789012345678901234567890
    123456789012345678901234567890
    123456789012345678901234567890
    0

    Sample Output

    370370367037037036703703703670
    
    

    题意: 大数相加~ 做法: 看AC的代码, 顺带注释, 容易懂的~ 另注:写了两份, 第一份AC之后发现代码不完美, 999999999999999 + 1计算不来 虽然AC了, 但是是由于黑盒子数据太弱了, 于是又重写了一份, 终于可计算999999999999999 + 1了 原因出在: 一个是每次加完都判断进位, 导致没有考虑到进位的连锁反应, 如999999999999999 + 1, 会一直进位 所以, 后来改成了全部累加完后, 统一进行进位, 就无懈可击了~ AC代码(无懈可击版, 样例999999999999999 + 1可行):

    #include<stdio.h>
    #include<string.h>
    
    int ans[1001];
    
    int main() {
    	char num[101];
    	char ch;
    	int len;
    	//初始化答案数组~
    	for(int i = 0; i < 1001; i++)
    		ans[i] = 0;
    
    	while(gets(num) != NULL) {
    		//结束并输出的标志
    		if(num[0] == '0')
    			break;
    		len = strlen(num);
    		//num数组倒序
    		for(int i = 0; i < len/2; i++) {
    			ch = num[i];
    			num[i] = num[len - 1 - i];
    			num[len - 1 - i] = ch;
    		}
    		//先累加~
    		for(int i = 0; i < len; i++)
    			ans[i] += (num[i] - '0');
    	}
    	//跳出后, 把累加的数值进行处理, 该进位的进~
    	for(int i = 0; i <= 999; i++) {
    		ans[i+1] += ans[i] / 10;
    		ans[i] = ans[i] % 10;
    	}
    	//从末尾开始找, 找到第一个非零数位置
    	int pos;
    	for(int i = 1000; i >= 0; i--) {
    		if(ans[i] != 0) {
    			pos = i;
    			break;
    		}
    	}
    	//从该位置倒序输出~
    	for(int i = pos; i >= 0; i--)
    		printf("%d", ans[i]);
    	printf("
    ");
    	return 0;
    }
    


    AC代码(有漏洞版, 很久以前写的, 比较搓, 样例999999999999999 + 1不可通过)

    #include <stdio.h>
    #include <string.h>
    
    int main(){
    
    	int i, t, l, n;
    	int Sum, sum[1001];
    	char add[101];
    	for (i = 0; i < 1001; i++)
    		sum[i] = 0;
    	while (gets(add)){		
    	
    		if (strcmp(add,"0") == 0)
    			break;
    
    		l = strlen(add);
    
    		//add字符数组的倒序:
    		for (i = 0; i < l/2; i++){
    			t = add[i];
    			add[i] = add[l-i-1];
    			add[l-i-1] = t;
    		}
    		//累加到sum数组:
    		for (i = 0; i < l; i++){
    			Sum = add[i]-'0' + sum[i];
    			sum[i+1] = Sum / 10 + sum[i+1];
    			sum[i] = Sum % 10;
    		}
    	}
    
    	//处理一:对sum数组从最后面开始寻找第一个非零数,标记
    	for (i = 1000; i >= 0; i--){
    		if (sum[i] != 0){
    			n = i;
    			break;
    		}	
    	}		
    	
    	//处理二:倒序输出~(yes)
    	for (i = n; i >= 0; i--)
    		printf("%d",sum[i]);
    	printf("
    ");
    	return 0;
    }
    
    
  • 相关阅读:
    A1066 Root of AVL Tree (25 分)
    A1099 Build A Binary Search Tree (30 分)
    A1043 Is It a Binary Search Tree (25 分) ——PA, 24/25, 先记录思路
    A1079; A1090; A1004:一般树遍历
    A1053 Path of Equal Weight (30 分)
    A1086 Tree Traversals Again (25 分)
    A1020 Tree Traversals (25 分)
    A1091 Acute Stroke (30 分)
    A1103 Integer Factorization (30 分)
    A1032 Sharing (25 分)
  • 原文地址:https://www.cnblogs.com/riskyer/p/3233858.html
Copyright © 2020-2023  润新知