现在的位置: 首页 > 综合 > 正文

POJ2105解题报告

2018年10月25日 ⁄ 综合 ⁄ 共 1048字 ⁄ 字号 评论关闭

 又继续奋斗水题,发现又学了点东西(好像都是细节问题),题目不难,自己的AC代码:

#include<iostream>
#include<math.h>
using namespace std;
int main()
{int n,i,j,k,s;
char a[4][8];
cin>>n;
for(i=0;i<n;i++)
{for(j=0;j<4;j++)
for(k=0;k<8;k++)
cin>>a[j][k];
for(j=0;j<4;j++)
{s=0;
for(k=0;k<8;k++)
s+=pow(float(2*(a[j][k]-48)),7-k);
if(a[j][7]=='0')  //因为pow(0,0)=1。
s--;
j==3?cout<<s:cout<<s<<".";}
cout<<endl;}
return 0;}
这里有两点值得注意:1.调用pow函数要在第一个实参前加float(刚开始不加Compile error N次 郁闷。),而在自己的VC6.0上运行却没问题,看来不同编译器在细节处理上还是有不少差别。。

2.就是pow(0,0)原来是等于1的,所以没办法之下只能再加个条件语句if(a[j][7]=='0')s--;了。

 然后百度看看其他人的解法,发现有个用bitset函数的,觉得值得学习下,这里发下:

#include<iostream>
#include<string>
#include<bitset>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string s;
    while(n>0)
    {
        cin>>s;   
        bitset<8> one(s,0,8);
        bitset<8> two(s,8,8);
        bitset<8> three(s,16,8);
        bitset<8> four(s,24,8);
        cout<<one.to_ulong()<<"."<<two.to_ulong()<<"."<<three.to_ulong()<<"."<<four.to_ulong()<<endl;
        n--;       
    }
    return 0;
}

然后自己改下这个代码把string s;改成char s[32]后发现无论之前输入n是多少都好,执行cin>>s输入32个数字这之后n就是0了,而把s[32]改成s[33]就可以了,这是什么原因呢???奇怪,待以后回来解决。

抱歉!评论已关闭.