61*21=1260
93*15=1395
41*35=1435
51*30=1530
87*21=1827
81*27=2187
86*80=6880
93*15=1395
41*35=1435
51*30=1530
87*21=1827
81*27=2187
86*80=6880
#include <string.h> #include <stdio.h> char cNum [ 24 ][ 5 ] = {"\0"}; //用于存放产生的24种排列组合 char cNumber [10][5] = {"\0"}; //用于存放吸血鬼数字 int iNum = 0; int iNumber = 0; void Swap(char * a, char * b) { // 交换a和b char temp = *a; *a = *b; *b = temp; } void Perm(char list[], int k, int m) { //生成list [k:m ]的所有排列方式 int i; if (k == m) { strcpy (cNum [iNum], list); iNum ++; if (iNum > 23) iNum = 0; } else // list[k:m ]有多个排列方式, 递归地产生这些排列方式 for (i=k; i <= m; i++) { Swap (&list[k], &list[i]); Perm (list, k+1, m); Swap (&list[k], &list[i]); } } void main() { int temp; int i, j, k, l; int c[10], d[10]; char a[5] = {"\0"}, b[5] = {"\0"}; for (i = 11; i < 100; i ++) for (j = 11; j < 100; j ++) { temp = i * j; if (temp < 1001) continue; a[0] = temp / 1000 + 48; //数字分解成字符串 temp = temp % 1000; a[1] = temp / 100 + 48; temp = temp % 100; a[2] = temp / 10 + 48; a[3] = temp % 10 + 48; b[0] = i / 10 + 48; //两位数分解成字符串 b[1] = i % 10 + 48; b[2] = j / 10 + 48; b[3] = j % 10 + 48; Perm (b, 0, 3); //排列所有可能的字符串 for (k = 0; k < 24; k ++) //逐个比较字符串 { if (strcmp (a, cNum [k]) == 0) //如果匹配 { for (l = 0; l < 10; l ++) //判断此串是否已经存在字串数组中 { if (strcmp (cNum [k], cNumber [l]) == 0) break; } if (l == 10) //如果不在数组中,就添加进去 { strcpy (cNumber [iNumber], cNum [k]); c[iNumber] = i; d[iNumber] = j; iNumber ++; } } } } i = 0; while (cNumber [i][0] != '\0') //输出字串 { printf("%s = %d * %d\n", cNumber[i], c[i], d[i]); i ++; } }
/* 吸血鬼数字: 1260 = 21 * 60 1827 = 21 * 87 2187 = 27 * 81 */ #include<stdio.h> void Vampire(int n); void main() { int n = 1001; while (n++ < 10000) Vampire(n); } void Vampire(int n) { int i, j, k, l, temp; int a[4]; int iflag = 0; //标志位,防止重复输出 temp = n; a[0] = temp / 1000; //数字分离 temp = temp % 1000; a[1] = temp / 100; temp = temp % 100; a[2] = temp / 10; a[3] = temp % 10; for( i = 0; i < 4; i ++) //四重循环嵌套找数字 { for( j = 0; j < 4; j ++) { if(j == i) continue; for( k = 0; k < 4; k ++) { if(k == i || k == j) continue; for(l = 0; l < 4; l ++) { if(l == k || l == i || l == j) continue; if((a[i] * 10 + a[j]) * (a[k] * 10 + a[l]) == n) { //吸血鬼数字的条件 printf("%d = %d * %d\n", n, a[i] * 10 + a[j], a[k] * 10 + a[l]); iflag = 1; break; } if( iflag == 1) break; } if (iflag == 1) break; } if (iflag == 1) break; } if (iflag == 1) { iflag = 0; break; } } }