当前位置:网站首页>C language classic exercise (1) - "daffodil number"“
C language classic exercise (1) - "daffodil number"“
2022-07-23 09:15:00 【it_ NunU】
C Interesting language exercises —— Narcissistic number
Catalog
Topic introduction
Narcissus number refers to “ The number itself is equal to the cubic sum of each number ” Three digits of .
for example :123 It's Narcissus , because 123 = 13 + 23 + 33. Please calculate the number of all daffodils .
One 、 Method explanation
1. Method 1
First, determine the number of daffodils n The possible value range of , because n It's a three digit number , So the value is 100~999 Between , It's not hard to see. , This is a technology controlled cycle . about n Every possible value of , First, separate the single digit numbers ( k )、 Ten digits ( j )、 Hundred digits ( i ), And then through n And j3 + k3 + i3 Compare whether it is equal , That's for sure n Is it the number of daffodils .
2. Method 2
Set the hundreds of daffodils 、 Ten digits 、 The single digits are i、j、k, By traversing i、j、k All possible values of , Then judge i * 100 + j * 10 + k And i * i * i + j * j * j + k * k * k Whether it is equal or not , The calculation can be completed .(* notes : Because daffodils are in three digits , Its hundreds of digits i The value of cannot be 0)
Two 、 Code implementation
Law 1 The code is as follows ( Example ):
int main()
{
int i, j, k, n;
for ( n = 100; n < 1000; n++)
{
i = n / 100; // Separate hundreds of digits
j = (n - i * 100) / 10; // Separate out ten digits
k = n % 10; // Separate out one digit
if (n == i*i*i + j*j*j + k*k*k) // Determine whether the number of daffodils is satisfied
{
printf("%d ", n);
}
}
printf("\n");
return 0;
}
Run the renderings

Law two The code is as follows ( Example ):
int main()
{
int i, j, k;
for ( i = 1; i <= 9; i++) // Traverse all possible values of hundreds of digits
{
for ( j = 0; j <= 9; j++) // Traverse all possible values of ten digits
{
for (k = 0; k <= 9; k++) // Traverse all possible values of each number
{
// Judge
if (i*100 + j*10 + k == i*i*i + j*j*j + k*k*k)
{
printf("%d ", i * 100 + j * 10 + k);
}
}
}
}
return 0;
}
Run the renderings

summary
The above is a simple number of daffodils C Language implementation method , If there are mistakes in the text, please point them out ~
If it helps you , Attention and praise Well .
Look forward to making progress with you !
边栏推荐
猜你喜欢

求解最大公约数和最小公倍数

Airserver third party projection software v7.3.0 Chinese Version (airplay terminal utility)

UGUI源码解析——Mask

一文了解微服务低代码实现方式
![[cloud native] in the era of storm and cloud, the sharp edge of DBAs is out of the sheath](/img/22/5547663b5c0b4224d9a02d51b0f325.png)
[cloud native] in the era of storm and cloud, the sharp edge of DBAs is out of the sheath

IDM downloader free high-quality win download tool without restrictions

SQL Server database design -- select statement

NodeJS 基于 Dapr 构建云原生微服务应用,从 0 到 1 快速上手指南

PMP备考心得 | 好的习惯、好的过程、好的结果

数学建模——图与网络模型及方法(二)
随机推荐
Amplitude limiting and deblocking filtering of botu PLC signal processing series
差分数组操作的一些性质
股票开户网上开户安全吗,银河证券怎么样
SQL用户表的通用设计
砥砺前行新征程,城链科技狂欢庆典在厦门隆重举行
AirServer第三方投屏软件v7.3.0中文版 (Airplay终端实用工具)
[array]nc77 adjust the array order so that odd numbers precede even numbers (one) - medium
详解Vector
PMP备考心得 | 好的习惯、好的过程、好的结果
UGUI源码解析——IMaskable
在通达信开户安全不
涅槃重生!字节大牛力荐大型分布式手册,凤凰架构让你浴火成神
【面试:并发篇21:多线程:活跃性】死锁、活锁、饥饿
Canal realizes MySQL data synchronization
基于时频图的音频处理-matlab
The concept and method of white box test
flutter 线性布局,填充
727. 最小窗口子序列 滑动窗口
数论 —— 整除分块,常见经典例题。
Learn the distributed architecture notes sorted out by Alibaba in one month