zoukankan      html  css  js  c++  java
  • oj 1002题 (大数题)

    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
        int q,j,h,k,l;
        int d;
        char s1[1001],s2[1001];//题目要求位数不大于1000 
        scanf("%d",&h);
        for(l=1;l<=h;l++){
            int c[1001]={0}, a[1001]={0},b[1001]={0};//这样可以令数组内全部元素为0 
            scanf("%s %s",&s1,&s2);
            int cd1,cd2,cd3,cdmax,cdmin;
            cd1=strlen(s1);
            cd2=strlen(s2);
            cdmax=cd1>cd2?cd1:cd2;
            for(q=0,j=cd1;q<cd1;q++,j--)
                a[q]=s1[j-1]-48;
            for(q=0,j=cd2;q<cd2;q++,j--)
                b[q]=s2[j-1]-48;
            for(k=0,d=0;k<cdmax;k++)
            {
                c[k]=(a[k]+b[k]+d)%10; //个个位相加满十进一 
                d=(a[k]+b[k]+d)/10;
             } 
             printf("Case %d:
    %s +%s =",l,s1,s2);
             if(d!=0){//判断最后一位和是否大于一 
                 c[cdmax]=1;
                 for(cd3=cdmax;cd3>=0;cd3--){
                     printf("%d",c[cd3]);
                 }
             }
             else{
                 for(cd3=cdmax-1;cd3>=0;cd3--){
                     printf("%d",c[cd3]);
                 }
             }
             if(l!=h)//最后跳一行 
                 printf("
    
    ");
            else 
                printf("
    ");
        }
        return 0;
    }

    题目:

    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.
                            处理                                                                                                   1000以内 ,即使double longlong 也无法满足
    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
    方法步骤:将数字以字符串的方式存储,在单个位相加(用10取余),满十进一(除10),注意初始化(对前几位数赋值,后面直接默认赋0)
  • 相关阅读:
    简易的sniffer程序
    ubuntu 13.10 Rhythmbox不能播放mp3 和中文乱码的问题
    5.单行函数,多行函数,字符函数,数字函数,日期函数,数据类型转换,数字和字符串转换,通用函数(case和decode)
    Java设计模式-单例模式
    WordPress的用户系统总结
    STM8S EEPROM 操作
    简化ui文件转换写法
    大型网站架构之应用服务器集群化
    大型网站架构之分布式缓存
    大型网站架构之JAVA中间件
  • 原文地址:https://www.cnblogs.com/yzbpxx/p/10335855.html
Copyright © 2011-2022 走看看