Three-digit numbers#2097

Three-digit numbers (Editorial)

Algorithm Analysis

We will iterate over all three-digit numbers from to . If all the digits of the number are different, then we output it.

Algorithm Implementation

The function diff receives a three-digit number . We extract from it the digits of hundreds , tens , and units . The function diff returns 1 if all the digits of the number are different.

int diff(int n)
{
    int a = n / 100;
    int b = (n / 10) % 10;
    int c = n % 10;
    return (a != b) && (b != c) && (a != c);
}

The main part of the program. We read the input values and .

scanf("%d %d",&a,&b);

We iterate over all numbers in the interval [, ]. If all the digits of the current number are different, then we output it.

for(i = a; i <= b; i++)
    if(diff(i))
        printf("%d\n",i);

Java Implementation

import java.util.*;

public class Main
{
    static boolean diff(int n)
    {
        int a = n / 100, b = (n / 10) % 10, c = n % 10;
        return !((a == b) || (b == c) || (a == c));
    }

    public static void main(String[] args)
    {
        Scanner con = new Scanner(System.in);
        int a = con.nextInt();
        int b = con.nextInt();
        for(int i = a; i <= b; i++)
            if(diff(i))
                System.out.println(i);
        con.close();
    }
}

Python Implementation

The function diff receives a three-digit number . We extract from it the digits of hundreds , tens , and units . The function diff returns 1 if all the digits of the number are different.

def diff(n):
    a = n // 100
    b = (n // 10) % 10
    c = n % 10
    return a != b and b != c and a != c

The main part of the program. We read the input values and .

a, b = map(int, input().split())

We iterate over all numbers in the interval [, ]. If all the digits of the current number are different, then we output it.

for i in range(a, b+1):
    if diff(i): print(i)