Unique numbers

The following needs to happen as output
a , b , c + d, e ,f = g, h i
example
275 + 193 = 468

each number ranging 1 - 9 is only allowed once. All I know is that some kind of loop and if/else statement is required to do this. Can someone please help?



#include <iostream>

using namespace std;


void myf(int a, int b, int c, int d, int e, int f)
{
int num1, num2, num3, temp;
int mynum[10];
int idx;
bool exist;
int g, h, i;

num1 = a * 100 + b * 10 + c;
num2 = d * 100 + e * 10 + f;
num3 = num1 + num2;

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

}




int main(int argc, char *argv[]) {

int aa, bb, cc, dd, ee, ff;

for (aa = 1; aa < 10; aa++)
{
for (bb = 1; bb < 10; bb++)
{
for (cc = 1; cc < 10; cc++)
{
for (dd = 1; dd < 10; dd++)
{
for (ee = 1; ee < 10; ee++)
{
for (ff = 1; ff < 10; ff++)
{
myf(aa, bb, cc, dd, ee, ff);
}
}
}
}
}
}

return 0;
}
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>//inner_product
using namespace std;

constexpr auto SIZE = 3;
const vector<double> mult {100, 10, 1};
vector<double>& setup(vector<double>& v);

int main()
{
    vector<double> num1, num2;

    cout << "Enter first number: \n";
    setup(num1);
    cout << "Enter second number: \n";
    setup (num2);

    cout << "Sum of the numbers is: " << inner_product (num1.begin(), num1.end(), mult.begin(), 0)
        + inner_product (num2.begin(), num2.end(), mult.begin(), 0) << '\n';
}

vector<double>& setup(vector<double>& v)
{
    size_t n{};
    while(n < SIZE)
   {
        cout << "Enter digit " << (n+1) << " of number: \n";
        double num{};
        cin >> num;
        if(num == 0 || (find(v.begin(), v.end(), num)) != v.end())
       {
            cout << "Invalid number, try again \n";
       }
       else
       {
           v.push_back(num);
           n++;
       }
    }
    cout << "Number entered successfully \n";
    return v;
}
I quite liked what Chervil said on the other thread about keeping main()
easily visible
and thought I'd tweak the above program a bit because its line 20 is, quite frankly, ugly!
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>//inner_product
using namespace std;

constexpr auto SIZE = 3;
const vector<double> mult {100, 10, 1};
vector<double>& setup(vector<double>& v);
double vec_mult(const vector<double>& lhs, const vector<double>& rhs);

int main()
{
    vector<double> num1, num2;

    cout << "Enter first number: \n";
    setup(num1);
    double n1 = vec_mult (num1, mult);

    cout << "Enter second number: \n";
    setup (num2);
    double n2 = vec_mult (num2, mult);

    cout << "Sum of: " << n1 << " and " << n2 << " is " << n1 + n2 <<'\n';
}

vector<double>& setup(vector<double>& v)
{
    size_t n{};
    while(n < SIZE)
   {
        cout << "Enter digit " << (n+1) << " of number: \n";
        double num{};
        cin >> num;
        if(num == 0 || (find(v.begin(), v.end(), num)) != v.end())
       {
            cout << "Invalid number, try again \n";
       }
       else
       {
           v.push_back(num);
           n++;
       }
    }
    cout << "Number entered successfully \n";
    return v;
}
double vec_mult(const vector<double>& lhs, const vector<double>& rhs)
{
    return inner_product (lhs.begin(), lhs.end(), rhs.begin(), 0);
}
Last edited on
closed account (48T7M4Gy)
Now it works with Quads, !=, <set> 's and no <map>'s
Last edited on
it works

what works?
closed account (48T7M4Gy)
124 659 783
125 739 864
127 359 486
127 368 495
128 367 495
128 439 567
129 357 486
129 438 567
129 654 783
129 735 864
134 658 792
135 729 864
138 429 567
138 654 792
139 428 567
139 725 864
142 596 738
142 695 837
143 586 729
145 692 837
146 583 729
146 592 738
152 487 639
152 784 936
154 629 783
154 638 792
154 782 936
157 329 486
157 482 639
158 634 792
159 327 486
159 624 783
162 387 549
162 783 945
163 782 945
167 328 495
167 382 549
168 327 495
173 286 459
173 295 468
175 293 468
176 283 459
182 367 549
182 394 576
182 457 639
182 493 675
182 754 936
182 763 945
183 276 459
183 492 675
183 546 729
183 762 945
184 392 576
184 752 936
186 273 459
186 543 729
187 362 549
187 452 639
192 384 576
192 483 675
192 546 738
192 645 837
193 275 468
193 482 675
194 382 576
195 273 468
195 642 837
196 542 738
214 569 783
214 659 873
215 478 693
215 748 963
216 378 594
216 738 954
218 349 567
218 376 594
218 439 657
218 475 693
218 736 954
218 745 963
219 348 567
219 438 657
219 564 783
219 654 873
234 657 891
235 746 981
236 718 954
236 745 981
237 654 891
238 419 657
238 716 954
239 418 657
241 596 837
243 576 819
243 675 918
245 673 918
245 718 963
245 736 981
246 573 819
246 591 837
246 735 981
248 319 567
248 715 963
249 318 567
251 397 648
254 619 873
254 637 891
257 391 648
257 634 891
259 614 873
264 519 783
269 514 783
271 593 864
271 683 954
273 546 819
273 591 864
273 645 918
273 681 954
275 418 693
275 643 918
276 318 594
276 543 819
278 316 594
278 415 693
281 394 675
281 673 954
283 671 954
284 391 675
291 357 648
291 384 675
291 546 837
291 573 864
293 571 864
294 381 675
296 541 837
297 351 648
314 658 972
317 529 846
317 628 945
318 627 945
318 654 972
319 527 846
324 567 891
324 657 981
327 519 846
327 564 891
327 618 945
327 654 981
328 617 945
329 517 846
341 586 927
342 576 918
346 572 918
346 581 927
352 467 819
354 618 972
354 627 981
357 462 819
357 624 981
358 614 972
362 457 819
364 527 891
367 452 819
367 524 891
372 546 918
376 542 918
381 546 927
386 541 927
Program ended with exit code: 0
Last edited on
Oh good, and where can we see this masterpiece?
closed account (48T7M4Gy)
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
114
115
#include <iostream>
#include <set>

static std::set<int> myset;
struct Quad
{
    int number = 0;
    static const int limit = 3;
    int digit[limit] = {0};
    
    Quad(){};
    Quad(int aNumber){number = aNumber; reduce();};
    
    void reduce()
    {
        int abc = number;
        
        for(int i = 0; i < limit; i++)
        {
            digit[i] = abc % 10;
            abc /= 10;
        }
        
        return;
    }
    
    bool isValid()
    {
        myset.clear();
        
        if( number > 987 or number < 123)
            return false;
        
        for(int i = 0; i < limit; i++)
        {
            if( digit[i] == 0 or myset.insert(digit[i]).second == false)
                return false;
            else
                myset.insert(digit[i]);
        }
        
        return true;
    }
    
    bool operator!=( Quad &q1 )
    {
        if( !isValid() or !q1.isValid())
            return false;
        
        myset.clear();
        
        for(int i = 0; i < limit; i++)
        {
            if( myset.insert(digit[i]).second == false)
                return false;
            else
                myset.insert(digit[i]);
            
            if( myset.insert(q1.digit[i]).second == false)
                return false;
            else
                myset.insert(q1.digit[i]);
        }
        
        return true;
    }
    
    Quad operator+( const Quad &rhs )
    {
        Quad total = number + rhs.number;
        total.reduce();
        return total;
    }
    
    friend std::ostream& operator<< (std::ostream& os, const Quad &rhs)
    {
        os << rhs.number << '{' << rhs.digit[0] << ',' << rhs.digit[1] << ',' << rhs.digit[2]<< "} ";
        return os;
    }
};

int main()
{
    const int limit = 987;
    Quad array[1000] = {0};
    
    Quad quad_1, quad_2, quad_3;
    Quad temp;
    
    for (int i = 0; i < limit; i++)
    {
        temp.number = i;
        temp.reduce();
        
        if(temp.isValid())
            array[i] = temp;
    }
    
    for( auto it_1 = 0; it_1 < limit; it_1++)
    {
        quad_1 = array[it_1];
        
        for( auto it_2 = it_1; it_2 < limit; it_2++)
        {
            quad_2 = array[it_2];
            if( quad_1 != quad_2 )
            {
                quad_3 = quad_1 + quad_2;
                if(quad_3.isValid() and quad_3 != quad_1 and quad_3 != quad_2)
                    std::cout << quad_1.number << ' ' << quad_2.number << ' ' << quad_3.number << '\n';
            }
        }
    }
    return 0;
}
Last edited on
Fix?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation

int foo( int * bar ) {
    return 100 * bar[0] + 10 * bar[1] + bar[2];
}

int main () {
  int myints[] = {1,2,3,4,5,6,7,8,9};

  do {
    auto a = foo( myints );
    auto b = foo( myints + 3 );
    auto c = foo( myints + 6 );
    if ( a + b == c )
      std::cout << a << ' ' << b << ' ' << c << '\n';
  } while ( std::next_permutation(myints,myints+9) );

  return 0;
}
Topic archived. No new replies allowed.