再切一题冲进9000名。---附注:POJ 3126 解题总结

题目不难,但提交了11次才AC,害得我 一天都在A一道题,而且还这么简单的题目。丢人啊!!!
遂有感叹:人世间,最难过的事情莫过于---通过了测试,但还是WA。

最后的问题,竟然出在 判断素数的一个“=”号上,伤心!!


Source Code





















Problem: 3126 User: omycle
Memory: 288K Time: 63MS
Language: C++ Result: Accepted



  • Source Code
    #include <iostream>
    #include <math.h>
    using namespace std;
    #define SIZE 10000
    bool flag[SIZE];//非顺序存储
    int step[SIZE];
    int duilie[SIZE];
    int tou=0;
    int wei=0;
    bool IsPrime(const int n)
    {//判断素数
    double temp=sqrt((double)n);
    int i=2;
    while(i<=temp)
    {
    if(n%i==0)
    return false;
    i++;
    }
    return true;
    }
    void Path(int n,int finish)
    {
    memset(flag,false,SIZE*sizeof(bool));
    memset(duilie,0,SIZE*sizeof(int));
    memset(step,0,SIZE*sizeof(int));
    tou=0;
    step[tou]=0;
    duilie[tou]=n;
    flag[duilie[tou]]=true;
    if(n==finish)
    {
    printf("0\n");
    // fq<<0<<endl;
    return ;
    }
    wei=1;
    while(wei>tou)
    {
    int temp=duilie[tou];
    for(int i=0;i<4;i++)
    {
    temp=duilie[tou];
    if(i==0)
    {
    temp=temp%1000;//去掉千位
    }
    else if(i==1)
    {
    temp=(temp/1000)*1000+temp%100;//去掉百位
    }
    else if(i==2)
    {
    temp=(temp/100)*100+temp%10;
    }
    else if(i==3)
    {
    temp=(temp/10)*10;
    }
    for(int j=0;j<=9;j++)
    {
    if(temp<=9999&&temp>=1000&&!flag[temp]&&IsPrime(temp))
    {
    flag[temp]=true;
    duilie[wei]=temp;
    step[wei]=step[tou]+1;
    if(duilie[wei]==finish)
    {
    printf("%d\n",step[wei]);
    return;
    }
    wei++;
    }
    if(i==0)
    {
    temp+=1000;//在千位上加
    }
    else if(i==1)
    {
    temp+=100;//在百位上加
    }
    else if(i==2)
    {
    temp+=10;//在十位上加
    }
    else if(i==3)
    {
    temp+=1;//在个位上加
    }
    }//end for
    }
    tou++;
    }
    printf("Impossible\n");
    }
    int main()
    {
    int n;
    scanf("%d",&n);
    while(n--)
    {
    int m,i;
    scanf("%d %d",&m,&i);
    Path(m,i);
    }
    return 0;
    }



评论

发表评论

此博客中的热门博文

Linux/ARM Page Table Entry 属性设置分析

由RFE指令引发的一串故事

提交了30次才AC ---【附】POJ 2488解题报告