标题:带分数
100 可以表示为带分数的形式:100 = 3 + 69258 / 714
还可以表示为:100 = 82 + 3546 / 197
注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。
类似这样的带分数,100 有 11 种表示法。
题目要求:
从标准输入读入一个正整数N (N<1000*1000)
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
注意:不要求输出每个表示,只统计有多少表示法!
例如:
用户输入:
100
程序输出:
11
再例如:
用户输入:
105
程序输出:
6
资源约定:
峰值内存消耗 < 64M
CPU消耗 < 3000ms
在这里使用prase而不使用substr的原因是,substr在多重循环中,效率很低,导致运行超时问题。
//带分数,解法1:next_permutation()方法
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
using namespace std;
int prase(const char* str,int loc, int len)
{
int data = 0;
int t = 1;
for(int i = loc + len - 1; i >= loc;i--)
{
data += (str[i] - '0') * t;
t *= 10;
}
return data;
}
int main()
{
int n, ans = 0;
scanf("%d", &n);
string num = "123456789";
do{
const char *str = num.c_str();
for(int i = 1; i< 7; i++)
{
int a = prase(str, 0, i);
if(a > n) break;
for(int j = 1; j < 9 - i; j++)
{
int b = prase(str, i, j);
int c = prase(str, j + i, 9 - i - j);
if(b== c * (n - a)) ans++;
}
}
}while(next_permutation(num.begin(), num.end()));
cout << ans;
return 0;
}