【[Offer收割]編程練習賽23 A】【爆搜】H國的身份證號碼I

題目1 : H國的身份證號碼I

時間限制:10000ms
單點時限:1000ms
內存限制:256MB

描述

H國的身份證號碼是一個N位的正整數(首位不能是0)。此外,由於防僞需要,一個N位正整數是合法的身份證號碼當且僅當每位數字都小於等於K,並且任意相鄰兩位數字的乘積也小於等於K。

例如對於K=5, 101、211、210等都是合法的號碼,而106、123、421等都是非法的號碼。

給定一個正整數N以及K,請從小到大輸出所有合法的號碼。  

輸入

兩個整數N和K。

對於80%的數據,1 ≤ N ≤ 6。

對於100%的數據,1 ≤ N ≤ 9,1 ≤ K ≤ 5。

輸出

按從小到大的順序輸出所有合法的N位號碼,每個號碼佔一行。

樣例輸入
2 4
樣例輸出
10  
11  
12  
13  
14  
20  
21  
22  
30  
31  
40  
41

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int n, K;
vector<int>vt;
void dfs(int p, int pre, int now)
{
	if (p > n)
	{
		vt.push_back(now);
		return;
	}
	for (int i = 0; i <= K; ++i)
	{
		if (i * pre > K)break;
		dfs(p + 1, i, now * 10 + i);
	}
}
int main()
{
	while (~scanf("%d%d", &n, &K))
	{
		vt.clear();
		for (int i = 1; i <= K; ++i)
		{
			dfs(2, i, i);
		}
		//sort(vt.begin(), vt.end());
		for (auto it : vt)
		{
			printf("%d\n", it);
		}
	}
	return 0;
}
/*
【題意】
讓你判定,對於首位不能爲0的恰好n(n <= 9)位數,
如果其每一位的數要<=K 且相鄰位的數之乘積<=K,
要你依次輸出所有這樣的數。

【分析】
直接dfs即可
因爲dfs是從小到大貪心dfs的,所以甚至不需要排序。

【時間複雜度&&優化】
O(n)

*/


//輸出掛,整數用print,否則用putc,初始化爲這裏的"PS = PF",最後要輸出
char PF[SIZE], *PS = PF, *PT = PF + SIZE;
void putc(char ch)
{
	if (PS == PT)fwrite(PF, 1, SIZE, stdout), PS = PF;
	*PS++ = ch;
}
template<class T>inline void print(T x)
{
	char s[20], *b; b = s;
	bool sgn = 0;
	if (x < 0) { sgn = 1; x = -x; }
	if (!x)*b++ = 48;
	while (x) { *b++ = x % 10 + 48; x /= 10; }
	if (sgn)putc('-');
	while (b-- != s) putc(*b);
}
void prints(char s[])
{
	for (int i = 0; s[i]; ++i)putc(s[i]);
}
//這兩句話要加在輸出之後
fwrite(PF, 1, PS - PF, stdout);
*PS = PF, *PT = PF + SIZE;


//簡單輸入掛
const int SIZE = 1 << 20;
char S[SIZE], *SS = S, *ST = S, CH;
inline char getc()
{
	return SS == ST && (ST = (SS = S) + fread(S, 1, SIZE, stdin), SS == ST) ? 0 : *SS++;
}
template<class T>inline bool read(T &x)
{
	x = 0; int sgn = 1;
	while (CH = getc(), !isdigit(CH) && CH != '-')if (CH == 0)return 0;
	if (CH == '-')sgn = -1; else x = CH ^ 48;
	while (CH = getc(), isdigit(CH))x = (x << 3) + (x << 1) + (CH ^ 48);
	x *= sgn;
	return 1;
}
template<class T1, class T2>inline void read2(T1 &x, T2 &y)
{
	read(x); read(y);
}
template<class T1, class T2, class T3>inline void read3(T1 &x, T2 &y, T3 &z)
{
	read(x); read(y); read(z);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章