当前位置:网站首页>For cycle: daffodil case
For cycle: daffodil case
2022-07-25 10:22:00 【anerl】
Daffodils case :
demand : Output all... In the console “ Narcissistic number
Problem description
What is? “ Narcissistic number ”?
The Narcissus count is a three digit number The number of daffodils 、 ten 、 The sum of the hundreds of digit cubes is equal to the original number Such as 123 :13+23+33=1+8+27=36±123 Not Narcissus Such as 371 :33+73+13=27+343+1=371=371 It's Narcissus
analysis ( use 371 Example )
How to find three digit numbers :
371 :1 It's the original number pair 10 The result of the remainder operation 371%10=1
How to find a three digit hundred digit number :
371 :3 Is the original number divided by 100 Result ( to be divisible by )371/100=3
How to find three digit ten digit numbers :
371 :371 By dividing by 10, Can be 7 Move to position ( Integers ) 371/10=3737 Through to 10 The value of the last digit can be obtained by performing the remainder operation 7 37%10=7 summary :371/ 10% 10 = 7
reflection : How to find the value in the specified bit of any number
First use the integer division operation to move the required number to one digit , Then use the remainder operation to get the value on the last bit 123456789, Divide first 10000 obtain 12345, Right again 10 Get surplus 5
Code :
public class forTest03 {
public static void main(String[] args) {
// Output the number of all daffodils : Traverse all three digits
for (int i =100;i<1000;i++){
// Get the value on each of the three digits before calculating
int ge = i%10;
int shi = i/10%10;
int bai = i/10/10%10;
// The judgment condition is to take the value on each of the three digits , After calculating the cube sum, compare it with the original number to see if it is equal
if (ge*ge*ge + shi*shi*shi +bai*bai*bai ==i){
// The number that meets the conditions is daffodil
System.out.println(i);
}
}
}
}
Comprehensive analysis of :
1. First establish a cycle
2. Take out the tens and hundreds of the three digits
3. Calculate whether the conditions are met , Output when conditions are met
边栏推荐
猜你喜欢
随机推荐
四、TestFixture测试夹具,或者测试固件
Dataset 和 Dataloader数据加载
Swing的组件图标
Mysql离线部署
多线程——五大状态
将 conda 虚拟环境 env 加入 jupyter kernel
Copy the old project into a web project
Number theory -- Research on divisor
Mouse monitor, Paintbrush
UE4源码的获取和编译
复现 ASVspoof 2021 baseline RawNet2
异常处理Exception
Open virtual private line network load balancing
About the jar package of slf4j log4j log4j2 used together
Erlang(离线部署)
VSCode Latex Workshop 设置 XeLatex 编译
多线程——静态代理模式
@Import, conditional and @importresource annotations
struct2的原理
Erlang (offline deployment)









