zoukankan      html  css  js  c++  java
  • 【原】 POJ 1503 Integer Inquiry 大整数加法 解题报告

    http://poj.org/problem?id=1503

    方法:
    很简单,模拟手动加法过程, 结果中的前置0要消去

    Description

    One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. 
    ``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)

    Input

    The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only 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

       1: #include <iostream>
       2: #include <string>
       3: #include <fstream>
       4:  
       5: using namespace std ;
       6:  
       7: string sum_bignum(const string& lhs,const string& rhs)
       8: {
       9:     int len1 = lhs.size() ;
      10:     int len2 = rhs.size() ;
      11:     int lenResult = len1>len2 ? len1+1 : len2+1 ;
      12:  
      13:     int i ;
      14:     int lval,rval,carry = 0 ;
      15:     int tmpresult ;
      16:     int lhsEnd = len1-1 ;
      17:     int rhsEnd = len2-1 ;
      18:     int resultEnd = lenResult-1 ;
      19:     string result(lenResult,'0') ;
      20:     for( i=resultEnd ; i>=0 ; --i )
      21:     {
      22:         //*** 如果不判断直接赋值,则会出错,例如999+9
      23:         //对结果的每一位都进行计算***
      24:         if(lhsEnd>=0)
      25:             lval = lhs[lhsEnd--] - '0' ;
      26:         else
      27:             lval = 0 ;
      28:  
      29:         if(rhsEnd>=0)
      30:             rval = rhs[rhsEnd--] - '0' ;
      31:         else
      32:             rval = 0 ;
      33:  
      34:         tmpresult = lval+rval+carry ;
      35:         result[i] = tmpresult%10 + '0' ;
      36:         carry = tmpresult/10 ;
      37:     }
      38:  
      39:     return result.substr( result.find_first_not_of('0') ) ;
      40: }
      41:  
      42: void run1503()
      43: {
      44:     ifstream in("in.txt") ;
      45:     string lhs,rhs ;
      46:     getline(in,lhs);
      47:     while(getline(in,rhs) && rhs!="0")
      48:         lhs = sum_bignum(lhs,rhs);
      49:     cout<<lhs<<endl;
      50: }
  • 相关阅读:
    mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
    jQuery实现判断li的个数从而实现其他功能
    移动平台对 meta 标签的定义
    移动平台对 meta 标签的定义
    HTML5手机网站开发页面宽度解决方案
    HTML5手机网站开发页面宽度解决方案
    别做“不会思考的码农”
    别做“不会思考的码农”
    myeclipse+spket1.6+Extjs4.2开发环境搭建
    myeclipse+spket1.6+Extjs4.2开发环境搭建
  • 原文地址:https://www.cnblogs.com/allensun/p/1870081.html
Copyright © 2011-2022 走看看