題目描述
100 可以表示爲帶分數的形式:100 = 3 + 69258 / 714
還可以表示爲:100 = 82 + 3546 / 197
注意特徵:帶分數中,數字 1∼9 分別出現且只出現一次(不包含 0)。
類似這樣的帶分數,100 有 11 種表示法。
輸入格式
輸入樣例
100
輸出樣例
6
分析
大佬都自己寫遞歸,唉可是我這個蒟蒻只會用函數。題目要求1-9中每個數都用一次,這個時時候我們很容易想到全排列,其實algorithm頭文件裏面有一個next_permutation函數可以直接拿過來用哈哈哈,這個函數會生成當前序列的下一個全排列序列,具體使用方法見代碼。
AC代碼
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n, ans;
int a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// 求出這個數
int getNum(int l, int r)
{
int res = 0;
for (int i = l; i <= r; i++)
{
res = res * 10 + a[i];
}
return res;
}
// 判斷a == a + b / c
bool check(int a, int b, int c)
{
if (b % c != 0)
{
return false;
}
if (a + b / c != n)
{
return false;
}
return true;
}
int main()
{
cin >> n;
do{
for (int i = 0; i < 6; i++)// 數據範圍是n < 1000000
{
for (int j = i + 1; j < 8; j++)// c必須大於0,所以要給c留出一位
{
int a = getNum(0, i);
int b = getNum(i + 1, j);
int c = getNum(j + 1, 8);
if (check(a, b, c))
{
ans++;
}
}
}
}while(next_permutation(a, a + 9));
printf("%d\n", ans);
return 0;
}