#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个数字都不相同的年份这句话。输入格式:输入在一行中给出出生年份y和目标年份中不同数字的个数n其中y在1 3000之间n可以是2、或3、或4。注意不足4位的年份要在前面补零例如公元1年被认为是0001年有2个不同的数字0和1。输出格式:根据输入输出x和能达到要求的年份。数字间以1个空格分隔行首尾不得有多余空格。年份要按4位输出。注意:所谓n个数字

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

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