bool and vector functions help

Pages: 12
If you want assistance with your current code, then you will have to show that code ... in its entirety.

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
#include <iostream>
#include <vector>
#include <set>
#include <numeric>
using namespace std;


bool check( int n )          // n is a valid number with at least 4 digits, not starting 0
{
   vector<int> digits;
   do
   {
      digits.push_back( n % 10 );
      n /= 10;
   } while( n );

   bool NO_DUPLICATES  = set<int>( digits.begin(), digits.end() ).size() == digits.size();
   bool THOUSAND_3_TEN = digits[3] == 3 * digits[1];
   bool ODD_NUMBER     = digits[0] % 2;
   bool SUM_TARGET     = accumulate( digits.begin(), digits.end(), 0 ) == 27;
   return NO_DUPLICATES && THOUSAND_3_TEN && ODD_NUMBER && SUM_TARGET;
}


int main()
{
   cout << "4 digits:\n";
   for ( int n = 1000 ; n <= 9999 ; n++ ) if ( check( n ) ) cout << n << '\n';
   cout << "5 digits:\n";
   for ( int n = 10000; n <= 99999; n++ ) if ( check( n ) ) cout << n << '\n';
}



4 digits:
9837
5 digits:
29637
29835
36729
36927
46827
49635
63819
69237
69435
69831
76329
76923
83619
86427
89037
89235
89631
96327
96723

Last edited on
I don’t even know who you are replying to because there are other people’s code that doesn’t pertain to the problem I posted.
@totoo,

I might jump in here and say @lastchance's last post is a kind offer by him to assist you, @totoo, if you require it.

If, on the other hand, you are fulfilled by the current and substantial assistance the normal protocol is for you as the originator of this thread to green tick it.

Your call :)
Sorry for the 'off-problem' posts. Got a bit sidetracked....

As @lastchance wrote, if you post your current code we'll provide guidance.
Topic archived. No new replies allowed.
Pages: 12