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;
}
|