while == isp 一个数 targe define case stream
传送门:https://vjudge.net/problem/UVA-524
回溯法深搜,我的硬是不知道哪里错了,和别人 AC 的程序输出一模一样
- #include#include#include using namespace std;
- const int maxn = 50;
- int n;
- int isp[maxn];
- int vis[maxn];
- int A[maxn] = {
- 1
- };
- void pre() {
- memset(vis, 0, sizeof(0));
- for (int i = 0; i <= maxn; i++) {
- isp[i] = 1;
- }
- for (int i = 2; i <= maxn; i++) {
- for (int j = i + i; j <= maxn; j += i) {
- isp[j] = 0;
- }
- }
- isp[0] = 0;
- isp[1] = 0;
- }
- //bool IsPrime( int num )
- //{
- // int tmp = sqrt( num);
- // for(int i= 2;i <=tmp; i++)
- // if(num %i== 0)
- // return 0 ;
- // return 1 ;
- //}
- void dfs(int cur) {
- if (cur == n && isp[A[0] + A[n - 1]]) {
- for (int i = 0; i < n; i++) {
- i ? printf(" %d", A[i]) : printf("%d", A[i]);
- }
- printf("\n");
- }
- //A[cur-1]相邻的上一个数,逆时针
- //A[cur]为当前数
- //枚举每一个没访问过的数
- else for (int i = 2; i <= n; i++) {
- if (!vis[i] && isp[i + A[cur - 1]]) {
- A[cur] = i;
- vis[i] = 1;
- dfs(cur + 1);
- vis[i] = 0;
- }
- }
- }
- int main() {
- // freopen("in.txt","r",stdin);
- // freopen("out.txt","w",stdout);
- int casen = 0;
- while (~scanf("%d", &n)) {
- if (casen != 0) printf("\n");
- pre();
- printf("Case %d:\n", ++casen);
- dfs(1);
- // printf("\n");
- }
- return 0;
- }
别人 AC 的程序:
- #include#include#include < string > #include#define MAXN 50 using namespace std;
- int n,
- A[MAXN] = {
- 1
- },
- ispe[MAXN],
- vis[MAXN];
- void dfs(int cur) {
- if (cur == n && ispe[A[0] + A[n - 1]]) {
- for (int i = 0; i < n; i++) {
- i ? printf(" %d", A[i]) : printf("%d", A[i]);
- }
- printf("\n");
- } else for (int i = 2; i <= n; i++) {
- if (!vis[i] && ispe[i + A[cur - 1]]) {
- A[cur] = i;
- vis[i] = 1;
- dfs(cur + 1);
- vis[i] = 0;
- }
- }
- }
- int main() {
- // freopen("in.txt","r",stdin);
- // freopen("out.txt","w",stdout);
- for (int i = 2; i <= 50; i++) ispe[i] = 1;
- for (int i = 2; i <= 50; i++) for (int j = i + i; j + i <= 50; j += i) ispe[j] = 0;
- int kase = 0;
- while (cin >> n) {
- if (kase++) printf("\n");
- printf("Case %d:\n", kase);
- dfs(1);
- }
- return 0;
- }
我把输出复制在一个文件里,最后一个空行去掉,然后专门用下面程序判断两个输出是否一样,结果式样的,输出为空
- #include#include using namespace std;
- char str1[90000][50];
- char str2[90000][50];
- int main() {
- freopen("in.txt", "r", stdin);
- for (int i = 0; i < 85065; i++) gets(str1[i]);
- for (int i = 0; i < 85065; i++) gets(str2[i]);
- for (int i = 0; i < 85065; i++) {
- if (strcmp(str1[i], str2[i]) != 0) printf("%d\n", i + 1);
- }
- return 0;
- }
Uva 524 相邻素数全排列
来源: http://www.bubuko.com/infodetail-2222608.html