Remainder

1 #include<stdio.h>
2 #include<iostream>
3 #include<string>
4 #include<cstring>
5 #include<queue>
6  using namespace std;
7  struct cc
8 {
9 int n,m;
10 };
11  string op[1005];
12  int step[1005];
13  int vis[1005],ans,k;
14  int main()
15 {
16 int n,m,ok,flag;
17 while(scanf("%d%d%d",&n,&k,&m)==3)
18 {
19 ok=0;
20 if(!n&&!m&&!k)break;
21 memset(step,0,sizeof(step));
22 memset(vis,0,sizeof(vis));
23 ans=(n+1+1000*k)%k;
24 for(int i=0;i<=1001;i++)
25 op[i].clear();
26 queue <cc> q;
27 cc c;
28 c.m=(n+1000*m)%m;//注意n=-1和n=-1+10(k)做%运算结果不一样,所以事先存n%m;当以后遇到%时c.n=c.m%m;
29 c.n=(n+1000*k)%k;
30 q.push(c);
31 vis[c.n]=1;
32 while(!q.empty())
33 {
34 cc temp=q.front();
35 q.pop();
36 for(int i=0;i<4;i++)
37 {
38 cc u=temp;
39 switch(i)
40 {
41 case 0:u.n=(u.n+m+1000*k)%k;break;
42 case 1:u.n=(u.n-m+1000*k)%k;break;
43 case 2:u.n=(u.n*m+1000*k)%k;u.m=0;break;//u.n*mºó£¬u.n%m==0£»ÒÔºóÓöµ½%ÔËËãºóu.n==0!=u.m%m;
44 case 3:u.n=(u.m+1000*k)%k;break;
45 }
46 if(!vis[u.n])
47 {
48 step[u.n]=step[temp.n]+1;
49 vis[u.n]=1;
50 switch(i)
51 {
52 case 0:op[u.n]=op[temp.n]+'+';break;
53 case 1:op[u.n]=op[temp.n]+'-';break;
54 case 2:op[u.n]=op[temp.n]+'*';break;
55 case 3:op[u.n]=op[temp.n]+'%';break;
56 }
57 q.push(u);
58 if(u.n==ans){ok=1;flag=u.n;break;}
59 }
60
61 }
62 if(ok)break;
63 }
64 if(!ok)printf("0\n");
65 else
66 {
67 int t=0;
68 printf("%d\n",step[flag]);
69 cout<<op[flag]<<endl;
70 }
71 }
72 }
不容易啊,这年头,抄个代码也要找好多才能找到一个真正可以AC的
我的WA的DFS,果断超时
1 #include <iostream>
2 using namespace std;
3
4 int n, k, m, count, mmin;
5 bool flag;
6 const int N = 1010;
7 bool visited[N];
8 char path[N];
9 char finalP[N];
10 char op[5] = "+-*%";
11
12 void getValue(int arr[], int value)
13 {
14 arr[0] = (value + m) % k;
15 arr[1] = (value - m) % k;
16 arr[2] = (value * m) % k;
17 arr[3] = (value % m) % k;
18 }
19
20 void dfs(int num, int count)
21 {
22 if(num == (n + 1) % k && count < mmin)
23 {
24 mmin = count;
25 for(int i = 0; i < count; ++i)
26 finalP[i] = path[i];
27 flag = true;
28 }
29 int arr[4];
30 getValue(arr, num);
31 for(int i = 0; i < 4; ++i)
32 {
33 if(visited[arr[i]] == false)
34 {
35 visited[arr[i]] = true;
36 path[count++] = op[i];
37 dfs(arr[i], count);
38 count--;
39 visited[arr[i]] = false;
40 }
41 }
42 }
43
44 int main()
45 {
46 while(cin >> n >> k >> m)
47 {
48 if( n == 0 && k == 0 && m == 0)
49 break;
50 ::count = 0;
51 ::mmin = 1 << 30;
52 visited[n] = true;
53 flag = false;
54 dfs(n, 0);
55 if(flag == false)
56 cout << 0 << endl;
57 else
58 {
59 cout << ::mmin << endl;
60 for(int i = 0; i < :: mmin; ++i)
61 cout << finalP[i];
62 cout << endl;
63 }
64 }
65 return 0;
66 }
我的WA的BFS,果断超内存,泪奔。。。
1 #include <iostream>
2 #include <queue>
3 using namespace std;
4
5 int n, k, m;
6 const int N = 1010;
7 char op[5] = "+-*%";
8 struct Node{
9 int num;
10 char op[N];
11 bool v[N];
12 int count;
13 };
14 void getValue(int arr[], int value)
15 {
16 arr[0] = (value + m) % k;
17 arr[1] = (value - m) % k;
18 arr[2] = (value * m) % k;
19 arr[3] = (value % m) % k;
20 }
21
22 void bfs()
23 {
24 bool flag = false;
25 queue<Node> q;
26 Node a, b;
27 a.num = n;
28 for(int i = 0; i < k; ++i)
29 {
30 a.v[i] = false;
31 a.op[i] = ' ';
32 }
33 a.v[n] = true;
34 a.count = 0;
35 q.push(a);
36 int arr[4];
37 while(!q.empty())
38 {
39 a = q.front();
40 q.pop();
41 if(a.num == (n + 1) % k)
42 {
43 flag = true;
44 while(!q.empty())
45 q.pop();
46
47 cout << a.count << endl;
48 for(int i = 0; i < a.count; ++i)
49 cout << a.op[i];
50 cout << endl;
51 }
52 getValue(arr, a.num);
53 for(int i = 0; i < 4; ++i)
54 {
55 if(a.v[arr[i]] == false)
56 {
57 b.num = arr[i];
58 for(int i = 0; i < k; ++i)
59 {
60 b.op[i] = a.op[i];
61 b.v[i] = a.v[i];
62 }
63 b.v[arr[i]] = true;
64 b.op[a.count] = op[i];
65 b.count = a.count + 1;
66 q.push(b);
67 }
68 }
69 }
70 if(!flag)
71 cout << 0 << endl;
72 }
73 int main()
74 {
75 while(cin >> n >> k >> m)
76 {
77 if( n == 0 && k == 0 && m == 0)
78 break;
79 bfs();
80 }
81 return 0;
82 }
posted @ 2011-07-11 15:52  georgechen_ena  阅读(150)  评论(0)    收藏  举报