Number of daffodils |
Difficulty level: A; run time limit: 1000ms; operating space limit: 256000KB; code length limit: 2000000B |
Question Description |
The so-called Narcissus number is a three-digit number, the sum of the cubic of its figures equals itself. Enter a natural number n to output all daffodils with no more than N. If there is no output 0, if there are multiple, from small to large, 22 separated by a space. |
Input |
A positive integer n (no more than 1000). |
Output |
Output according to the requirements of the topic. |
Input example |
200 |
Output example |
153 |
A simple problem of circular judgment. You only need to start the loop from 100 to determine if the condition is met. But excitedly to hand it up, "output format is wrong"?! What kind of metaphysical mistake?!
There is a need to "set up a flag" here. Since multiple sequential output formats can be considered as "number, space, number of spaces, number of spaces, number of spaces ...". So a single flag is to determine whether the next should be output "number" or "number of spaces." At the same time, the flag can be used to determine whether the number of eligible conditions has been found. Because if found, the value of flag must be 1.
Code:
#include <bits/stdc++.h>using namespace Std;bool flag;int i,n;int main () {scanf ("%d", &n); for (i=100;i<=n; i++)//from the smallest three digits to the end of N. {if (i%10) * (i%10) * (i%10) + (i/10%10) * (i/10%10) * (i/10%10) + (i/100%10) * (i/100%10) * (i/100%10) ==i)//Determine if the condition is met. {if (flag) printf ("");//is not the first number to output a space. flag=1;//indicates not the first number. printf ("%d", I);//outputs the number. }}if (!flag) printf ("0");//The output "0" was not found. return 0;}
0067-Number of daffodils