当前位置:网站首页>第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[x,y]内的守形数,探求n位守形数,递推探索n位逐位整除数
第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[x,y]内的守形数,探求n位守形数,递推探索n位逐位整除数
2022-07-03 19:16:00 【股_四】
//基于分解的求水仙花数程序
int main()
{
int m, a, b, c;
for (m = 100; m <= 999; m++)
{
a = m / 100;
b = (m / 10) % 10;
c = m % 10;
if (m == a * a * a + b * b * b + c * c * c)
printf(" %d", m);
}
return 0;
}
结果:
//基于组合的求水仙花数程序
int main()
{
int m, a, b, c;
for(a = 1; a <= 9; a++)
for(b = 0; b <= 9; b++)
for (c = 0; c <= 9; c++)
{
m = a * 100 + b * 10 + c;
if (m == a * a * a + b * b * b + c * c * c)
printf(" %d", m);
}
return 0;
}
结果:
//兰德尔数
int main()
{
int m, n, i;
double f, k, s, t, y;
printf("请输入位数n (2 < n < 10):");
scanf("%d", &n);
m = 0;
t = 1;
for (i = 1; i <= n - 1; i++)
t = t * 10;
for (y = t + 1; y <= t * 10 - 1; y++)
{
f = y;
for (s = 0, i = 1; i <= n; i++) // 循环分离y的n个数字k
{
k = fmod(f, 10);
s += pow(k, n);
f = floor(f / 10); //求y的n个数字的n次幂之和s
}
if (y == s)
{
m++;
printf(" %.0f", y); // 检测是否满足条件 //输出探索结果
}
}
printf(" \n %d位兰德尔数共%d个", n, m);
return 0;
}
结果:
// 求[x,y]内的守形数
int main()
{
long a, b, c, k, m, s, x, y;
printf(" 请输入区间上下限整数x,y:");
scanf ("%ld" , &x) ;
scanf("%ld", &y);
m=0;
for (a = x;a <= y;a++)
{
s=a*a; //计算a的平方数s
b = 1;
k=a;
while (k > 0)
{
b = b * 10;
k = k / 10;
}
c = s % b; //为a的平方数s的尾部
if (a == c)
{
m++;
printf("%ld^2 = %1d \n", a, s);
}
}
printf(" 区间[%1d,%1d]中,共以上%d个守形数。", x, y, m);
return 0;
}
结果:
//探求n位守形数
int main()
{
int n, d, k, j, i, t, m, w, z, u, v, a[500], b[500], c[500];
printf(" 请输入整数n:");
scanf("%d", &n);
for (d = 1; d <= 9; d++)
{
for (k = 1; k <= 499; k++)
{
a[k] = 0;
b[k] = 0;
c[k] = 0;
}
a[1] = d; // 给守形数个位数赋值
for (k = 2; k <= n; k++)
{
for (j = 0; j <= 9; j++)
{
a[k] = j;
v = 0;
for (i = 1; i <= k; i++)
c[i] = 0;
for (i = 1; i <= k; i++)
{
for (z = 0, t = 1; t <= k; t++)
{
u = a[i] * a[t] + z;
z = u / 10;
b[i + t - 1] = u % 10; //计算中间结果存于b数组
}
for (w = 0, m = i; m <= k; m++)
{
u = c[m] + b[m] + w; // 计算平方存于c数组
w = u / 10;
c[m] = u % 10;
}
}
for (i = 1; i <= k; i++)
if (a[i] != c[i])
v = 1;
if (v == 0) break;
}
}
if (v == 0 && a[n] != 0)
{
printf(" %d 结尾的%d位守形数: ", a[1], n);
for (k = n; k >= 1; k--)
printf("%d", a[k]);
printf("\n");
}
}
return 0;
}
结果:
//n位逐位整除数探索
int main()
{
int i, j, n, r, t, s, a[100];
printf("逐位整除数n位,请确定n:");
scanf("%d", &n);
printf(" 所求%d 位逐位整除数:\n",n);
for (j = 1;j <= 100; j++)
a[j] = 0;
t = 0;
s = 0;
i = 1;
a[1] = 1;
while (a[1] <= 9)
{
if (t == 0 && i < n)
i++;
for (r = 0, j = 1; j <= i; j++)
{
r = r * 10 + a[j];
r = r % i;
}
//检测 i时是否整除 i
if (r != 0)
{
a[i] = a[i] + 1;
t = 1;
while (a[i] > 9 && i > 1)
{
a[i] = 0;
i--;
a[i] = a[i] + 1;
}
}
else
t = 0;
if (t == 0 && i == n)
{
s++;
printf(" %d: ", s);
for (j = 1; j <= n; j++)
printf("%d", a[j]);
printf("\n");
a[i] = a[i] + 1;
}
}
if (s == 0)
printf("没有找到!\n");
else
printf(" 共以上%d个解。\n", s) ;
return 0;
}
结果;
//递推探索n位逐位整除数
int main()
{
int d, g, i, j,k, m, n, r, a[3000][30], b[3000][30];
printf("请输入送位整除数的位数n:");
scanf("%d", &n);
g = 9;
for (j = 1; j <= g; j++)
a[j][1] = j;
for (k = 2; k <= n; k++)
{
m = 0;
for(i = 1; i <= g; i++)
for (j = 0; j <= 9; j++)
{
a[i][k] = j;
for (r = 0, d =1; d <=k; d++ )
{
r = r * 10 + a[i][d];
r = r % k;
}
if (r == 0)
{
m++;
for (d = 1; d <= k; d++)
b[m][d] = a[i][d];
}
}
g = m;
for (i = 1; i <= g; i++)
for (d = 1; d <= k; d++)
a[i][d] = b[i][d];
}
if (g > 0)
{
printf(" %d位逐位整数除数共%4d个", n,g);
for (i =1; i <= g; i++)
{
printf(" %d: ", i);
for (d = 1; d <= n; d++)
printf("%d", a[i][d]);
printf("\n");
}
}
else
{
printf(" 无解!\n");
return 0;
}
//return 0;
}
结果:
边栏推荐
- Record: pymysql is used in pycharm to connect to the database
- 【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁
- Record the errors reported when running fluent in the simulator
- Processing of user input parameters in shell script
- We have built an intelligent retail settlement platform
- 【水质预测】基于matlab模糊神经网络水质预测【含Matlab源码 1923期】
- 【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
- Differential constrained SPFA
- cipher
- 【LeetCode】【SQL】刷题笔记
猜你喜欢
This Chinese numpy quick look-up table is too easy!
【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁
Record: solve the problem that MySQL is not an internal or external command environment variable
Free sharing | linefriends hand account inner page | horizontal grid | not for sale
记录在模拟器中运行flutter时报的错
[water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
【数学建模】基于matlab船舶三自由度MMG模型【含Matlab源码 1925期】
我們做了一個智能零售結算平臺
我眼中真正优秀的CTO长啥样
Integrated easy to pay secondary domain name distribution system
随机推荐
Smart wax therapy machine based on STM32 and smart cloud
Day18 - basis of interface testing
Web Security (VIII) what is CSRF attack? Why can token prevent csdf attacks?
flask 生成swagger文档
Php based campus lost and found platform (automatic matching push)
Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week
[optics] vortex generation based on MATLAB [including Matlab source code 1927]
Compared with 4G, what are the advantages of 5g to meet the technical requirements of industry 4.0
【LeetCode】【SQL】刷题笔记
Why should we do feature normalization / standardization?
Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
These problems should be paid attention to in the production of enterprise promotional videos
leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]
SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)
2022.2.14 Li Kou - daily question - single element in an ordered array
TFs and SVN [closed] - TFs vs SVN [closed]
Succession of flutter
Driveseg: dynamic driving scene segmentation data set
记录在模拟器中运行flutter时报的错
01. Preparation for automated office (free guidance, only three steps)