c - 在 C 中输出自守数
问题描述
我需要在每行中输出 10 个自同构数 ,5。我写了下面的代码,但它不能正常工作,你能帮我吗?我需要输出的自守数字:“1、5、6、25、76、376、625、9376、90625、109376”。
int n = 10, m = 10, a, b, c;
for ( n = 1; n < 11; n++) {
b = m;
while (n > b) {
b = b * m;
}
a = (n * n) % b;
if (a == n)
{
printf("%d ", n);
if (n % 6 == 0)
printf("\n");
}
a++;
解决方案
有趣的问题。
这就是我想出的。
#include <stdio.h>
#include <math.h>
#include <limits.h>
int main()
{
int counter = 0;
int i=1;
while(counter < 10)
{
if (i*i % (int)pow(10,(int)log10(i)+1) == i)
{
counter++;
printf("%d) %d is automorphic because %d * %d == %d\n", counter, i, i, i, i*i);
}
++i;
}
return 0;
}
样本输出
Success #stdin #stdout 0s 4516KB
1) 1 is automorphic because 1 * 1 == 1
2) 5 is automorphic because 5 * 5 == 25
3) 6 is automorphic because 6 * 6 == 36
4) 25 is automorphic because 25 * 25 == 625
5) 76 is automorphic because 76 * 76 == 5776
6) 376 is automorphic because 376 * 376 == 141376
7) 625 is automorphic because 625 * 625 == 390625
8) 9376 is automorphic because 9376 * 9376 == 87909376
(由于时间限制,我在找到 8 个值后停止了)
推荐阅读
- java - MySql 内连接结果重复值
- r - 如何在R中按顺序重新编号组ID?
- spring - 如何翻译 grails 2 LDAP Active Directory 值以使用 Spring Boot
- symfony - 如何仅在 require-dev 中使用 bunde?
- python - Django 模型访问 Django 外部而不访问设置文件
- angular - 外部导航在角度 5 中不起作用
- php - 无法从大型表行中检索记录
- python - 不同文件/模块中的 Python 选项卡 - Tkinter
- sql - 如何在一个查询中插入 1 个表并更新另一个表
- discord - 如何通过代理连接不和谐机器人