Kaidul

UVa 10400

Nov 5th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<string>
  5.  
  6. using namespace std;
  7.  
  8. #define Max 100
  9. #define Range 32000
  10.  
  11. int N, target, data[Max];
  12. bool dp[Max][2 * Range + 1];
  13.  
  14. bool valid(int a) {
  15.         return ( a >= -Range and a <= +Range );
  16. }
  17.  
  18. bool solve(int indx, int value, string expression) {
  19.     if(indx == N) {
  20.         if(value == target) {
  21.             printf("%d", data[0]);
  22.             for (int i = 0, n = expression.length(); i < n; i++) {
  23.                     printf("%c%d", expression[i], data[i + 1]);
  24.             }
  25.             printf("=%d\n", target);
  26.             return true;
  27.         }
  28.         return 0;
  29.     }
  30.     if( dp[indx][value] )
  31.         return false;
  32.     dp[indx][value] = true;
  33.  
  34.     if( valid(value + data[indx]) and solve(indx + 1, value + data[indx], expression + '+' ) )
  35.         return true;
  36.     if( valid(value - data[indx]) and solve(indx + 1, value - data[indx], expression + '-' ) )
  37.         return true;
  38.     if( valid(value * data[indx]) and solve(indx + 1, value * data[indx], expression + '*' ) )
  39.         return true;
  40.     if(data[indx] != 0 and value % data[indx] == 0 and valid(value / data[indx]) and solve(indx + 1, value / data[indx], expression+ '/' ) )
  41.         return true;
  42.  
  43.     return false;
  44. }
  45.  
  46. int main(void) {
  47. #ifndef ONLINE_JUDGE
  48.     freopen("input.txt", "r", stdin);
  49. #endif
  50.     int tcase;
  51.     scanf("%d",&tcase);
  52.     while (tcase--) {
  53.         scanf("%d", &N);
  54.         for (int i = 0; i < N; i++) {
  55.             scanf("%d", data + i);
  56.         }
  57.         scanf("%d", &target);
  58.         memset(dp, false, sizeof dp);
  59.         if( !solve (1, data[0], "") ) {
  60.             printf("NO EXPRESSION\n");
  61.         }
  62.     }
  63. }
  64.  
  65. /** --------------------------
  66. Input:
  67. 11
  68. 3 5 7 4 3
  69. 2 1 1 2000
  70. 5 12 2 5 1 2 4
  71. 2 32000 2 16000
  72. 2 32000 32000 1
  73. 9 9 1 2 -10 33 3 3 2 2 23
  74. 50 9 1 2 -10 33 3 3 2 2 23 9 1 2 -10 33 3 3 2 2 23 9 1 2 -10 33 3 3 2 2 23 9 1 2 -10 33 3 3 2 2 23 9 1 2 -10 33 3 3 2 2 23 109
  75. 2 31998 2 32000
  76. 3 -31997 -2 1 -32000
  77. 1 10 10
  78. 1 10 11
  79.  
  80. Output:
  81. 5+7/4=3
  82. NO EXPRESSION
  83. 12+2-5-1/2=4
  84. 32000/2=16000
  85. 32000/32000=1
  86. 9*1+2+-10*33-3-3-2-2=23
  87. 9*1*2*-10*33*3+3+2+2+23+9*1+2+-10+33+3+3+2+2+23+9*1+2+-10+33+3+3+2+2+23+9*1+2+-10+33+3+3+2/2+23/9+1*2/-10-33+3+3/2+2+23=109
  88. 31998+2=32000
  89. -31997+-2-1=-32000
  90. 10=10
  91. NO EXPRESSION
  92. **/
Advertisement
Add Comment
Please, Sign In to add comment