zoukankan      html  css  js  c++  java
  • A + B Problem II

    Problem Description
    I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
     
    Input
    The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
     
    Output
    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
     
    Sample Input
    2 1 2 112233445566778899 998877665544332211
     
    Sample Output
    Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
     
    代码 如下,为了 省事 ,我用C#写的控制台 程序,并且考虑有点 不太周全 ,只考虑到了A数字长度 大于等于B数字的情况。
     
    using System;
    using System.Text;
    
    namespace TestSum
    {
        class Program
        {
            static void Main(string[] args)
            {
                long a = 112233445566778899;
                long b = 998877665544332211;
    
                //long a = 123;
                //long b = 29;
    
                char[] ac = a.ToString().ToCharArray();
                char[] bc = b.ToString().ToCharArray();
                StringBuilder result = new StringBuilder();
                if(ac.Length>=bc.Length)
                {
    
                    int temp = 0;
    
                    for (int i = ac.Length - 1, j = bc.Length-1; i>=0; i--,j--)
                    {
                        
                        int indexValue = 0;
    
                        if (temp == 1)
                        {
                            indexValue++;
                        }
                        int _a = 0, _b = 0;
                        if(j<0)
                        {
                            _b = 0;
                            _a = int.Parse(ac[i].ToString());
                        }
                         else
                        {
                            _a = int.Parse(ac[i].ToString());
                            _b = int.Parse(bc[j].ToString());
                        }
         
                        if(_a+_b>9)
                        {
                            indexValue+= ((_a + _b-10 ));
                            temp = 1;
                        }
                        else
                        {
                            indexValue+= (_a + _b);
                            temp = 0;
                        }
    
                        
    
                        result.Append(indexValue);
                    }
                }
    
              reverse(result.ToString());
                Console.ReadKey();
    
            
            }
    
           static void reverse(string result)
            {
                for(int i =result.Length-1;i>=0;i--)
                {
                    Console.Write(result[i]);
                }
            }
        }
    }
    

      

  • 相关阅读:
    php基础之简单运算
    选择平淡
    php基础之控制结构
    关于三元运算符的初步应用及理解
    VS2015 遇到异常。这可能是由某个扩展导致的
    C#中如何去除窗体默认的关闭按钮
    (转载)SQL基础--> 约束(CONSTRAINT)
    SQL Server安装后设置SQL Server验证登录
    附加数据库 对于 ""失败,无法打开物理文件 操作系统错误 5:拒绝访问 SQL Sever
    SQL Server数据库操作(二)
  • 原文地址:https://www.cnblogs.com/kmsfan/p/7471405.html
Copyright © 2011-2022 走看看