C++ 4. Find exponential power

Insert picture description hereInsert picture description here
After understanding the meaning of the question, you will find that this question uses only very basic knowledge points. After several debugging, the code is as follows:

//找幂指数
#include<cmath>
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    
    
	int n;
	double p;
	while(scanf("%d%lf",&n,&p) !=EOF){
    
    
		for(double i=1;i<=pow(double(10),9);i++){
    
    
			if(pow(i,n)==p){
    
    
				printf("%d\n",int(i));
			}
			else{
    
    
				continue;
			}
		}
	}
	return 0;	 
} 

input Output:

Insert picture description here
Debugging report:
1. The parameter type of the
pow () function is unclear a.long double pow(long double,int)
b.float pow(float,int)
c.double pow(double,int)
One thing is beyond doubt, The return value of pow() is a double-precision or floating-point type, not an integral type, and its parameter types seem to have no restrictions now:
Insert picture description here
it is easy to understand, and practice is the only criterion for testing truth. It is easier to understand. Not much to say, I suggest you try it yourself. When you want to get an integer, you can only return 0.
2. Note that when inputting, make sure that the correct type is matched after %.
Disadvantages:
Although you can get the desired answer normally, It is a relatively large project to traverse this range once, and it is not necessary, so it is displayed when submitting:

Time Limit Exceeded

This is not an efficient algorithm!
So make the following changes:

//找幂指数
#include<cmath>
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    
    
	double n,p;
	while(scanf("%lf%lf",&n,&p) !=EOF){
    
    
		printf("%.lf\n",pow(p,1/n));
	}
	return 0;	 
} 

The number involved here is larger, so use a higher precision double precision, look at an input and output:

//输入:2 16
//输出:4

If you will:

printf("%.lf\n",pow(p,1/n));

To:

printf("%lf\n",pow(p,1/n));

input Output:

2 16
4.000000

Just remove a dot, the output format is very different, if there is no dot, it will be output according to the default value of double-precision decimal places, and adding the dot ID, although there is no specified number of digits, is equivalent to keeping the zero position. Decimals!
At first I forgot to add a newline character when outputting, and the test system reported an error:

Presentation Error

The answer is very close to the standard result, that is to say, the most likely reason is that there are more or less unnecessary spaces or carriage returns in the output result. The OJ platform checks the format very strictly, so you must carefully check the program. Is the output result consistent with the standard! Otherwise, it is only one step away from success. What a pity, maybe this is the details that determine success or failure!
Come on with ZDZ!
The flower of success, people only admired her now bright and beautiful! However, its buds were soaked in the tears of struggle and sprinkled with the blood of sacrifice.
-----Bing Xin

Guess you like

Origin blog.csdn.net/interestingddd/article/details/113648818