#include <stdio.h> #include <string.h> #include <stdbool.h>

/**

  • 判断num是否合法
  • @param num 待判断的数字
  • @param n num中不同数字的个数
  • @return 是否合法 */ bool isLegal(int num, int n) { int nums[10] = {0}; // 存储数字出现的次数 while (num != 0) { nums[num % 10]++; num /= 10; } int count = 0; // 不同数字的个数 for (int i = 0; i < 10; i++) { if (nums[i] != 0) { count++; } } return count == n; }

int main() { int y, n; scanf("%d %d", &y, &n); int x = 0; int year = y; while (true) { x++; year++; if (isLegal(year, n)) { break; } } printf("%d %04d", x, year); return 0; }

一些坑点:

  1. 注意不足4位的年份要在前面补零,例如公元1年被认为是0001年;
  2. 输入的n可以是2、或3、或4,注意不是4位数字都不同,而是n个数字都不同;
  3. 输出的年份要按4位输出。
C语言代码:自动填充“我出生于y年,直到x岁才遇到n个数字都不相同的年份”语句

原文地址: https://www.cveoy.top/t/topic/oSdz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录