C/C++ Programming Learning-Week 3 ④ Number of Daffodils

Topic link

Title description

Spring is the season of flowers, and daffodils are the most fascinating representative of them. There is a daffodil number in mathematics. He defines it like this: "Daffodil number" refers to a three-digit number, and the cube sum of its digits is equal By itself, for example: 153 = 1 3 + 5 3 + 3 3 .

Now it is required to output the number of all daffodils in the range of m and n.

There
are multiple groups of input data, each group occupies one line, including two integers m and n (100 <= m <= n <= 999).

Output
For each test instance, it is required to output all the number of daffodils within a given range, that is, the number of daffodils output must be greater than or equal to m and less than or equal to n. If there are more than one, it is required to arrange from small to large Output in one line, separated by a space; if the number of daffodils does not exist in the given range, output no.

The output of each test instance occupies one line.

Sample Input

100 120
300 380

Sample Output

no
370 371

Ideas

Just simulate the operation of the question. According to the title description, the daffodil number refers to a three-digit number, and the sum of the cubes of its digits is equal to itself.

C language code:

#include<stdio.h>
#include<math.h>
int check(int i)
{
    
    
	int x = i;
	int x1 = x % 10;
	x = x / 10;
	int x2 = x % 10;
	x = x / 10;
	int x3 = x % 10;
	if((x1 * x1 * x1 + x2 * x2 * x2 + x3 * x3 * x3) == i) return 1;
	else return 0;
}
int main()
{
    
    
	int m, n;
	while(~scanf("%d %d", &m, &n))
	{
    
    
		int flag = 1;
		int cnt = 0;
		for(int i = m; i <= n; i++)
		{
    
    
			if(check(i))
			{
    
    
				cnt++;
				if(flag)
				{
    
    
					flag--;
					printf("%d", i);
				}else printf(" %d", i);
			}
		}
		if(cnt == 0) printf("no\n");
		else printf("\n");
	}
	return 0;
}

C++ code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	ios::sync_with_stdio(false);	//加速读入
	int m, n, cnt;
	while(cin >> m >> n)
	{
    
    
		cnt = 0;
		for(int i = m; i <= n; i++)
		{
    
    
			int a = i / 100;	//百位 
			int b = i / 10 % 10;//十位 
			int c = i % 10;		//个位 
			if(i == a * a * a + b * b * b + c * c * c)
			{
    
    
				if(cnt == 0) cout << i;
				else cout << " " << i;
				cnt++;
			}
		}
		if(cnt == 0) cout << "no";
		cout << endl;
	}
	return 0;
}

The function of the following sentence is to speed up the reading of data, even without writing.

ios::sync_with_stdio(false);

In fact, there are 4 daffodils in total: 153,370,371,407.

So we can also write:

#include<iostream>
using namespace std;
int main()
{
    
    
	int m, n, cnt = 0;
	while(scanf("%d%d", &m, &n) != EOF)
	{
    
    
		for(int i = m; i <= n; i++)
			if(i == 153 || i == 370 || i == 371 || i == 407)
			{
    
    
				if(cnt == 0)
				{
    
    
					printf("%d", i);
					cnt++;
				}
				else if(cnt > 0) printf(" %d", i);
			}
		cnt == 0 ? printf("no\n") : printf("\n");
		cnt = 0;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/112860273