C语言写的21位花朵数

来源:互联网 发布:淘宝推广教程 编辑:程序博客网 时间:2024/05/07 23:17
// 21位花朵数.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>#define N 21using namespace std;void fang(int x);int arr[10][21];int main(){int cot[10];for(int i = 0;i < 10; ++i){cot[i] = 0;fang(i);}//生成一个N位数并计数该数中每个数字出现的次数for(cot[9] = 0;cot[9] <= 9; ++cot[9]){for(cot[8] = 0;cot[8] <= N - cot[9]; ++cot[8]){for(cot[7] = 0;cot[7] <= N - cot[9] - cot[8]; ++cot[7]){for(cot[6] = 0;cot[6] <= N - cot[9] - cot[8] - cot[7]; ++cot[6]){for(cot[5] = 0;cot[5] <= N - cot[9] - cot[8] - cot[7] - cot[6]; ++cot[5]){for(cot[4] = 0;cot[4] <= N - cot[9] - cot[8] - cot[7] - cot[6] - cot[5]; ++cot[4]){for(cot[3] = 0;cot[3] <= N - cot[9] - cot[8] - cot[7] - cot[6] - cot[5] - cot[4]; ++cot[3]){for(cot[2] = 0;cot[2] <= N - cot[9] - cot[8] - cot[7] - cot[6] - cot[5] - cot[4] - cot[3]; ++cot[2]){for(cot[1] = 0;cot[1] <= N - cot[9] - cot[8] - cot[7] - cot[6] - cot[5] - cot[4] - cot[3] - cot[2]; ++cot[1]){cot[0] = N - cot[9] - cot[8] - cot[7] - cot[6] - cot[5] - cot[4] - cot[3] - cot[2] - cot[1];int cota[10],add[N];for(int j = 0;j < N; ++j){add[j] = 0;}for(int k = 0;k < 10; ++k){cota[k] = 0;}//求该数的每个位上的数字的N次方的和for(int l = 0;l < 10; ++l){for(int m = 1;m <= cot[l]; ++m){for(int n = 0;n < N; ++n){add[n] += arr[l][n];while(add[n] > 9){add[n] -= 10;add[n + 1]++;}}}}//判断得到的和是不是一个21位数if(0 == add[N - 1]) continue;//计数和中每个数字出现的次数for(int o = 0;o < N; ++o){cota[add[o]]++;}//将和中每个数字出现的次数与该数中每个数字出现的次数进行比较int flag = 1;for(int p = 0;p < 10; ++p){if(cot[p] != cota[p]){flag = 0;break;}}//当该数不符合要求时执行下一次循环,不输出该数if(0 == flag) continue;for(int q = N - 1;q >= 0; --q)cout<<add[q];cout<<endl;}}}}}}}}}return 0;}//该函数用来求x的21次方,并将所求结果存储在数组arr[x]中void fang(int x){long int a = 0,b = 0,c = 1;for(int i = N;i > 0; --i){c = c * x;a = a * x;b = b * x;while(b > 9999999){b -= 10000000;a += 1;}while(c > 9999999){c -= 10000000;b += 1;while(b > 9999999){b -= 10000000;a += 1;}}}int d;int e = N - 14;while(a){d = a % 10;arr[x][N - (e--)] = d;a = a / 10;}e = N - 7;while(b){d = b % 10;arr[x][N - (e--)] = d;b = b / 10;}e = N;while(c){d = c % 10;arr[x][N - (e--)] = d;c = c / 10;}}