From 999 to 1023

Hi Everyone! This problem seems to be really easy to me at first but I couldn't quite figure out how to solve it. The distinct numbers into arrays then outputting the next is really challenging! I was hoping someone could show me their working code as this is really urgent for me.. heres my code so far it has errors.. lots.

#include <iostream>
using namespace std;
int main ()
{
int y;
int a[4];
cin >> y;

for (int cc = y; cc < 10000; cc++)
{
a[3] = cc%10;
a[3] += 1;
cc /= 10;
a[2] = cc%10;
cc /= 10;
a[1] = cc%10;
cc /= 10;
a[0] = cc%10;
cc /=10;

if (a[0] == a[1] && a[0] == a[2] && a[0] == a[3] && a[1] == a[2] && a[1] == a[3] && a[2] == a[3])
{
cc++;
if(a[0] != a[1] && a[0] != a[2] && a[0] != a[3] && a[1] != a[2] && a[1] != a[3] && a[2] != a[3])
{
cout << cc << endl;
break;
}
}
}
return 0;
}
Last edited on
If I have a string of conditions: a && b && c && d && e && f everyone one of those sub-expressions (a, b, c, d, ...) must evaluate to true for the full expression to evaluate to true.

Likewise for !a && !b && !c && !d , everyone one of those sub-expressions must evaluate to false for the full expression to evaluate to true. Furthermore, the if condition with a similar expression in your code is inside an if condition similar to the first described here, which guarantees that the second will not be true.

This is pretty clearly not what you want to do.

Perhaps you can express the logic in everyday language before you attempt to code it.

Since there are already a number of solutions out there that you are apparently too lazy to google, here's an untested one I slapped together:

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>

using val_type = unsigned long long;

bool hasDistinctDigits(val_type val)
{
    unsigned digits[10] = {};

    while (val)
    {
        ++digits[val % 10];
        val /= 10;
    }

    for (auto d : digits)
        if (d > 1)
            return false;

    return true;
}

int main()
{
    val_type year;
    std::cin >> year;

    while (!hasDistinctDigits(++year))
        ;

    std::cout << year << '\n';
}
Thank you so very much for taking your time to help me! I was wondering if there were any other ways to replace the line "for (auto d : digits)" since it gives me an error. I've done my research for a while and since im new to coding I really want all the help I can get! Thanks!!

Error ~~ range based "for" loops are not allowed with C++98 mode.
Is there another line of code that could be compatible with C++98?
What compiler/IDE are you using? There may be an option to enable C++11 or C++14 mode.

Absent that, try

1
2
3
for (int idx = 0; idx < 10; ++idx)
    if (digits[idx] > 1)
        return false;
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/192783/
Topic archived. No new replies allowed.