C语言代码:自动填充“我出生于y年,直到x岁才遇到n个数字都不相同的年份”
#include <stdio.h> #include <stdbool.h> #include <string.h>
int check(int n) { // 判断有几个不同的数字 bool vis[10] = {0}; while (n) { if (vis[n % 10]) return 0; vis[n % 10] = true; n /= 10; } return 1; }
int main() { int y, n = 0; scanf('%d %d', &y, &n); for (int x = 0; x <= 100; x++) { int year = y + x; char s[10]; sprintf(s, '%04d', year); int len = strlen(s), cnt = 0; for (int i = 0; i < len; i++) { for (int j = i + 1; j < len; j++) { if (s[i] == s[j]) { cnt = 0; goto end; } } } cnt = check(year); if (cnt == n) { printf('%d %s\n', x, s); break; } end:; // 添加end标签,方便理解代码逻辑 } return 0; }
原文地址: https://www.cveoy.top/t/topic/oPJ6 著作权归作者所有。请勿转载和采集!