zoukankan      html  css  js  c++  java
  • Integer Inquiry

    Integer Inquiry
    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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<stdio.h>
     2 #include<string.h>
     3 
     4 char a[105][200] ;
     5 char sum[200] ;
     6 
     7 void add (char temp[] ) {
     8 
     9     int rem = 0;
    10     int k ;
    11     int i ;
    12     for (i = 0 ; temp[i] != '' ; i++ ) {
    13         if ( ( k = temp[i] - '0' + sum[i] - '0' + rem ) >= 10 ) {
    14             rem = 1 ;
    15             sum[i] = '0' + k - 10;
    16         }
    17         else {
    18             rem = 0 ;
    19             sum[i] = '0' + k ;
    20         }
    21     }
    22     if ( rem == 1)
    23         sum[i] = '1' ;
    24     for ( k = strlen (sum) - 1 ; sum[k] == 0 && k >= 0; k-- ) ;
    25     if ( k == -1)
    26         sum[k + 2] = '' ;
    27     else
    28         sum[k + 1] = '' ;
    29 
    30 }
    31 
    32 int main () {
    33    // freopen ("a.txt" ,"r" , stdin ) ;
    34     int n ;
    35     int ls , li ;
    36     int j ;
    37     while ( gets(a[0]) != NULL ) {
    38         n = 1 ;
    39         while ( strcmp(a[n - 1] , "0") != 0 )  {
    40             gets (a[n++]) ;
    41         }
    42         strcpy (sum , a[0] ) ;
    43         strrev (sum) ;
    44 
    45         for (int i = 1 ; i < n; i++ ) {
    46             li = strlen (a[i]) ;
    47             ls = strlen (sum) ;
    48             strrev (a[i]) ;
    49             if ( li > ls ) {
    50                  for (j = ls ; j < li ; j++ ) {
    51                     sum[j] = '0' ;
    52                  }
    53                 sum[j] = '' ;
    54             }
    55             else {
    56                 for (j = li ; j < ls ; j++ ) {
    57                     a[i][j] = '0' ;
    58                  }
    59                 a[i][j] = '' ;
    60             }
    61             add ( a[i] ) ;
    62         }
    63         strrev (sum) ;
    64         puts (sum) ;
    65     }
    66     return 0;
    67 }
    试试 1 + 9 或 直接输入 0
  • 相关阅读:
    ADO.NET入门教程(五) 细说数据库连接池
    Delphi下使用指针的简单总结
    Delphi
    Delphi
    Delphi
    TXLSReadWriteII5 单元格读写
    [Delphi]Delphi开发的一些技巧
    TXLSReadWriteII2版本导出Excel文件:
    Tomcat使用startup启动,一闪而过,如何查看出错信息
    【转】Java保留固定小数位的4种方法
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4272280.html
Copyright © 2011-2022 走看看