有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
Some parts use both Chinese and English, but some parts use only one language. Feel free to share, but please contact me first and list this blog as your reference.

2014年1月20日 星期一

UVa OJ - 476 Points in Figures Rectangles

The following program is my ACcepted code for UVA-476.
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃UAV 476的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )

//This program is for UVA 476 Points in Figures Rectangles



#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;

int numR = 0;// for number of rectangle
int numP = 0;// for number of points
double rec[11][2][2];

int main()
{
    numR = 0;
    numP = 0;
 
    char tmpChar;
    double x,y;
 
    while(scanf("%c", &tmpChar)!=EOF)
    {
        if(tmpChar == '*')
            break;
         
        numR ++;// from 1 to the number of rectangle
     
        scanf("%lf %lf %lf %lf\n", &rec[numR][0][0], &rec[numR][0][1], &rec[numR][1][0], &rec[numR][1][1]);
        //[0][0] and [0][1] is the upper left , [1][0] and [1][1] is the lower right
    }
 
    while(scanf("%lf %lf", &x, &y)!=EOF)
    {
        numP++;// from 1 to the number of points
     
        //To compare the double, we use the absolute value, use float would fail
        if( fabs(x-9999.9)<=0.0000001 && fabs(y-9999.9)<=0.0000001 )
            break;
     
        bool findAns = false;
     
        for(int i=1; i<= numR; i++)
        {
            if(x > rec[i][0][0] && x < rec[i][1][0] && y > rec[i][1][1] && y < rec[i][0][1])
            {
                findAns = true;
                printf("Point %d is contained in figure %d\n", numP, i);
            }
        }
     
        if(findAns == false)
            printf("Point %d is not contained in any figure\n", numP);
    }
 
 
    return 0;
}

沒有留言:

張貼留言

請留下您的任何想法或建議!
Please leave any thought or comment!