spoj question

http://www.spoj.com/problems/JULKA/help me solve this question please .explain why we are adding zero in end after addition of bits.as commented in code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77


#include <stdio.h>
#include <string.h>

#define MAX 111

char klaudia[MAX], natalia[MAX], total[MAX], diff[MAX];

void calc()
{
    int len1 = strlen(total);
    int len2 = strlen(diff);
    int a, b, c, i, j, k, f;
    char temp[MAX];

    for(i=len1-1, j=len2-1, k=c=0; i>=0 || j>=0 || c; i--, j--, k++)
    {
        a = i>=0? total[i]-'0' : 0;
        b = j>=0? diff[j]-'0' : 0;
        temp[k] = (a+b+c)%10 + '0';
        c = (a+b+c)/10;
    }
    temp[k] = 0;//explain

    strcpy(klaudia,"0");
    for(i=k-1, j=a=f=0; i>=0; i--)
    {
        b = (a*10 + temp[i]-'0') / 2;
        a = (a*10 + temp[i]-'0') % 2;
        if(b) f = 1;
        if(f) klaudia[j++] = b+'0';
    }
    if(!j) j++;
    klaudia[j] = 0;

    for(i=len1-1, j=len2-1, k=c=0; i>=0; i--, j--, k++)
    {
        a = total[i]-'0';
        b = j>=0? diff[j]-'0' : 0;
        if(a < b+c)
        {
            temp[k] = (10+a-b-c) + '0';
            c = 1;
        }
        else
        {
            temp[k] = a-b-c + '0';
            c = 0;
        }
    }
    temp[k] = 0;

        strcpy(natalia,"0");
        for(i=k-1, j=a=f=0; i>=0; i--)
        {
                b = (a*10 + temp[i]-'0') / 2;
                a = (a*10 + temp[i]-'0') % 2;
                if(b) f = 1;
                if(f) natalia[j++] = b+'0';
        }
        if(!j) j++;
        natalia[j] = 0;
}

int main()
{
    while(scanf("%s %s", total, diff)==2)
    {
        calc();
        printf("%s\n%s\n", klaudia, natalia);
    }
    return 0;
}


  
Topic archived. No new replies allowed.