Fruit Ninja(随机数rand())

链接:https://www.nowcoder.com/acm/contest/163/A
来源:牛客网

题目描述

Fruit Ninja is a juicy action game enjoyed by millions of players around the world, with squishy,
splat and satisfying fruit carnage! Become the ultimate bringer of sweet, tasty destruction with every slash.
Fruit Ninja is a very popular game on cell phones where people can enjoy cutting the fruit by touching the screen.
In this problem, the screen is rectangular, and all the fruits can be considered as a point. A touch is a straight line cutting
thought the whole screen, all the fruits in the line will be cut.
A touch is EXCELLENT if ≥ x, (N is total number of fruits in the screen, M is the number of fruits that cut by the touch, x is a real number.)
Now you are given N fruits position in the screen, you want to know if exist a EXCELLENT touch.

输入描述:

The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
The first line of each case contains an integer N (1 ≤ N ≤ 1e4) and a real number x (0 < x < 1), as mentioned above.
The real number will have only 1 digit after the decimal point.
The next N lines, each lines contains two integers xi and yi(-1e9≤ xi,yi≤ 1e9),denotes the coordinates of a fruit.

输出描述:

For each test case, output "Yes" if there are at least one EXCELLENT touch. Otherwise, output "No".
示例1

输入

复制
2
5 0.6
-1 -1
20 1
1 20
5 5
9 9
5 0.5
-1 -1
20 1
1 20
2 5
9 9

输出

复制
Yes
No


第一次做题用到随机数,蒟蒻
用不用srand应该都差不多
题意是给你n个点 是否满足一条线上有m个点 m/n>=x,随机数枚举

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn = 1e4+5;
 5 struct Node
 6 {
 7     int x,y;
 8 } node[maxn];
 9 int main()
10 {
11     int t;
12     scanf("%d",&t);
13     while(t--)
14     {
15         int n;
16         double x;
17         int cnt = 0;
18         int flag = 0;
19         scanf("%d%lf",&n,&x);
20         for(int i=0; i<n; i++)
21         {
22             scanf("%d%d",&node[i].x,&node[i].y);
23         }
24         srand(time(0));
25         for(int i=0; i<1000; i++)
26         {
27             int a = rand()%n;
28             int b = rand()%n;
29             if(a == b)continue;
30             cnt = 0;
31             for(int j=0; j<n; j++)
32             {
33                 if((node[a].x-node[j].x)*(node[b].y-node[j].y) == (node[b].x-node[j].x)*(node[a].y-node[j].y))
34                     cnt++;
35             }
36             if(cnt >= x * n)
37             {
38                 flag = 1;
39                 break;
40             }
41         }
42         if(flag)printf("Yes
");
43         else printf("No
");
44     }
45 }
View Code
原文地址:https://www.cnblogs.com/iwannabe/p/9435602.html