当前位置:网站首页>How to find the number of daffodils with simple and rough methods in C language

How to find the number of daffodils with simple and rough methods in C language

2022-07-06 10:31:00 Yisu cloud

C How to count daffodils in a simple and rough way in language

This article mainly introduces C How to find the relevant knowledge of daffodil number in a simple and rough way in language , The content is detailed and easy to understand , The operation is simple and fast , It has certain reference value , I believe that after reading this article C How to use simple and crude methods to find daffodils in language and count articles will be fruitful , Let's have a look .

What is narcissus number :

Means a n digit , Of its digits n The sum of the powers is exactly equal to the number itself

for example :   

1         1^1=1;

153      3^3+5^3+1^3=153;

problem : seek 0~100000 Between the number of daffodils , And print it out

Look at the topic , Find a breakthrough :

  • 0~100000  I can think of using cycles to judge 0~10000 Between the digital

  • From chestnuts : We need to find out how many digits the number is

  • To use the power , Then we need to quote math In the library pow function

  • Sum up , Still use the loop

So start writing code , Step by step, think by step :

#include <stdio.h>
#include <math.h>
 
int main()
{
	int i = 0;
	for (i = 0; i <= 100000; i++)
	{
		int n = 1;                // n  Is the number of digits 
		int z = i;                // after while after i change ,i Assign a value to z, Replace original value i Carry out the following calculation 
		while (z / 10 != 0)          // It's just beginning to use i Carry out operations    Dead cycle 
		{
			n++;
			z=z / 10;                // use i Operation , When i=10 when  i=i/10  Directly equal to 1 , Dead cycle , So we use variables z Instead of i
		}
		if (i == Sum(i, n))        // Judge whether the sum is equal to the original value 
			printf("%d ",i);
	}
	return 0;
}

Look at the code and be sure to look at the comments later , Here are some points to pay attention to :

It was used n Represents the number of digits , seek n when while The loop changes i value , It is not conducive to the following calculation , So use z Instead of

Set function Sum To calculate and , The function code is as follows :

int Sum(int x, int y)               //x receive i,y receive n
{
	int k = 0;
	int num = 0;
	int sum = 0;
	for (k=0;k<y;k++)             // Circle sum 
	{
		num = pow(x % 10, y);      // Take out the number of each digit , Find the power 
		sum += num;                    // Sum by accumulation 
		x/=10;                  // Remove the lowest number 
	}
	return sum;               // Return and 
}

The complete code is as follows :

#include <stdio.h>
#include <math.h>
 
int Sum(int x, int y)
{
	int k = 0;
	int num = 0;
	int sum = 0;
	for (k=0;k<y;k++)
	{
		num = pow(x % 10, y);
		sum += num;
		x/=10;
	}
	return sum;
}
 
int main()
{
	int i = 0;
	for (i = 0; i <= 100000; i++)
	{
		int n = 1;
		int z = i;
		while (z / 10 != 0)
		{
			n++;
			z=z / 10; 
		}
		if (i == Sum(i, n))
			printf("%d ",i);
	}
	return 0;
}

Running results :

C How to count daffodils in a simple and rough way in language

About “C How to count daffodils in a simple and rough way in language ” That's all for this article , Thank you for reading ! I'm sure you're right “C How to count daffodils in a simple and rough way in language ” Knowledge has a certain understanding , If you want to learn more , Welcome to the Yisu cloud industry information channel .

原网站

版权声明
本文为[Yisu cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131711093648.html