题目:带分数(16分)
100可以表示为带分数的形式:100=3+69258/714,还可以表示为:100=82+3546/197,注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。类似这样的带分数,100有11种表示法。
题目要求:
从标准输入读入一个正整数N(N<1000*1000)
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。注意:不要求输出每个表示,只统计有多少表示法!
例如,用户输入:100
程序输出:11
再例如,用户输入:105
程序输出:6
解题思路:用next_permutation 或 dfs 遍历1-9的全部组合,满足等式即可
#include<iostream>
#include<algorithm>
using namespace std;
int GetNum(int num[], int left, int right){
int result = 0;
for(int i=left; i<=right; ++i){
result *= 10;
result += num[i];
}
return result;
}
int main()
{
int cnt = 0;
int n, arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cin >> n;
do{
for(int i=1; i<7; ++i){
int a = GetNum(arr, 1, i);
if(a > n){
continue;
}
for(int j=i+1; j<9; ++j){
int b = GetNum(arr, i+1, j);
int c = GetNum(arr, j+1, 9);
if(a+(double)b/(double)c == n){
// cout << a << " " << b << " " << c << endl;
cnt++;
}
}
}
}while(next_permutation(arr+1, arr+10));
cout << cnt;
return 0;
}