PAT L1-033. 出生年

该编程题目要求根据出生年份和目标年份的不同数字个数,输出所需年龄和满足条件的年份。给定出生年份y和目标年份n位不同数字的数量,程序会计算并输出相应的年龄和年份。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

“我出生于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;
									}
								}

							}

						}
					}
				}
			}
		}

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值