Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

大数相加C语言版 #9

Open
InnocentLi opened this issue Apr 23, 2018 · 0 comments
Open

大数相加C语言版 #9

InnocentLi opened this issue Apr 23, 2018 · 0 comments

Comments

@InnocentLi
Copy link
Owner

大数相加C

/*************************************************************************
	> File Name: BigData.c
	> Author: 
	> Mail: 
	> Created Time: 一  4/23 16:20:30 2018
 ************************************************************************/

#include<stdio.h>
#include<string.h>
int main(){
    int sum[200]={0};
    char arr[200];
    while(~scanf("%s",arr)){
        int len = strlen(arr);
        for(int i = len;i>0;i--){
            
            sum[i] += arr[len-i] - '0';
        }
        
        for(int i = 0;i<199;i++){
            sum[i+1]+=sum[i]/10;
            sum[i]=sum[i]%10;
        }
        int lock = 0;

        for(int i = 199;i>0;i--){
           if(sum[i]!=0){
                lock = 1;
            }
            if(lock){
                printf("%d",sum[i]);
            }
        }
        printf("\n");
    }
}

大数相加C语言优化版

#include<stdio.h>
#include<string.h>
int main(){
    int sum[200]={0};
    char arr[200];
    while(~scanf("%s",arr)){
        int len = strlen(arr);
        for(int i = len;i>0;i--){            
           sum[i] += arr[len-i] - '0';
        }
     
    }
    
    for(int i = 0;i<199;i++){
        sum[i+1]+=sum[i]/10;
        sum[i]=sum[i]%10;
    }
    int lock = 0;

    for(int i = 199;i>0;i--){
       if(sum[i]!=0){
            lock = 1;
        }
        if(lock){
            printf("%d",sum[i]);
        }
    }
    printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant