ZOJ3176 Rounding or Truncation

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3176

问题: If a list of (number, percent) pairs has been recorded, can we decide whether truncation or rounding KAV may use to calculate the percentage?

        我们能通过进位或者舍弃小数点后的数来计算百分比吗?

思路:参看了这篇博客的思路https://blog.csdn.net/zdsfwy/article/details/6313315:“对于每组数据,和一种给定的舍入方式(舍弃或者进位)。我们可以求出相应满足该方式的文件数量的数据范围。然后如果这组的所有数组范围有交集的话,那么说明他可能是按这种方式舍入的。”

按照这个思路做题就是分两步:1求数据范围  2判断是否有交集

具体来说:有进位和舍弃两种方式。进位:根据给出的n个数据及对应百分比判断最小的百分比和最大的百分比是否有交集;舍弃:同理判断数据交集即可。

#include <iostream>
#include <algorithm>
#define inf 0x7777777
using namespace std;
int main()
{
    int t,n,x,y,rr,rl,tr,tl,l,r,ans;
    double tmp;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        rr=tr=inf;
        rl=tl=0;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%d%d%%",&x,&y);
            //Rounding
            if(y==0)
                l=inf;//即使进位还是不足1,文件大小可看作无穷大
            else
            {
                tmp=(100.0*x)/(y*1.0-0.5);//y/100才是y所代表的百分数,算出在当前百分比是通过进位得到的情况下总文件数
                l=int(tmp);
            }
            if(y==100)
                r=x;//完成扫描,x就是总文件数
            else
            {
                tmp=(100.0*x)/(y*1.0+0.5);//百分比+0.5肯定要进位的情况下求出总文件数
                r=(int)tmp+1;
            }
            rl=max(rl,r);//数据范围的最大值
            rr=min(rr,l);//数据范围的最小值
            //Truncation,同上不再赘述
            if(y==0)
                l=inf;
            else
            {
                tmp=(100.0*x)/(y*1.0);
                l=int(tmp);
            }
            if(y==100)
                r=x;
            else
            {
                tmp=(100.0*x)/(y*1.0+1);
                r=(int)tmp+1;
            }
            tl=max(tl,r);
            tr=min(tr,l);
        }
        if(rl<=rr)
            ans+=1;
        if(tr>=tl)
            ans+=10;
        if(ans==11)
            printf("Either\n");
        else if(ans==10)
            printf("Truncation\n");
        else if(ans==1)
            printf("Rounding\n");
        else
            printf("Neither\n");
    }
    return 0;
}

 

posted @ 2018-08-23 22:21  子诚-  阅读(261)  评论(0编辑  收藏  举报