随笔 - 87  文章 - 279  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

潜心看书研究!

常用链接

留言簿(19)

随笔分类(81)

文章分类(89)

相册

ACM OJ

My friends

搜索

  •  

积分与排名

  • 积分 - 211865
  • 排名 - 116

最新评论

阅读排行榜

评论排行榜

原题: A Simple Task
Given a positive integer n and the odd integer o and the nonnegative
integer p such that n = o2^p.


Example

For n = 24, o = 3 and p = 3.


Task

Write a program which for each data set:

reads a positive integer n,

computes the odd integer o and the nonnegative integer p such that n = o2^p,

writes the result.


Input

The first line of the input contains exactly one positive integer d
equal to the number of data sets, 1 <= d <= 10. The data sets follow.

Each data set consists of exactly one line containing exactly one
integer n, 1 <= n <= 10^6.


Output


Line i, 1 <= i <= d, corresponds to the i-th input and should contain two
integers o and p separated by a single space such that n = o2^p.


Sample Input

1
24


Sample Output

3 3

 
#include<iostream>
#include
<cmath>
using namespace std;

int main()
{
    
int set_num;
    
int *set = NULL;
    
int i,j,k;
    
int temp;
    
bool flag;
    cin
>>set_num;
    
set = new int[set_num];
    
for (i = 0;i<set_num;i++)
        cin
>>set[i];

    
for (i = 0;i<set_num;i++)
    
{
        
if (set[i]%2!=0)
        
{
            cout
<<set[i]<<' '<<0<<endl;
            
continue;
        }

        flag 
= false;
        
for (j = 1;j<=set[i]/2;j+=2)
        
{
            temp 
= 0;
            k
=1;
            
while(temp<set[i])
            
{
                temp 
= j*pow(2,k);
                
if (temp==set[i])
                
{
                    cout
<<j<<' '<<k<<endl;
                    flag 
= true;
                    
break;               
                }

                
else
                    k
++;
            }

            
if (flag)
                
break;
        }

    }



    
return 0;
}


上面是我提交的程序
在zju提交编译错误一次  因为标准c++   pow函数为 pow(double,<type>)第一个参数必须为double,但是我再shantou 上用pow(int,int)就过了,都是编译器惹的祸...
posted on 2006-02-08 23:40 阅读(891) 评论(14)  编辑 收藏 引用 所属分类: 算法&ACM

FeedBack:
# re: 今天又过条简单题,呵呵 2006-02-09 19:09 dzz
帮我看看
http://acm.pku.edu.cn/JudgeOnline/showproblem?problem_id=2271

怎么判断输入结束
怎么用简单的方法操作字符串,要建立一个放字符的class吗?
我的代码在机子上测试数据都没完全通过
#define SZ 80

#include <iostream>
#include <string>
#include <strstream>
#include <fstream>
using namespace std;
char buf[SZ];
char* p;
int dis=0;

main(){
ostrstream cl;
string st;
ofstream s("temp.txt");
char a;
while(getline(cin,st,'\n')){
s<<st;
a=getchar();
if (a=='\n') break;
else s<<a;

};
s<<ends;
s.close();

ifstream o("temp.txt");
while(o.get(buf,SZ)){
if (strstr(buf,"<br>")) {
dis = (strstr(buf,"<br>") - buf);
o.seekg(dis-80+6,ios::cur);
for (int ctr=0;ctr<dis;ctr++) cl<<buf[ctr];
cl<<endl;
}
else if(strstr(buf,"<hr>")){
dis = (strstr(buf,"<hr>") - buf);
o.seekg(dis-80+6,ios::cur);
for (int ctr=0;ctr<dis;ctr++) cl<<buf[ctr];cl<<endl; for (int ctr=0;ctr<80;ctr++) cl<<'-';

}
else if(strstr(buf," ")){

}
else cl<<buf<<endl;
}

cout<<cl.rdbuf()<<endl;


}  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-10 01:12 
判断结束用while(cin>>str) {...} 然后对每个str操作行否?

题目只有<br> <hr> 和80字换行,对这三种规则分别操作,就应该可以了

acm用class小题大作拉,还有,线上题目好象不能用文件的
  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-10 13:59 Dzz
#include <strstrea.h>
#include <iostream.h>

main(){
char str[80];
ostrstream os;
while (cin>>str) {os<<str;}
cout << os.rdbuf();
}

这个怎么也结束不了  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-10 18:06 
当没有输入,就应该结束的  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-11 19:28 
我在zju上过了

#include<iostream>
#include<string>
using namespace std;

int main()
{
const char br = '\n' ;
char hr[82];
char str[81];
int lineCharNum = 0;
int charLength = 0;
int i;
bool flag;
for (i=0;i<80;i++)
hr[i] = '-';
hr[i] = '\n';
hr[i+1] = '\0';

while (cin>>str)
{
flag = true;
if(strcmp(str,"<br>")==0)
{
cout<<br;
lineCharNum = 0;
flag = false;
continue;
}
if(strcmp(str,"<hr>")==0)
if(lineCharNum==0)
{
cout<<hr;
flag = false;
continue;
}
else
{
cout<<br<<hr;
flag = false;
lineCharNum = 0;
continue;
}
charLength = strlen(str);

if (lineCharNum==0)
{
cout<<str;
lineCharNum = charLength;
}
else if (charLength+lineCharNum+1<=80)
{
cout<<' '<<str;
lineCharNum += charLength+1;
}
else
{
cout<<br<<str;
lineCharNum = charLength;
}
}
if (flag) cout<<endl;
return 0;
}  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-11 23:39 dzz
谢谢,我好好看看~  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-11 23:56 dzz
看懂了,谢谢,你的代码上如果一个单词超出了该行,就换行再输出单词,如果想要把它拆开,应该如何改呢?

还有就是
if (flag) cout<<endl;
return 0;

这两个语句不知道什么情况程序可以运行到这里??
我们交换一下blog链接吧,我已经添加你了~
我的Http://www.dzzsoft.net/blog2/  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-12 14:26 
if (flag) cout<<endl;
return 0;

其实用vc 这句好像执行不了的,我也不知道,不过估计linux下的编译器能:)我是按照逻辑这样写,想不到能过:)  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-12 16:04 Dzz
你那样结不结束都能得到正确答案嘛~  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-12 18:06 
不能。。。因为题目要求最后 一个换行符 作为结束  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-18 18:07 hhh
真强,类存泄漏~~~  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2006-02-19 01:27 
是啊,忘记了,不可原谅的错误!~
  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2007-03-07 16:20 阿七
你的程序好吓人啊,我是新手,刚才被你的A+B吓着了。这是我做的simple task,为什么你要编那么多呢,严谨?我没仔细看。
#include <iostream>
using namespace std;

int main()
{
int n,o,p=0;
cin >> n;
o = n;
while(o % 2 == 0)
{
o = o / 2;
p++;
}
cout << n <<"=" << o << "*" << "2^" << p << endl;
return 0;
}  回复  更多评论
  
# re: 今天又过条简单题,呵呵 2007-03-07 22:10 
呵呵,因为那个时候我也是新手嘛,不过现在虽然老了,还是一只老菜鸟  回复  更多评论
  

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