PAT L1-033. 出生年

来源:互联网 发布:windows共享文件夹 编辑:程序博客网 时间:2024/05/21 11:00
“我出生于1988年,直到25岁才遇到4个数字都不相同的年份。”也就是说,直到2013年才达到“4个数字都不相同”的要求。本题请你根据要求,自动填充“我出生于y年,直到x岁才遇到n个数字都不相同的年份”这句话。

输入格式:

输入在一行中给出出生年份y和目标年份中不同数字的个数n,其中y在[1, 3000]之间,n可以是2、或3、或4。注意不足4位的年份要在前面补零,例如公元1年被认为是0001年,有2个不同的数字0和1。

输出格式:

根据输入,输出x和能达到要求的年份。数字间以1个空格分隔,行首尾不得有多余空格。年份要按4位输出。注意:所谓“n个数字都不相同”是指不同的数字正好是n个。如“2013”被视为满足“4位数字都不同”的条件,但不被视为满足2位或3位数字不同的条件。

输入样例1:
1988 4
输出样例1:
25 2013
输入样例2:
1 2
输出样例2:
0 0001
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Scanner;public class Main {      public static void main(String[] args)throws Exception {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));String str[]=br.readLine().split(" ");String year = str[0];int num = Integer.parseInt(str[1]);if (Integer.parseInt(year) >= 1 && Integer.parseInt(year) <= 9) {year = "000" + year;} else if (Integer.parseInt(year) >= 10 && Integer.parseInt(year) <= 99) {year = "00" + year;} else if (Integer.parseInt(year) >= 100 && Integer.parseInt(year) <= 999) {year = "0" + year;} else {}out: for (int i = 0; i < 10; i++) {for (int j = 0; j < 10; j++) {for (int z = 0; z < 10; z++) {for (int y = 0; y < 10; y++) {if ((i * 1000 + j * 100 + z * 10 + y) >= 1 && (i * 1000 + j * 100 + z * 10 + y) <= 3000) {if (Integer.parseInt("" + i + j + z + y) >= Integer.parseInt(year)) {if (num == 4) {if (i != j && i != z && i != y && j != z && j != y && z != y) {System.out.print(Integer.parseInt("" + i + j + z + y) - Integer.parseInt(year));System.out.print(" " + i + j + z + y );break out;}} else if (num == 3) {if (i == j && i != z && i != y && z != y  || i == z && i != j && i != y && j != y || i == y && i != j && i != z && j != z || j == z && j != i && j != y && i != y || j == y && j != i && j != z && i != z || z == y && z != j && z != i && i != j) {System.out.print(Integer.parseInt("" + i + j + z + y) - Integer.parseInt(year));System.out.print(" " + i + j + z + y );break out;}} else if (num == 2) {if (i == j && i == z && i != y || i == j && i == y && i != z|| j == z && j == z & j == y && j != i) {System.out.print(Integer.parseInt("" + i + j + z + y) - Integer.parseInt(year));System.out.print(" " + i + j + z + y );break out;}} else {if (i == j && i == z && i == y && j == z && j == y && z == y) {System.out.print(Integer.parseInt("" + i + j + z + y) - Integer.parseInt(year));System.out.print(" " + i + j + z + y );break out;}}}}}}}}}}

0 0