飛天

快乐的生活......

 

[ACM 1002] 数字游戏

数字游戏
Time Limit:1000MS  Memory Limit:1024K

Description:

最近Catcher对数字游戏很感兴趣,他发明了一种新的游戏,给出一个数N,问N是否能表示成某个正整数X的K次幂(K>1),N可能有多种表示方法,请找出最大的X并输出相应的K。例如 16=2^4=4^2,64=4^3=2^6=8^2则16应表示为4^2,64应表示为8^2。

Input:

每行一个正整数N,输入文件以0为结束标志。(0<N<10^8)

Output:

每行有两个整数,如果能表示,则输出X K,(中间用一个空格隔开)如果不能,则输出0 0;

Sample Input:

5
4
16
27
0

Sample Output:

0 0
2 2
4 2
3 3

Source:

Jin Qiwei
/*
**    Author    :flysky
**  date    :2008-01-20    
**    Description: 
http://acm.zjut.edu.cn/ShowProblem.aspx?ShowID=1002
*/

#include 
<iostream>
#include 
<math.h>
using namespace std;

#define MAX 50

void solve(int number)
{
    
int k=sqrt(number);
    
for(int i=k;i>1;--i)
    
{
        
int j=2;
        
bool loop=true;
        
while(loop)
        
{
            
if(pow(i,j)==number)
            
{
                cout
<<i<<" "<<j<<endl;
                
return;
            }

            
else if(pow(i,j)>number)
                loop
=false;
            
else
                j
++;
        }

    }

    cout
<<0<<" "<<0<<endl;
}


int main()
{
    
int input[MAX];
    
int len=0;
    
while((cin>>input[len])&&input[len]!=0)
    
{
        
if(input[len]<0||input[len]>pow(10,8))
            
continue;
        len
++;
    }

    
for(int i=0;i<len;++i)
        solve(input[i]);
    
return 0;
}

posted on 2008-01-20 13:30 飛天 阅读(1560) 评论(2)  编辑 收藏 引用 所属分类: ACM

评论

# re: [ACM 1002] 数字游戏 2008-08-23 11:03 dengxp

为什么我提交总是通不过,下面是错误提示


(9) : error C2668: “sqrt”: 对重载函数的调用不明确
VC8\include\math.h(581): 可能是“long double sqrt(long double)”
VC8\include\math.h(533): 或“float sqrt(float)”
VC8\include\math.h(128): 或“double sqrt(double)”
试图匹配参数列表“(int)”时
(16) : error C2668: “pow”: 对重载函数的调用不明确
VC8\include\math.h(575): 可能是“long double pow(long double,int)”
VC8\include\math.h(527): 或“float pow(float,int)”
VC8\include\math.h(489): 或“double pow(double,int)”
试图匹配参数列表“(int, int)”时
(21) : error C2668: “pow”: 对重载函数的调用不明确
VC8\include\math.h(575): 可能是“long double pow(long double,int)”
VC8\include\math.h(527): 或“float pow(float,int)”
VC8\include\math.h(489): 或“double pow(double,int)”
试图匹配参数列表“(int, int)”时
(36) : error C2668: “pow”: 对重载函数的调用不明确
VC8\include\math.h(575): 可能是“long double pow(long double,int)”
VC8\include\math.h(527): 或“float pow(float,int)”
VC8\include\math.h(489): 或“double pow(double,int)”
试图匹配参数列表“(int, int)”时
  回复  更多评论   

# re: [ACM 1002] 数字游戏 2008-08-25 09:29 飛天

@dengxp
以上程序是在Dev C++ 4.9.9.2上編譯。
在vs2005里sqrt,pow找不到對應的重載函數,所以要修改一下:
#include "stdafx.h"


#include <iostream>
#include <math.h>
using namespace std;

#define MAX 50

void solve(int number)
{
int k=sqrt((float)number);
for(int i=k;i>1;--i)
{
int j=2;
bool loop=true;
while(loop)
{
if(pow((float)i,j)==number)
{
cout<<i<<" "<<j<<endl;
return;
}
else if(pow((float)i,j)>number)
loop=false;
else
j++;
}
}
cout<<0<<" "<<0<<endl;
}

int main()
{
int input[MAX];
int len=0;
while((cin>>input[len])&&input[len]!=0)
{
if(input[len]<0||input[len]>pow(10.0,8))
continue;
len++;
}
for(int i=0;i<len;++i)
solve(input[i]);
return 0;
}  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

统计

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

Blogs

搜索

最新评论

阅读排行榜

评论排行榜