简体   繁体   中英

How to find greater than a number that should be a Fascinating number ,

Fascinating Number:-

Hey find any four digit number fascinating that has all the 4 digits unique. For example 1234 is a fascinating number. His friend Rahul gave him N numbers and asks him to find the minimum number which is strictly larger than the given one and has only distinct digits.

Input format The first line of the input contains integer N, denoting the count of numbers provided by Rahul. Each of the next N lines contains one integer.

Output format Print the next fascinating number.

Constraints 1<=N<=10

1000<=number<=9000

Time Limit 1 second

Example Input

2

1234

2010

Output

1235

2013

Sample test case explanation, 1235 is the minimum number that is larger than 1234 with all 4 digits 1,2,3,5 distinct.

Please can anyone help, my approach is to take a number from user and find out all the digits and check whether they are equal or not, if they aren't equal then add +1 in the digit.

#include <iostream>
using namespace std;
int main() {
    int N,num;
    cin>>num;
    int first_digit,second_digit,third_digit,fourth_digit;
    fourth_digit=num%10;
    num=num/10;
    third_digit=num%10;
    num=num/10;
    second_digit=num%10;
    num=num/10;
    first_digit=num%10;
    
    while (first_digit==second_digit || first_digit==third_digit || first_digit==fourth_digit || second_digit==first_digit||second_digit==third_digit||second_digit==fourth_digit || third_digit==first_digit|| third_digit==second_digit|| third_digit==fourth_digit || fourth_digit==first_digit || fourth_digit==second_digit || fourth_digit==third_digit)
    {
    if (first_digit == second_digit)
    {
        second_digit=second_digit+1;
    }
    else if(first_digit == third_digit)
    {
        third_digit=third_digit+1;
    }
    else if(first_digit == fourth_digit)
    {
        fourth_digit=fourth_digit+1;
    }
    
    if (second_digit == first_digit)
    {
        second_digit=second_digit+1;
    }
    else if(second_digit == third_digit)
    {
        third_digit=third_digit+1;
    }
    else if(second_digit == fourth_digit)
    {
        fourth_digit=fourth_digit+1;
    }
    
    if (third_digit == first_digit)
    {
        third_digit=third_digit+1;
    }
    else if(third_digit == second_digit)
    {
        third_digit=third_digit+1;
    }
    else if(third_digit == fourth_digit)
    {
        fourth_digit=fourth_digit+1;
    }
    
    
    if (fourth_digit == first_digit)
    {
        fourth_digit=fourth_digit+1;
    }
    else if(fourth_digit == second_digit)
    {
        fourth_digit=fourth_digit+1;
    }
    else if(fourth_digit == third_digit)
    {
        fourth_digit=fourth_digit+1;
    }
    
    }
    cout<<first_digit<<second_digit<<third_digit<<fourth_digit;   
    
    
    
    
    return 0;
}

Their are many mistakes in the code let me point them so you check and try it yourself

Mistakes which i noticed

1.What are u exactly checking in the while condition (your intention is to check weather the number digits are unique or not but when u give a unique input then it will never go inside the while loop)

  1. When their is a digit like 9 when u add 1 to it it becomes 10 and 10 will appended please try other approach when its 9 (hint increase the previous digit by 1 and move that 9 to zero) [example if number is 2039 you code will make it 20310 which is wrong if u try increased the before number which means 2049 and replacing 9 with 0 and again as 0 is duplicate repeat until u get unique numbers]

Hope this explanation helps!

(Remember these things(Few suggestions))

  1. If this question is from a competition of codding post the questions after competition
  2. If you just want direct code from here this is not the correct place!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM