Here i am finding maximum sum which can be formed by these (user input) numbers but numbers which are used to sum should not have any digit in common.
- We are given
n
numbers. - We look at all subsets of this numbers,where every digit appears not more than once.
- For this subsets we calculate the sums.
- We are then looking for the maximum of this sums.
eg. input cases numbers n=4
0 1 2 3 - > 6 ( no repeated digits here ) 0+1+2+3
3 30 8 1 - > 39 ( here 3 is repeated so choose max from 3 & 30 i.e. 30) 30+8+1
11 21 31 41 - > 41 ( here 1 is repeated to all so max number will print ) 41
11 5 45 88 - > 99 ( here 5 is repeated so choose max from 45 & 5 i.e 45 ) 11+88+45
int sum(int Ticket[], int n)
{
int max;
int abc;
for (int i = 0; i <= n; i++) {
for (int k = 0; k <= n; k++) {
abc = Ticket[i];
max = Ticket[i] + Ticket[i]
int len = to_string(abc).length();
for (int j = 0; j <= len; j++) {
std::string nstr = std::to_string(abc);
std::cout << "->" << nstr[j];
}
}
}
return max;
}
int main()
{
// n total number of array or numbers
int max = sum(Ticket, n);
}
quetion is how check uniqe digits of each numbers and form maximum sum.
Aucun commentaire:
Enregistrer un commentaire