| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
跪求大牛指出错误#include<stdio.h> #include<string.h> #include<queue> #include<cmath> #include<iostream> using namespace std; int q[10005]; int r[10006]; int c; int qq(int e) { for(int i=2;i*i<e;i++) { if(e%i==0) return 0; } return 1; } void yy() { memset(q,0,sizeof(q)); for(int i=1000;i<=9999;i++) { q[i]=qq(i); } } int show(int num, int n){ int m[5], i; for(i = 1; i <= 4; i ++){ m[i] = num % 10; num = num / 10; } m[n] = 0; for(i = 1; i <= 4; i ++) { int w=1; for(int k=0;k<i-1;k++) w=w*10; num += m[i] *w; } return num; } int main() { int a,b; yy(); scanf("%d",&a); while(a--) { scanf("%d%d",&b,&c); if(b==c) { printf("0\n"); continue; } int fine = 0; int steps = 0; queue<int> pri; pri.push(b); memset(r,0,sizeof(r)); r[b]=1; while(!pri.empty()&&!fine) { int t=pri.size(); steps++; while(t--) { int num=pri.front(); pri.pop(); for(int i=1;i<=4;i++) { int n = show(num, i); int w=1; for(int k=0;k<i-1;k++) w=w*10; for(int j=0;j<=9;j++) { if(i==4&&j==0) continue; int now=n+j*w; if(now==c) { fine=true; break; } else if(q[now]&&!r[now]) { pri.push(now); r[now]=1; } } } } } if(fine) printf("%d\n",steps); else printf("Impossible\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator