c ++不同平台上的不同答案

时间:2015-06-04 09:33:22

标签: c++ codeblocks

您好我在Codechef的IDE和Codeblocks上运行了此代码。 我得到了不同的答案: CODEBLOCK为1001 818(正确之一)Codechef IDE。 请帮忙!

代码snipplet:http://pastie.org/10223010

#include <iostream>
#include <string>
#include<sstream>
#include<cmath>


using namespace std;
int palin(int n);
int main()
{
    int i,t;
    int n;
    cin>>t;
    for(i=0;i<t;i++){
    cin>>n;
    palin(n);

while(1){
        n++;
    if(palin(n)==1){
        cout<<n<<endl;
        break;
    }

}
    }
    return 0;
}

int palin(int n){
    int len=0;
    int m=0;
    int dum=n;
    while(n!=0){
        n=n/10;
        len++;

    }
    n=dum;

  while(n!=0){
    m=m+((pow(10,(len-1)))*(n%10));
    n=n/10;
    len--;

  }


    if(dum==m)
        return 1;
    else
        return 0;
}

问题陈述:http://www.codechef.com/problems/PALIN/

1 个答案:

答案 0 :(得分:0)

不要使用pow()函数,因为它使用浮点运算并且使用整数变量给错误答案

而是使用自定义幂函数作为整数变量,如

int pw(int a, int b){
    int ans = 1;
    while(b--){
        ans *= a;
    }
    return ans;
}