带分数 蓝桥(枚举,全排列 )--next_permutation(a.begin(),a.end());

试题 历届试题 带分数

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
100 可以表示为带分数的形式:100 = 3 + 69258 / 714。

还可以表示为:100 = 82 + 3546 / 197。

注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。

类似这样的带分数,100 有 11 种表示法。

输入格式
从标准输入读入一个正整数N (N<1000*1000)

输出格式
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。

注意:不要求输出每个表示,只统计有多少表示法!

样例输入1
100
样例输出1
11
样例输入2
105
样例输出2
6

枚举,全排列

#pragma warning(disable:4996)
#include"iostream"
#include"functional"
#include"algorithm"
#include"cstring"
#include"stack"
#include"cmath"
#include"queue"
#include"vector"
#include"map"
typedef long long int ll;
using namespace std;
string a = "123456789";
ll ans = 0, ad = 0;
void solve() {
	int i = 0;
	ll sum = 0, bc = 0, ch = 0;
	int j = 0;
	for (; i <= 7; i++) {
		sum *= 10;
		sum += a[i] - '0';
		if(sum >= ad) return;
		bc = 0, ch = 0;
		int k = i+1;
		for (; k < 8; k++) {
			bc *= 10;
			bc += a[k] - '0';	
			ch = 0;
			int l = k+1;
			for (; l < 9; l++) {
				ch *= 10;
				ch += a[l] - '0';
			}
			if (bc % ch != 0) continue;
			if (ad == ((bc / ch) + sum)) {
				ans++;
			}
		}
		
	}
}
int main() {
	cin >> ad;
	do {
		solve();
	} while (next_permutation(a.begin(), a.end()--));
	cout << ans;
}
发布了49 篇原创文章 · 获赞 0 · 访问量 660

猜你喜欢

转载自blog.csdn.net/qq_14989799/article/details/104693562