当前位置:网站首页>第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[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;
}
结果:
边栏推荐
- [optics] vortex generation based on MATLAB [including Matlab source code 1927]
- Nous avons fait une plateforme intelligente de règlement de détail
- What does a really excellent CTO look like in my eyes
- Record the errors reported when running fluent in the simulator
- [water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
- [mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]
- Simulation scheduling problem of SystemVerilog (1)
- Max of PHP FPM_ Some misunderstandings of children
- Record: pymysql is used in pycharm to connect to the database
- Add control at the top of compose lazycolumn
猜你喜欢

Record: solve the problem that MySQL is not an internal or external command environment variable

Day_ 18 IO stream system

Free sharing | linefriends hand account inner page | horizontal grid | not for sale

Does SQL always report foreign key errors when creating tables?

我眼中真正优秀的CTO长啥样

A green plug-in that allows you to stay focused, live and work hard

SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)

The installation path cannot be selected when installing MySQL 8.0.23

Think of new ways
![[wallpaper] (commercially available) 70 wallpaper HD free](/img/21/6802da1056a18157b15de85df60cf5.jpg)
[wallpaper] (commercially available) 70 wallpaper HD free
随机推荐
Free year-end report summary template Welfare Collection
How can I avoid "div/0!" Errors in Google Docs spreadsheet- How do I avoid the '#DIV/0!' error in Google docs spreadsheet?
C enum contains value - C enum contains value
【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
“google is not defined” when using Google Maps V3 in Firefox remotely
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
If the warehouse management communication is not in place, what problems will occur?
Php based campus lost and found platform (automatic matching push)
Find the median of two positive arrays
01. Preparation for automated office (free guidance, only three steps)
Pecan - route
The most valuable thing
This Chinese numpy quick look-up table is too easy!
Today I am filled with emotion
Record: install MySQL on ubuntu18.04
Work Measurement - 1
EGO Planner代碼解析bspline_optimizer部分(1)
Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
What is the content of game modeling
Sentinel source code analysis part II - sentinel dashboard console startup and configuration