注册 登录
编程论坛 C++教室

关于回文素数的问题

realheat 发布于 2009-08-25 18:13, 496 次点击
我自己写的代码如下,为什么出来的结果是1? 那个素数的文件倒是没问题,就是回文那个只出了个1就没了,好像后面的就没读取了。我刚接触文件方面的操作,改了很久都还不行,各位大侠帮帮忙啊,看看哪里出问题了,不胜感激!!!
#include <iostream>
#include <fstream>
using namespace std;
void huiwen10();
void sushu();
int main()
{
    sushu();
    huiwen10();
}

void sushu()
{
    int a=1;
    ofstream out("prime.dat");
    for(int n=1;n<=10000;n++)
    {
        for(int i=2;i<n;i++)
        {

            if(n%i==0)
            {
                a=0;
                break;
            }

        }
        if(a)
        out<<n<<endl;
        a=1;
    }
}
void huiwen10()
{
    long a,temp,b=0;
    ifstream in("prime.dat");
    ofstream out("p10.dat");
    for(a;in>>a;)
    {
        temp=a;
        while(temp>0)
        {
            b=b*10+temp%10;
            temp=temp/10;
        }

        if (b==a)
        {
            cout<<b<<endl;
            out<<b<<endl;
        }
    }

}
1 回复
#2
realheat2009-08-25 19:51
突然自己想到为什么了,,原来忘了把b清零了
1