Halting Problem
Halting Problem:
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4049
总结一个小规律:题目中给的那个取mod的那个数N, 可能就是循环的边界;
本题AC代码:
1 /* */ 2 # include <bits/stdc++.h> 3 4 using namespace std; 5 const int N = 1e4+10; 6 7 struct node 8 { 9 char s[10]; 10 int v, k; 11 } a[N]; 12 13 int vis[N]; 14 15 int main() 16 { 17 int t; 18 scanf("%d", &t); 19 while( t-- ) 20 { 21 memset(vis,0,sizeof(vis)); 22 int n; 23 scanf("%d", &n); 24 for( int i=1; i<=n; i++ ) 25 { 26 scanf("%s", a[i].s); 27 if( strcmp(a[i].s, "add") != 0 ) 28 { 29 scanf("%d %d", &a[i].v, &a[i].k); 30 } 31 else 32 { 33 scanf("%d", &a[i].v); 34 } 35 } 36 int r = 0; 37 int flag=1; 38 for( int i=1; i<=n; i++ )///跳出循环的第一个条件 39 { 40 vis[i]++; 41 if( vis[i]>256 )///重点!!!终止循环的条件,每一步最多走256次,否则就陷入了死循环 42 { 43 flag = 0; 44 break; 45 } 46 if( strcmp(a[i].s, "add")==0 ) 47 { 48 r = (r+a[i].v)%256; 49 } 50 else if( strcmp(a[i].s, "beq")==0 ) 51 { 52 if( r==a[i].v ) 53 { 54 i = a[i].k - 1;///要跳到第k步,但是for循环的时候i还会++,所以i要先到(k-1)步 55 } 56 } 57 else if( strcmp(a[i].s, "bne")==0 ) 58 { 59 if( r!=a[i].v ) 60 { 61 i = a[i].k - 1; 62 } 63 } 64 else if( strcmp(a[i].s, "blt")==0 ) 65 { 66 if( r<a[i].v ) 67 { 68 i = a[i].k - 1; 69 } 70 } 71 else if( strcmp(a[i].s, "bgt")==0 ) 72 { 73 if( r>a[i].v ) 74 { 75 i = a[i].k - 1; 76 } 77 } 78 } 79 if( flag ) 80 { 81 printf("Yes\n"); 82 } 83 else 84 { 85 printf("No\n"); 86 } 87 } 88 return 0; 89 }