历届试题 带分数 全排列

来源:互联网 发布:商标设计软件 编辑:程序博客网 时间:2024/06/05 03:44

                还有半个小时就出发去成都了,希望这次蓝桥杯能够取的好成绩。也祝愿参加蓝桥杯的其他选手能够发挥爆表,让自己的努力得到证明。

           思路:直接枚举1~9的全排列,然后分成三个数,看是否符合条件。典型的省赛暴力题。

AC代码

#include <cstdio>#include <cmath>#include <cctype>#include <algorithm>#include <cstring>#include <utility>#include <string>#include <iostream>#include <map>#include <set>#include <vector>#include <queue>#include <stack>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000") #define eps 1e-10#define inf 0x3f3f3f3f#define PI pair<int, int> typedef long long LL;const int maxn = 1e4 + 5;int ans, n;int a[15], vis[15];void dfs(int cur) {if(cur == 9) {//一个数for(int i = 0; i < 7; ++i) {int x = 0;for(int k = 0; k <= i; ++k) {x = x * 10 + a[k];}if(x >= n) break;for(int j = i+1; j < 8; ++j) {int y = 0, z = 0;for(int k = i+1; k <= j; ++k) {y = y * 10 + a[k];}for(int k = j+1; k < 9; ++k) {z = z * 10 + a[k];}if(y % z == 0 && n == x + y / z) {ans++;}}}return;}for(int i = 1; i <= 9; ++i) {if(!vis[i]) {vis[i] = 1;a[cur] = i;dfs(cur+1);vis[i] = 0;}} }int main() {while(scanf("%d", &n) == 1) {ans = 0;memset(vis, 0, sizeof(vis));dfs(0);printf("%d\n", ans);}return 0;} 

如有不当之处欢迎指出!

0 0
原创粉丝点击