lightoj 1027 A Dangerous Maze desired

Provided the answer is r, cnt is x [i]> = 0 the number of

那么r = 1/n *  (Σx[i](x[i] >= 0) + ∑(r - x[i])(x[i] < 0))

Then transposed to the solution of the equation r together to obtain r = Σ | x [i] | / cnt, except with the gcd. Remember sentenced under special x [i] are negative situation can be.

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 int T,cas,n,tot,gcd,cnt;
 5 int x[110];
 6 int main()
 7 {
 8     for (scanf("%d",&T);T != 0;T--)
 9     {
10         cas++;
11         tot = cnt = 0;
12         scanf("%d",&n);
13         for (int i = 1;i <= n;i++)
14         {
15             scanf("%d",&x[i]);
16             if (x[i] >= 0)
17             {
18                 cnt++;
19                 tot += x[i];
20             }else
21                 tot -= x[i];
22         }
23         if (cnt == 0)
24         {
25             printf("Case %d: inf\n",cas); 
26             continue;
27         }
28         gcd = __gcd(tot,cnt);
29         printf("Case %d: %d/%d\n",cas,tot / gcd,cnt / gcd);
30         
31     }
32     return 0;
33 }

 

Guess you like

Origin www.cnblogs.com/iat14/p/11410053.html