zoukankan      html  css  js  c++  java
  • csu1002 A+B(III)

    题目描述:

    There are multiple test cases. Each test case contains only one line. Each line consists of a pair of integers a and b1=< a,b <=1016, separated by a space. Input is followed by a single line with a = 0, b = 0, which should not be processed.

    For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

    RE : 数组开的不够大

    看到有的代码只有114B(我的超1000B),不解。

    // csuoj 1002 : A+B(III)
    # include <stdio.h>
    # include <string.h>
    # define MAXN 20 // RE: # define MAXN 17
    char a[MAXN], b[MAXN], ans[MAXN];
    void add(char *a, char *b, char *ans); //ans=a+b
    void strnrev(char *a);
    int main()
    {
    while(2==scanf("%s%s", a,b))
    {
    if(1==strlen(a) && 1==strlen(b) && a[0]=='0' && b[0]=='0') return ;
    strnrev(a);
    strnrev(b);
    add(a, b, ans);
    strnrev(ans);
    printf("%s\n", ans);
    }
    return 0;
    }
    void add(char *a, char *b, char *ans)
    {
    char *p, *q;
    int i, j, tmp, c;
    if(strlen(a)>strlen(b)) {p=a;q=b;}
    else {p=b;q=a;}
    i = c = 0;
    while (q[i] != '\0')
    {
    tmp = (q[i]-'0')+(p[i]-'0')+c;
    ans[i] = tmp%10+'0';
    c = tmp/10;
    ++i;
    }
    while (p[i] != '\0')
    {
    tmp = (p[i]-'0')+c;
    ans[i] = tmp%10+'0';
    c = tmp/10;
    ++i;
    }
    if(c==1) ans[i++] = c+'0';
    ans[i] = '\0';
    }
    void strnrev(char *a)
    {
    int i, len;
    char tmp;
    len = strlen(a);
    for (i = 0;i < len>>1; ++i)
    {
    tmp = a[i];
    a[i] = a[len-i-1];
    a[len-i-1] = tmp;
    }
    }
  • 相关阅读:
    Leetcode#179 Largest Number
    Leetcode#155 Min Stack
    Leetcode#14 Longest Common Prefix
    Leetcode#101 Symmetric Tree
    Leetcode#172 Fractorial Trailing Zero
    Leetcode#28 Implement strStr()
    Leetcode#46 Permutations
    Leetcode#48 Rotate Image
    Leetcode#134 Gas station
    Leetcode#137 Single Number II
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2354495.html
Copyright © 2011-2022 走看看