当前位置:网站首页>第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[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;
}
结果:
边栏推荐
- How to build an efficient information warehouse
- 记录在模拟器中运行flutter时报的错
- 论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
- cipher
- Pecan — @expose()
- I study database at station B (4): DQL
- Record: MySQL changes the time zone
- Driveseg: dynamic driving scene segmentation data set
- FBI warning: some people use AI to disguise themselves as others for remote interview
- Comments on flowable source code (37) asynchronous job processor
猜你喜欢
![[new year job hopping season] test the technical summary of interviewers' favorite questions (with video tutorials and interview questions)](/img/4e/a51365bb88b1fc29d1c77fcdde5350.jpg)
[new year job hopping season] test the technical summary of interviewers' favorite questions (with video tutorials and interview questions)

利用可视化结果,点击出现对应的句子

SQL injection for Web Security (1)

为什么要做特征的归一化/标准化?

QT -- qfileinfo file information reading

Record: install MySQL on ubuntu18.04

记录在模拟器中运行flutter时报的错

SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)
![235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]](/img/f5/f2d244e7f19e9ddeebf070a1d06dce.png)
235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]

The earliest record
随机推荐
Nous avons fait une plateforme intelligente de règlement de détail
Work Measurement - 1
记录在模拟器中运行flutter时报的错
In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life
[mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]
Today I am filled with emotion
235. The nearest common ancestor of the binary search tree [LCA template + same search path]
leetcode:556. Next larger element III [simulation + change as little as possible]
Why should we do feature normalization / standardization?
Why should the gradient be manually cleared before back propagation in pytorch?
Record: MySQL changes the time zone
QT -- qfile file read / write operation
Comments on flowable source code (37) asynchronous job processor
If the warehouse management communication is not in place, what problems will occur?
C enum contains value - C enum contains value
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext
Verilog HDL continuous assignment statement, process assignment statement, process continuous assignment statement
Smart wax therapy machine based on STM32 and smart cloud
Scrapy爬虫框架
FBI warning: some people use AI to disguise themselves as others for remote interview