ZOJ - 3176 Rounding or Truncation

给你一些数据,让你判断数据小数点后面的方式是什么。

 

大致说一下思路:对于每组数据,和一种给定的舍入方式(舍弃或者进位)。我们可以求出相应满足该方式的文件数量的数据范围。然后如果这组的所有数组范围有交集的话,那么说明他可能是按这种方式舍入的。

 

 

#include <cstdio> #define min(a,b) (a < b ? a : b) #define max(a, b) (a > b ? a : b) #define INF 10000000 int m[1010], p[1010]; int left, right; /*bool gao(int l, int r) { if(l < left) { if(r >= left) { right = min(r, right); return true; } } else if(l <= right){ left = l; right = min(r, right); return true; } return false; }*/ bool gao(int l, int r) { //求两个区间的相交区间 left = max(l, left); right = min(r, right); if(left > right) return false; else return true; } bool is_R(int n) { int tl, tr, i; double tt; if(p[0] != 0) { tt = (double)m[0]*100/(p[0]-0.5); right = (int)tt; } else { right = INF; } if(p[0] != 100) { tt = (double)m[0]*100/(p[0]+0.5); left = (int)tt + 1; } else { left = m[0]; } for(i = 1; i < n; i++) { if(p[i] != 0) { tt = (double)m[i]*100/(p[i]-0.5); tr = (int)tt; } else { tr = INF; } if(p[i] != 100) { tt = (double)m[i]*100/(p[i]+0.5); tl = (int)tt + 1; } else { tl = m[i]; } if(!gao(tl, tr)) { //tl可能是大于tr的(大1) return false; } } return true; } bool is_T(int n) { int tl, tr, i; double tt; if(p[0] != 0) { tt = (double)m[0]*100/(p[0]); right = (int)tt; } else { right = INF; } if(p[0] != 100) { tt = (double)m[0]*100/(p[0]+1); left = (int)tt + 1; } else { left = m[0]; } for(i = 1; i < n; i++) { if(p[i] != 0) { tt = (double)m[i]*100/(p[i]); tr = (int)tt; } else { tr = INF; } if(p[i] != 100) { tt = (double)m[i]*100/(p[i]+1); tl = (int)tt + 1; } else { tl = m[i]; } if(!gao(tl, tr)) { return false; } } return true; } int main(int argc, char* argv[]) { int T, rflag, tflag, i, n; scanf("%d", &T); while(T--) { rflag = 0; tflag = 0; scanf("%d", &n); for(i = 0; i < n; i++) { scanf("%d %d", &m[i], &p[i]); getchar(); } if(is_R(n)) { rflag = 1; } if(is_T(n)) { tflag = 1; } if(rflag == 1 && tflag == 1) { printf("Either/n"); } else if(rflag == 0 && tflag == 0) { printf("Neither/n"); } else if(rflag == 1) { printf("Rounding/n"); } else { printf("Truncation/n"); } } return 0; } 

你可能感兴趣的:(ZOJ - 3176 Rounding or Truncation)