Digit sums problem

Hello, I was solving a problem that is as follows:

When Grace was in third grade, her elementary school teacher assigned her the following problem:
What is the smallest possible sum of two numbers that together use the numerals 1, 2, 7, 8,
and 9?

Grace figured out that the answer to this problem is 207 (for example, as 78 + 129), but when the
teacher assigned four pages of similar problems as homework, Grace got bored. It turns out that Grace
was a rather advanced third grader, so she decided that it would be more fun to write a computer
program to solve such problems. Surely you can do the same!


Input
Each problem is described on a single line. The line begins with an integer N, such that 2 <= N <= 14,
designating the number of numerals included in the problem. Following that are those N numerals.
There will always be at least 2 numerals that are nonzero. The end of the input is designated by a line
containing only the value ‘0’.


Output
For each case, output a line with the minimum sum S that can be achieved. Please keep in mind that
by standard convention, the numeral ‘0’ cannot appear as the first digit of either summand.


Sample Input
5 1 2 7 8 9
6 3 4 2 2 2 2
9 0 1 2 3 4 0 1 2 3
0

Sample Output
207
447
11257



So I managed to get through it like 95% and the only thing missing in my code is to handle the 0 cases, like the 3rd input example it has 0s and my code still can't deal with 0s so here it is:

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
    ACM-ICPC Live archive.
    
    6497-Digit Sum
    
    When Grace was in third grade, her elementary school teacher assigned her the following problem:
    What is the smallest possible sum of two numbers that together use the numerals 1, 2, 7, 8,
    and 9?
    
    Grace figured out that the answer to this problem is 207 (for example, as 78 + 129), but when the
    teacher assigned four pages of similar problems as homework, Grace got bored. It turns out that Grace
    was a rather advanced third grader, so she decided that it would be more fun to write a computer
    program to solve such problems. Surely you can do the same!
    
    
    Input
    Each problem is described on a single line. The line begins with an integer N, such that 2 <= N <= 14,
    designating the number of numerals included in the problem. Following that are those N numerals.
    There will always be at least 2 numerals that are nonzero. The end of the input is designated by a line
    containing only the value ‘0’.
    
    
    Output
    For each case, output a line with the minimum sum S that can be achieved. Please keep in mind that
    by standard convention, the numeral ‘0’ cannot appear as the first digit of either summand.
    
    
    Sample Input
    5 1 2 7 8 9
    6 3 4 2 2 2 2
    9 0 1 2 3 4 0 1 2 3
    0
    
    Sample Output
    207
    447
    11257
    */
    
    
    #include <bits/stdc++.h>
    
    
    #define ll long long
    #define nl endl
    #define uinit 1
    #define init -1
    
    
    #define fl(n) for(int i = 0; i < n; i++)
    
    
    
    using namespace std;
    
    int main()
    {
    
        int x = init;
        int b; //our outer "i" scope equivalent
        int res = 0; //holds the sum of our combinations
        int arr[1000]; //holds all user inputs to 1000 inorder to stay in a safe zone
        int rem[1000]; //holds all our program outputs
        static int j = 0; //integer that stays during program runtime so that it can be used later to subscript data into rem[1000]
        int n = 0,m = 0; // our combination variables for our first search and 2nd search respectively
    
        for(int i = 0; cin >> x; i++)
        {
            if(x == 0) break; //end of input
    
            //reset the combinations
            n = 0;
            m = 0;
    
            //user input
            for(int y = 0; y < x; y++)
            {
                cin >> arr[y];
            }
    
            //algorithm library sort to array in an ascending order
            sort(arr,arr+x);
    
            //dividing the array elements into 2 parts then combining each part's elements forming a bigger number out of it
            for(int z = 0; z < x; z+=2)
            {
                n *= 10;
                n += arr[z];
            }
            for(int c = 1; c < x; c+=2)
            {
                m *= 10;
                m += arr[c];
            }
    
            res = 0; //resetting variable incase of previous operations
            res = n+m; //adding up the 2 combined parts
    
            //inputting the outputs to our rem[1000] array using our static global variable j inorder for elements to not get mixed up.
            for(; j <= i; j++)
            {
                rem[j] = res;
            }
            //our variable b that will be used outside this scope to print the output array rem[1000]
            b = i;
        }
    
    
        //printing out the outputs from rem[1000] array
        for(int i = 0; i <= b; i++) cout << rem[i] << nl;
    
        return 0;
    }


Basically according to the sample inputs the input : 9 0 1 2 3 4 0 1 2 3
outputs: 11257

While my code outputs: 1357
Last edited on
Hey

I got your program working properly with 0's with just one minor change (applied twice). I'll give you a clue to the answer - judging by the quality of your code you've got enough skills to write the C++ yourself.

In the third example (9 0 1 2 3 4 0 1 2 3), here's the output from those two for loops starting at line 85:
for loop #1...
n=0,arr[0]=0
n=1,arr[2]=1
n=12,arr[4]=2
n=123,arr[6]=3
n=1234,arr[8]=4
for loop #2...
m=0,arr[1]=0
m=1,arr[3]=1
m=12,arr[5]=2
m=123,arr[7]=3


So the first two numbers you take there are 0 and 1, with the 0 being discarded. That's breaking the rules of the question, which states that numbers can't start with 0. You need to check for the special case at the top of the for loop that your number isn't starting with 0. If it is, re-order the array slightly so that 0 is positioned where it will be a part of the number, BUT will still give you the smallest number possible. Hint: std::swap.

Hope that helps.
Last edited on
Topic archived. No new replies allowed.