From the given
interval [a, b] print in ascending order all three-digit numbers with different
digits.
Input. Two positive three-digit integers a and b (100 ≤ a
≤ b ≤ 999).
Output. Print in
ascending order all three-digit numbers from the interval [a, b] with different
digits. Print each number on a separate line.
Sample
input |
Sample
output |
100 105 |
102 103 104 105 |
full search
Iterate over all three-digit numbers from a to b. If all the digits of the number
are different, then print it.
The function diff takes
a three-digit number n as input. Separate its digits:
hundreds a, tens b and ones c. The function diff returns 1 if all digits of the number n 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. Read the input values a and b.
scanf("%d %d",&a,&b);
Iterate over all numbers on the interval [a, b]. If all digits of the current number i are different,
then print it.
for(i = a; i <= b; i++)
if (diff(i))
printf("%d\n",i);
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();
}
}
The function diff takes
a three-digit number n as input. Separate its digits:
hundreds a, tens b and ones c. The function diff returns 1 if all digits of the number n 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. Read the input values a and b.
a, b = map(int, input().split())
Iterate over all numbers on the interval [a, b]. If all digits of the current number i are different,
then print it.
for i in range(a, b+1):
if diff(i): print(i)