锘??xml version="1.0" encoding="utf-8" standalone="yes"?>日本精品久久久久中文字幕8,国产精品久久波多野结衣,色婷婷综合久久久中文字幕http://m.shnenglu.com/goAheadtw/category/15891.htmlhahazh-cnWed, 02 Feb 2011 02:09:05 GMTWed, 02 Feb 2011 02:09:05 GMT60hdu3440(宸垎綰︽潫)http://m.shnenglu.com/goAheadtw/articles/139140.htmltwtwSat, 22 Jan 2011 17:13:00 GMThttp://m.shnenglu.com/goAheadtw/articles/139140.htmlhttp://m.shnenglu.com/goAheadtw/comments/139140.htmlhttp://m.shnenglu.com/goAheadtw/articles/139140.html#Feedback0http://m.shnenglu.com/goAheadtw/comments/commentRss/139140.htmlhttp://m.shnenglu.com/goAheadtw/services/trackbacks/139140.html棰樼洰緗戝潃錛?http://acm.hdu.edu.cn/showproblem.php?pid=3440 

House Man

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 446    Accepted Submission(s): 157


Problem Description
In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. Today he plans to use N houses to hone his house hopping skills. He will start at the shortest house and make N-1 jumps, with each jump taking him to a taller house than the one he is jumping from. When finished, he will have been on every house exactly once, traversing them in increasing order of height, and ending up on the tallest house.
The man can travel for at most a certain horizontal distance D in a single jump. To make this as much fun as possible, the crazy man want to maximize the distance between the positions of the shortest house and the tallest house.
The crazy super man have an ability鈥攎ove houses. So he is going to move the houses subject to the following constraints:
1. All houses are to be moved along a one-dimensional path.
2. Houses must be moved at integer locations along the path, with no two houses at the same location.
3. Houses must be arranged so their moved ordering from left to right is the same as their ordering in the input. They must NOT be sorted by height, or reordered in any way. They must be kept in their stated order.
4. The super man can only jump so far, so every house must be moved close enough to the next taller house. Specifically, they must be no further than D apart on the ground (the difference in their heights doesn't matter).
Given N houses, in a specified order, each with a distinct integer height, help the super man figure out the maximum possible distance they can put between the shortest house and the tallest house, and be able to use the houses for training.
 

Input
In the first line there is an integer T, indicates the number of test cases.(T<=500)
Each test case begins with a line containing two integers N (1 ≤ N ≤ 1000) and D (1 ≤ D ≤1000000). The next line contains N integer, giving the heights of the N houses, in the order that they should be moved. Within a test case, all heights will be unique.
 

Output
For each test case , output “Case %d: “first where d is the case number counted from one, then output a single integer representing the maximum distance between the shortest and tallest house, subject to the constraints above, or -1 if it is impossible to lay out the houses. Do not print any blank lines between answers.
 

Sample Input
3 4 4 20 30 10 40 5 6 20 34 54 10 15 4 2 10 20 16 13
 

Sample Output
Case 1: 3 Case 2: 3 Case 3: -1

// Bellman_Ford 
#include <stdio.h>
#include 
<memory>
#include 
<iostream>
#include 
<algorithm>
#include 
<cstring>
#include 
<vector>
#include 
<map>
#include 
<cmath>
#include 
<set>
#include 
<queue>
#include 
<time.h> 
#include 
<limits>
using namespace std;
#define typev int
#define N 1005
#define inf 0x7fffffff 
#define E (N*5) 
const double pi = acos(-1.0); 
struct e{
    
int st, ed; 
    typev len;  
    
void set(int _st, int _ed, typev _len){
        st 
= _st; 
        ed 
= _ed; 
        len 
= _len; 
    }
}es[E];
struct node{
    
int h; 
    
int i; 
}nodes[N];
e
* fir[N]; 
int n, en, d;
int st, ed; 
int vis[N], t[N], que[N]; 
typev dist[N]; 
inline 
bool cmp(node n1, node n2){
    
return n1.h < n2.h; 
}
inline 
bool relax(int st, int ed, int len){
    
if(dist[st] < inf && dist[ed] > dist[st] + len){
        dist[ed] 
= dist[st] + len;
        
return true
    }
    
return false
}
bool Bellman_Ford(int n, int st){  //榪斿洖true琛ㄧず鏈夎礋鐜?/span>
    int i, j; 
    
bool flag; 
    
for(i = 0; i < n; i++) dist[i] = inf; 
    dist[st] 
= 0
    
for(i = 0; i < n; i++){
        flag 
= false
        
for(j = 0; j < en; j++){
            
if(relax(es[j].st, es[j].ed, es[j].len)) flag = true
        }
        
if(!flag) break
    }
    
return flag;  
}
int cnt = 0
int main(){
#ifndef ONLINE_JUDGE
    freopen(
"in.txt""r", stdin); 
    
//freopen("out.txt", "w", stdout); 
#endif 

    
int t, i, ans, u, v;
    scanf(
"%d"&t);
    
while(t--){
        scanf(
"%d%d"&n, &d);
        
for(i = 0; i < n; i++){
            scanf(
"%d"&nodes[i].h);
            nodes[i].i 
= i; 
        }
        en 
= st = ed = 0
        sort(nodes, nodes
+n, cmp); 
        st 
= nodes[0].i; 
        ed 
= nodes[n - 1].i; 
        
if(st > ed) swap(st, ed); 
        
for(i = 0; i < n; i++) fir[i] = NULL; 
        
for(i = 1; i < n; i++){
            es[en
++].set(i, i-1-1); 
            u 
= nodes[i].i; 
            v 
= nodes[i - 1].i; 
            
if(u < v){
                es[en
++].set(u, v, d); 
            }
else es[en++].set(v, u, d); 
        } 
        
if(!Bellman_Ford(n, st)) ans = dist[ed];
        
else ans = -1
        printf(
"Case %d: %d\n"++cnt, ans);

    }
    
return 0;
}





tw 2011-01-23 01:13 鍙戣〃璇勮
]]>
hdu3436(鏁版嵁緇撴瀯)http://m.shnenglu.com/goAheadtw/articles/139120.htmltwtwSat, 22 Jan 2011 08:58:00 GMThttp://m.shnenglu.com/goAheadtw/articles/139120.htmlhttp://m.shnenglu.com/goAheadtw/comments/139120.htmlhttp://m.shnenglu.com/goAheadtw/articles/139120.html#Feedback0http://m.shnenglu.com/goAheadtw/comments/commentRss/139120.htmlhttp://m.shnenglu.com/goAheadtw/services/trackbacks/139120.html
#include <stdio.h>
#include 
<memory>
#include 
<iostream>
#include 
<algorithm>
#include 
<cstring>
#include 
<vector>
#include 
<map>
#include 
<cmath>
#include 
<set>
#include 
<queue>
#include 
<time.h> 
#include 
<limits>
using namespace std;
#define N 100005
#define typev int 
typev ar[N
*2]; 
int ks[N], num[N], pos[N], id[N];
bool has[N]; 
int kn, n, qn, fr; 
char qs[N]; 
int vMax; 
int lowb(int t){ return t & (-t); }
void add(int i, typev v){
    
for(; i < vMax; ar[i] += v, i += lowb(i)) ; 
}
typev sum(
int i){
    typev s 
= 0;
    
for(; i > 0; s += ar[i], i -= lowb(i));
    
return s; 
}
int find_k(int k){ //find the kth number
    int pos = 0, cnt = 0, i; 
    
for(i = log((double)vMax) / log(2.0+ 1; i >= 0; i--){
        pos 
+= (1 << i); 
        
if(pos >= vMax || cnt + ar[pos] >= k) pos -= (1 << i); 
        
else cnt += ar[pos]; 
    }
    
return pos + 1
}
bool input(){
    scanf(
"%d%d"&n, &qn);
    
int i; 
    
char ops[10]; 
    
for(i = 0; i < qn; i++){
        scanf(
"%s%d", ops, num+i);
        ks[i] 
= num[i]; 
        qs[i] 
= ops[0]; 
    }
    
return true
}
void init(){
    kn 
= qn; 
    ks[kn
++= 1
    sort(ks, ks
+kn);  
    
int i, j; 
    j 
= 1;
    
for(i = 1; i < kn; i++){
        
if(ks[i] != ks[j-1]) ks[j++= ks[i]; 
    }
    kn 
= j; 
    vMax 
= qn + kn + 1
    fill(ar, ar
+vMax+10); 
    fr 
= qn; 
}
int cnt = 0
void solve(){
    
int i, pMax, val, p, s, ans; 
    init(); 
    ks[kn] 
= n+1
    
for(i = 0; i < kn; i++){
        has[i] 
= true
        val 
= ks[i+1]-ks[i]; 
        pos[i] 
= qn + i + 1
        add(pos[i], val); 
    }
    printf(
"Case %d:\n"++cnt);
    
for(i = 0; i < qn; i++){
        
if(qs[i] == 'T'){
            p 
= lower_bound(ks, ks+kn, num[i]) - ks; 
            has[p] 
= false
            val 
= -1
            add(pos[p], val); 
            val 
= 1
            id[fr] 
= num[i]; 
            pos[p] 
= fr--
            add(pos[p], val); 
        }
else if(qs[i] == 'R'){
            p 
= find_k(num[i]); 
            
if(p <= qn){
                ans 
= id[p]; 
            }
else{
                s 
= sum(p-1); 
                ans 
= num[i] - s + ks[p - qn - 1- 1
                
if(!has[p - qn - 1]) ans++
            }
            printf(
"%d\n", ans);
        }
else//'Q'
            p = lower_bound(ks, ks+kn, num[i]) - ks; 
            ans 
= sum(pos[p] - 1); 
            printf(
"%d\n", ans + 1);
        }
    }
}
int main(){
#ifndef ONLINE_JUDGE
    freopen(
"in.txt""r", stdin); 
    
//freopen("out.txt", "w", stdout); 
#endif 
    
int t; 
    scanf(
"%d"&t);
    
while(t--){
        input(); 
        solve(); 
    }
    
return 0;
}







tw 2011-01-22 16:58 鍙戣〃璇勮
]]>
hdu3433(dp)http://m.shnenglu.com/goAheadtw/articles/139038.htmltwtwFri, 21 Jan 2011 08:44:00 GMThttp://m.shnenglu.com/goAheadtw/articles/139038.htmlhttp://m.shnenglu.com/goAheadtw/comments/139038.htmlhttp://m.shnenglu.com/goAheadtw/articles/139038.html#Feedback0http://m.shnenglu.com/goAheadtw/comments/commentRss/139038.htmlhttp://m.shnenglu.com/goAheadtw/services/trackbacks/139038.htmlhttp://acm.hdu.edu.cn/showproblem.php?pid=3433 
/* 
棰樼洰鎻忚堪: N涓漢,絎琲涓漢瀹屾垚涓涓狝浠誨姟闇瑕佹椂闂碼i,瀹屾垚涓涓狟浠誨姟闇瑕佹椂闂碽i錛?br>鐜板湪鍙圶涓換鍔鍜孻涓換鍔錛屾眰瀹屾垚鎵鏈変換鍔℃墍闇瑕佺殑鏈鐭椂闂淬?br>瑙f硶錛氫簩鍒嗘椂闂磘錛宒p[i][j]琛ㄧず鍓峣涓漢瀹屾垚j涓狝浠誨姟鎵鑳藉瀹屾垚鐨凚浠誨姟鐨勬暟閲?br>
*/ 
#include 
<stdio.h>
#include 
<memory>
#include 
<iostream>
#include 
<algorithm>
#include 
<cstring>
#include 
<vector>
#include 
<map>
#include 
<cmath>
#include 
<set>
#include 
<queue>
#include 
<time.h> 
#include 
<limits>
using namespace std;
#define XY 205
#define N 55
#define inf 0x7fffffff
int a[N], b[N], dp[XY], n, X, Y; 
bool check(int t){  //鍒ゆ柇鍦ㄦ椂闂磘鍐呮槸鍚﹀彲浠ュ畬鎴愭墍鏈夌殑浠誨姟
    int i, j, k, kMax; 
    
for(i = 1; i <= X; i++) dp[i] = -1
    dp[
0= 0
    
for(i = 0; i < n; i++){
        kMax 
= min(X, t / a[i]); 
        
if(dp[X] >= Y) return true
        
for(j = X; j >= 0; j--){  
            
for(k = 0; k <= kMax && j - k>= 0; k++){  //絎琲涓漢瀹屾垚k浠?/span>
                if(dp[j-k] < 0continue
                dp[j] 
= max(dp[j], dp[j - k] + (t - k * a[i]) / b[i]); 
            }
        }
    }
    
return dp[X] >= Y; 
}
int main(){
#ifndef ONLINE_JUDGE
    freopen(
"in.txt""r", stdin); 
    
//freopen("out.txt", "w", stdout); 
#endif 
    
int t, i,ca, low, high, mid; 
    scanf(
"%d"&t);
    
for(ca = 1; ca <= t; ca++){
        scanf(
"%d%d%d"&n, &X, &Y);
        low 
= 0
        high 
= inf; 
        
for(i = 0; i < n; i++){
            scanf(
"%d%d", a+i, b+i);
            high 
= min(high, a[i] * X + b[i] * Y); 
        }
        
while(low <= high){
            mid 
= (low + high) >> 1
            
if(check(mid)) high = mid - 1
            
else low = mid + 1
        }
        printf(
"Case %d: %d\n", ca, high + 1);
    }
    
return 0;
}





tw 2011-01-21 16:44 鍙戣〃璇勮
]]>
hdu3434http://m.shnenglu.com/goAheadtw/articles/139032.htmltwtwFri, 21 Jan 2011 08:01:00 GMThttp://m.shnenglu.com/goAheadtw/articles/139032.htmlhttp://m.shnenglu.com/goAheadtw/comments/139032.htmlhttp://m.shnenglu.com/goAheadtw/articles/139032.html#Feedback0http://m.shnenglu.com/goAheadtw/comments/commentRss/139032.htmlhttp://m.shnenglu.com/goAheadtw/services/trackbacks/139032.html棰樼洰鏉ユ簮錛?http://acm.hdu.edu.cn/showproblem.php?pid=3434 

Sequence Adjustment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 463    Accepted Submission(s): 144


Problem Description
Given a sequence consists of N integers. Each time you can choose a continuous subsequence and add 1 or minus 1 to the numbers in the subsequence .You task is to make all the numbers the same with
the least tries. You should calculate the number of the least tries
you needed and the number of different final sequences with the least tries.
 


 

Input
In the first line there is an integer T, indicates the number of test cases.(T<=30)
In each case, the first line contain one integer N(1<=N<=10^6),
the second line contain N integers and each integer in the sequence is between [1,10^9].
There may be some blank lines between each case.
 


 

Output
For each test case , output “Case d: x y “ where d is the case number
counted from one, x is the number of the least tries you need and y
is the number of different final sequences with the least tries.
 


 

Sample Input
2 2 2 4 6 1 1 1 2 2 2
 


 

Sample Output
Case 1: 2 3 Case 2: 1 2
Hint
In sample 1, we can add 1 twice at index 1 to get {4,4},or minus 1 twice at index 2 to get {2,2}, or we can add 1 once at index 1 and minus 1 once at index 2 to get {3,3}. So there are three different final sequences.
 


 

Author
wzc1989
 


 

Source


/*
鏇磋灝界殑瑙i鎶ュ憡瑙侊細
http://hi.baidu.com/liwang112358/blog/item/3dac7e566f300f55d0090679.html 
*/
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
#include <limits>
using namespace std;
#define N 1000005
#define ll long long
#define ABS(a) (a > 0 ? a : -a)
ll a[N], p[N];
int main(){
#ifndef ONLINE_JUDGE
 freopen("in.txt", "r", stdin);
 //freopen("out.txt", "w", stdout);
#endif
 int t, n, ca;
 ca = a[0] = p[0] = p[1] = 0;
 scanf("%d", &t);
 while(t--){
  scanf("%d", &n);
  int i, j;
  ll sum, ans;
  for(i = 1; i <= n; i++) scanf("%d", a+i);
  for(i = 2, j = 1; i <= n; i++){
   if(a[i] != a[j]){
    a[++j] = a[i];
    p[j] = a[j] - a[j - 1];
   }
  }
  n = j;
  ans = sum = 0;
  for(i = 2; i <= n; i++){
   if(p[i] * sum < 0) ans += min(ABS(sum), ABS(p[i]));
   sum += p[i];
  }
  sum = ABS(sum);
  ans += sum;
  //printf("Case %d: %lld %lld\n", ++ca, ans, sum + 1);
  printf("Case %d: %I64d %I64d\n", ++ca, ans, sum + 1);
 }
 return 0;
}

 

 



tw 2011-01-21 16:01 鍙戣〃璇勮
]]>
hdu3596(閫嗘嘗鍏拌〃杈懼紡姹傚?http://m.shnenglu.com/goAheadtw/articles/138607.htmltwtwSun, 16 Jan 2011 11:58:00 GMThttp://m.shnenglu.com/goAheadtw/articles/138607.htmlhttp://m.shnenglu.com/goAheadtw/comments/138607.htmlhttp://m.shnenglu.com/goAheadtw/articles/138607.html#Feedback0http://m.shnenglu.com/goAheadtw/comments/commentRss/138607.htmlhttp://m.shnenglu.com/goAheadtw/services/trackbacks/138607.html/*************************************/
/*鏀寔鏁板瓧(鑰岄潪琛ㄨ揪寮?鍓嶉潰鏈夋璐熷彿   */
/*鏀寔嫻偣鏁扮殑+錛?錛?錛?,^ 榪愮畻      */
/*************************************/
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
#include <limits>
#include <stack>
using namespace std;
#define N 10005
#define eps 1e-9
#define vType double
struct node{
 vType val;
 char op;
 node(vType _val=0, char _op = ' '):val(_val), op(_op){}
}nodes[N];
char str[N];
inline bool isDigit(char ch){
 return ch >= '0' && ch <= '9';
}
map<char, int> ms;
void init(){
 ms['('] = 0;
 ms['-'] = ms['+'] = 1;
 ms['*'] = ms['/'] = 2;
 ms['^'] = 3;
}
vType calPoland(node* nodes, int k){
 int i, j;
 vType a, b;
 stack<vType> s;
 for(i = 0; i < k; i++){
  if(nodes[i].op == ' '){
   s.push(nodes[i].val);
  }else{
   a = s.top();
   s.pop();
   b = s.top();
   s.pop();
   switch(nodes[i].op){
    case '+':
     s.push(b + a);
     break;
    case '-':
     s.push(b - a);
     break;
    case '*':
     s.push(b * a);
     break;
    case '/':
     if(fabs(a) < eps) throw true;
     s.push(b / a);
     break;
    case '^':
     s.push(pow(b, a));
     break;
   }
  }
 }
 return s.top();
}
vType poland(char* str){
 int i, k, sign;
 bool inNum, hasDot;  //inNum鏍囪褰撳墠鏄惁鍙互杈撳叆鏁板瓧, hasDot鏍囪鏄惁宸茬粡杈撳叆灝忔暟鐐?br> stack<char> oper;
 for(i = k = 0, sign = 1, inNum = true; str[i]; i++){
  if(isDigit(str[i]) || str[i] == '.'){
   if(inNum){
    vType val;
    double w = 1;
    if(str[i] == '.'){
     hasDot = true;
     val = 0;
    }
    else{
     val = str[i] - '0';
     hasDot = false;
    }
    i++;
    while(isDigit(str[i]) || str[i] == '.'){
     if(str[i] == '.'){
      if(hasDot) throw true;
      hasDot = true;
     }else{
      if(hasDot){
       w *= 0.1;
       val += (str[i] - '0') * w;
      }
      else val = val * 10 + str[i] - '0';
     }
     i++;
    }
    i--;
    nodes[k++] = node(val * sign, ' ');
    sign = 1;
    inNum = false;
   }else throw true;
  }else{
   switch(str[i]){
    case '(':
     oper.push(str[i]);
     break;
    case ')':
     while(!oper.empty() && oper.top() != '('){
      nodes[k++] = node(0, oper.top());
      oper.pop();
     }
     if(oper.empty()) throw true;  //娌℃湁涓?)'鍖歸厤鐨?('
     oper.pop();
     break;
    case '+':
    case '-':
    case '*':
    case '/':
    case '^':
     if(inNum){
      if(str[i] != '+' && str[i] != '-') throw true;
      while(str[i] == '+' || str[i] == '-'){
       if(str[i] == '-') sign *= -1;
       i++;
      }
      i--;
     }else{
      //while(!oper.empty() && ((str[i] != '^' && ms[str[i]] <= ms[oper.top()]) ||
      // ((str[i] == '^' && ms[str[i]] < ms[oper.top()])))){ //濡傛灉^鏄彸緇撳悎鐨勮瘽灝辯敤榪欎釜
      while(!oper.empty() && ms[str[i]] <= ms[oper.top()]){
        nodes[k++] = node(0, oper.top());
        oper.pop();
      }
      oper.push(str[i]);
      inNum = true;
     }
     break;
   }
  }
 }
 while(!oper.empty()){
  nodes[k++] = node(0, oper.top());
  oper.pop();
 }
 return calPoland(nodes, k);
}
void Cal(char* str){
 try{
  vType ans = poland(str);
  printf("%.8lf\n", ans);
 }
 catch(bool){
  printf("The teacher is so lazy!\n");
 }
}
int main(){
#ifndef ONLINE_JUDGE
 //freopen("in.txt", "r", stdin);
 //freopen("out.txt", "w", stdout);
#endif
 init();
 while(~scanf("%s", str)) Cal(str);
 return 0;
}


tw 2011-01-16 19:58 鍙戣〃璇勮
]]>
hdu 3624 錛堢畻鏈〃杈懼紡姹傚鹼級http://m.shnenglu.com/goAheadtw/articles/138553.htmltwtwFri, 14 Jan 2011 17:43:00 GMThttp://m.shnenglu.com/goAheadtw/articles/138553.htmlhttp://m.shnenglu.com/goAheadtw/comments/138553.htmlhttp://m.shnenglu.com/goAheadtw/articles/138553.html#Feedback0http://m.shnenglu.com/goAheadtw/comments/commentRss/138553.htmlhttp://m.shnenglu.com/goAheadtw/services/trackbacks/138553.html/*
1銆佸乏閫掑綊鍙互鐢ㄥ驚鐜潵瀹炵幇錛屽彸閫掑綊鍙互鐢ㄩ掑綊瀹炵幇(涔熷彲浠ョ敤寰幆瀹炵幇,涓嶈繃閫掑綊瀹炵幇璧鋒潵姣旇緝綆鍗?
2銆佸乏閫掑綊瀹炵幇宸︾粨鍚堣繍綆楋紝鍙抽掑綊瀹炵幇鍙崇粨鍚堣繍綆?br>hdu 3624(緗戝潃錛?http://acm.hdu.edu.cn/showproblem.php?pid=3624 ) 鐨勬枃娉?br>exp         -> addTerm | exp addOp addTerm
addOp    -> + | -
addTerm -> mulTerm | addTerm mulOp mulTerm
mulOp     -> * | / | %
mulTerm -> eTerm ^ mulTerm | eTerm
eTerm    ->sNum | (exp)
sNum     -> num | -eTerm
*/
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
#include <limits>
using namespace std;
#define vType long long
#define MAXLEN 2005
const int vMax = 0x7fffffff;
const int vMin = ~vMax;
#define checkVal(val) (val >= vMin && val <= vMax)
char str[MAXLEN];
int pos, cnt; //pos涓哄綋鍓嶆壂鎻忎綅緗紝cnt涓哄凡緇忓垎閰嶇殑鑺傜偣鐨勬暟鐩?br>char ch;  //ch涓哄綋鍓嶆壂鎻忕殑瀛楃
struct treeNode{
    char op, flag; 
    vType val;
    treeNode* ch[2];
    void setFlag(){
        if((ch[0] && !ch[0]->flag) || (ch[1] && !ch[1]->flag)){
            flag = false;
        }
    }
}mem[MAXLEN*10];
treeNode* exp();
inline bool isDigit(char ch){
    return ch >= '0' && ch <= '9';
}
inline treeNode* newNode(){
    mem[cnt].flag = true;
    mem[cnt].op = ' ';  //op涓虹┖鏍艱〃紺轟笉闇瑕佽繍綆?br>    mem[cnt].ch[0] = mem[cnt].ch[1] = NULL;
    return &mem[cnt++];
}
treeNode* getSyntaxTree(){
    treeNode* tree;
    pos = cnt = 0;
    int i, j;
    i = j = 0;
    ch = '\n';
    do{  //蹇界暐絀烘牸
        if(str[i] != ' ') str[j++] = str[i];
    }while(str[i++]);
 for(i = j = 0; str[i]; i++){ //鍔犺繖涓姝ユ槸涓轟簡澶勭悊2000涓?鐨勫彉鎬佹暟鎹?br>  if(str[i] == '(') j++;
  else if(str[i] == ')') j--;
 }
 if(j != 0){
  tree = newNode();
  tree->flag = false;
 }else tree = exp();
    return tree;
}
treeNode* eTerm(){
    treeNode* t;
    t = newNode();
    ch = str[pos++];
    if(ch == '('){
        t = exp();
        if(ch != ')') t->flag = false;
        else ch = str[pos++];
    }else if(isDigit(ch)){
        vType a = ch - '0';
        for(ch = str[pos++];isDigit(ch); ch = str[pos++]){
            if(t->flag){
                a = a * 10 + ch - '0';
            }
            if(!checkVal(a)){
                t->flag = false;
            }
        }
  t->val = a;
 }else if(ch == '-'){
  t->op = ch;
  t->ch[0] = newNode();
  t->ch[0]->val = 0;
  t->ch[1] = eTerm();
  t->setFlag();
 }else t->flag = false;
    return t;
}
treeNode* mulTerm(){
    treeNode *t, *p, *r, *t0, *t1;;
    r = t = eTerm();
 p = NULL;
    while(ch == '^'){ //寰幆瀹炵幇鍙抽掑綊鏂囨硶
  t1 = newNode();
  t1->op = ch;
  t1->ch[0] = t;
  t0 = eTerm();
  t1->ch[1] = t0;
  if(p){
   p->ch[1] = t1;
  }else r = t1;
  p = t1;
  t = t0;
    }
    return r;
}
treeNode* addTerm(){
    treeNode *t, *p;
    t = newNode();
    t->ch[0] = mulTerm();
    t->setFlag();
    while(ch == '*' || ch == '/' || ch == '%'){
        t->op = ch;
        t->ch[1] = mulTerm();
        t->setFlag();
        p = t;
        t = newNode();
        t->ch[0] = p;
    }
    return t;
}
treeNode* exp(){
    treeNode *t, *p;
    t = newNode();
    t->ch[0] = addTerm();
    while(ch == '-' || ch == '+'){
        t->op = ch;
        t->ch[1] = addTerm();
        p = t;
        t = newNode();
        t->ch[0] = p;
    }
    return t;
}
void cal(treeNode* root){
    if(!root) return;
    cal(root->ch[0]);
    cal(root->ch[1]);
    root->setFlag();
    if(!root->flag) return;
    if(root->op != ' '){
        vType a = 0;
        switch(root->op){
            case '-':
                a = root->ch[0]->val - root->ch[1]->val;
                break;
            case '+':
                a = root->ch[0]->val + root->ch[1]->val;
                break;
            case '*':
                a = root->ch[0]->val * root->ch[1]->val;
                break;
            case '/':
                if(root->ch[1]->val == 0){
                    root->flag = false;
                    return;
                }
                a = root->ch[0]->val / root->ch[1]->val;
                break;
            case '%':
                if(root->ch[1]->val == 0){
                    root->flag = 0;
                    return;
                }
                a = root->ch[0]->val % root->ch[1]->val;
                break;
            case '^':
    if(root->ch[1]->val < 0 || (root->ch[0]->val == 0 && root->ch[1]->val == 0)){
                    root->flag = false;
                    return;
                }
                double b = pow((double)root->ch[0]->val, (double)root->ch[1]->val);
                if(checkVal(b)) a = (vType)b;
                else{
                    root->flag = false;
                    return;
                }
                break;
        }
  if(checkVal(a)){
   root->val = a;
   root->setFlag();
  }else{
   root->flag = false;
  }
    }else if(root->ch[0]){
        root->flag = root->ch[0]->flag;
        root->val = root->ch[0]->val;
    }
}
int main(){
#ifndef ONLINE_JUDGE
 //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    int i, c;
    treeNode* root;
    scanf("%d", &c);
    getchar();
    for(i = 1; i <= c; i++){
        gets(str);
        root = getSyntaxTree();
        printf("Case %d: ", i);
        cal(root);
  if(root->flag){
   printf("%I64d\n", root->val);
  }
        else printf("ERROR!\n");
    }
    return 0;
}

 



tw 2011-01-15 01:43 鍙戣〃璇勮
]]>
成人午夜精品久久久久久久小说| 久久久久女人精品毛片| 久久国产色AV免费观看| 欧美一级久久久久久久大| 91精品观看91久久久久久| 亚洲精品无码久久久久| 一日本道伊人久久综合影| 欧美精品丝袜久久久中文字幕| 国产精品一区二区久久精品无码| 久久99国产精品久久99果冻传媒| 精品久久久久久亚洲精品| 亚洲级αV无码毛片久久精品| 久久这里只有精品视频99| 久久婷婷五月综合成人D啪 | 亚洲午夜久久影院| 精品久久久久中文字幕日本| 精品久久久久久久| 精品亚洲综合久久中文字幕| 丁香五月综合久久激情| 国内精品久久久久久久亚洲| 欧美色综合久久久久久| 亚洲午夜无码久久久久小说| 久久精品国产免费观看| 久久精品毛片免费观看| 激情五月综合综合久久69| 香蕉99久久国产综合精品宅男自 | 久久精品国产免费一区| 国产精品久久久久久一区二区三区 | 久久国产亚洲精品无码| 亚洲午夜久久久精品影院| 国内精品久久久久久不卡影院 | 久久国产精品成人片免费| 久久这里只有精品首页| 久久青青草原精品国产软件| 欧美日韩精品久久免费| 久久久久免费精品国产| 国产69精品久久久久观看软件| 91久久精一区二区三区大全| 国产亚洲美女精品久久久| 久久综合给合久久狠狠狠97色69 | 久久精品国产亚洲av麻豆图片|