ACM 第十六天
计算几何
练习题:
Even though Bessie travels directly in a straight line
between pairs of farms, the distance between some farms can be quite
large, so she wants to bring a suitcase full of hay with her so she has
enough food to eat on each leg of her journey. Since Bessie refills her
suitcase at every farm she visits, she wants to determine the maximum
possible distance she might need to travel so she knows the size of
suitcase she must bring.Help Bessie by computing the maximum distance
among all pairs of farms.
Input
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
the pair of farms that are farthest apart from each other.
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2
Hint
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h> using namespace std; struct Point
{
int x,y;
Point(int _x=,int _y=)
{
x=_x;
y=_y;
}
Point operator -(const Point &b) const
{
return Point(x-b.x,y-b.y);
}
int operator ^(const Point &b) const
{
return x*b.y-y*b.x;
}
int operator *(const Point &b) const
{
return x*b.x+y*b.y;
}
void input()
{
scanf("%d%d",&x,&y);
}
}; int dist2(Point a,Point b)
{
return (a-b)*(a-b);
}
const int maxn=;
Point list[maxn];
int Stack[maxn],top;
bool _cmp(Point p1,Point p2)
{
int tmp=(p1-list[])^(p2-list[]);
if(tmp>) return true;
else if(tmp== && dist2(p1,list[])<= dist2(p2,list[]))
return true;
else return false;
}
void Graham(int n)
{
Point p0;
int k=;
p0=list[];
for(int i=;i<n;i++)
if(p0.y>list[i].y || (p0.y==list[i].y && p0.x>list[i].x))
{
p0=list[i];
k=i;
}
swap(list[],list[k]);
sort(list+,list+n,_cmp);
if(n==)
{
top=;
Stack[]=;
return ;
}
if(n==)
{
top=;
Stack[]=;
Stack[]=;
return ;
}
Stack[]=;
Stack[]=;
top=;
for(int i=;i<n;i++)
{
while(top> && ((list[Stack[top-]]-list[Stack[top-]])^(list[i]-list[Stack[top-]]))<=)
top--;
Stack[top++]=i;
}
}
//旋转卡壳,求两点间距离平方的最大值
int rotating_calipers(Point p[],int n)
{
int ans=;
Point v;
int cur=;
for(int i=;i<n;i++)
{
v=p[i]-p[(i+)%n];
while((v^(p[(cur+)%n]-p[cur]))<)
cur=(cur+)%n;
ans=max(ans,max(dist2(p[i],p[cur]),dist2(p[(i+)%n],p[(cur+)%n])));
}
return ans;
}
Point p[maxn];
int main()
{
int n;
while(scanf("%d",&n)==)
{
for(int i=;i<n;i++)
list[i].input();
Graham(n);
for(int i=;i<top;i++)
p[i]=list[Stack[i]];
printf("%d\n",rotating_calipers(p,top));
}
return ;
}
Arcade mall is a new modern mall. It has a new hammer game called "Arcade Game". In this game you're presented with a number n which is hanged on a wall on top of a long vertical tube, at the bottom of the tube there is a button that you should hit with your hammer.
When you hit the button with all your force (as you always do), a ball is pushed all over the tube and hit the number n. The number n flies in the air and it's digits fall back in any random permutation with uniform probability.
If the new number formed is less than or equal to the previous number, the game ends and you lose what ever the new number is. Otherwise (if the number is greater than the previous number), you are still in the game and you should hit the button again.
You win if the new number formed is greater than the previous number and it is equal to the greatest possible permutation number.
Can you compute the probability of winning?
The first line of the input contains the number of test cases T. Following that there are T lines represents T test cases. In each line, there is a single integer (1 ≤ n ≤ 109) the target number. The digits of n are all unique, which means that any 2 digits of n are different.
Output
For each test case, print one line containing the answer. Print the answer rounded to exactly 9 decimal digits.
Examples
3
952
925
592
0.000000000
0.166666667
0.194444444
Note
In the first test case, the answer is 0 because 952 is greater than all 2,5 and 9 permutations so you can't win, whatever you do.
In the second test case, the answer is 0.166666667 because you may win by getting number 952 with probability 1/6.
In the third test case the answer is 0.194444444 because you may win by getting number 952 in round1 with probability 1/6 or you can win by getting number 925 in round 1 and then 952 in round 2 with probability 1/6 * 1/6.
原博客:https://blog.csdn.net/why850901938/article/details/51204620
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n[]={,};
int a[];
double p,q;
int main()
{
for(int i=;i<;i++)
{
n[i]=n[i-]*i;
}
int T;
cin>>T;
while(T--)
{
char s[];
int cnt=;
scanf("%s",s);
int l=strlen(s);
for(int i=;i<l;i++)
{
a[i]=s[i]-'';
}
while(next_permutation(a,a+l))
{
cnt++;
}
if(cnt==)
{
printf("0.000000000\n");
}
else
{
double temp=1.0/n[l];
q=p=temp;
for(int i=;i<cnt;i++)
{
q=p+p*temp;
p=q;
}
printf("%.9lf\n",p);
}
}
return ;
}
Geometry is a very important field in mathematics, Squares and rectangles are essential shapes in geometry, both of them have 4 right angles, but a square is a special case of a rectangle where width and height are the same.
The figure below shows a square on the left and a rectangle on the right:
If you have the width and the height of a 4 right angled shape, can you figure out if it is a square or a rectangle?
The first line contains T, the number of test cases, for each test case there is a line with two integers (1 ≤ w, h ≤ 1, 000, 000) representing width and height, respectively.
Output
For each test case, print one line consists of 'Square'
if the shape is a square, otherwise print 'Rectangle' if it is a
rectangle.
Examples
3
10 10
13 200
300 300
Square
Rectangle
Square
#include<stdio.h>
int main()
{
int n,a,b;
scanf("%d",&n);
while(n--)
{
scanf("%d %d",&a,&b);
if(a==b)
printf("Square\n");
else
printf("Rectangle\n");
}
return ;
}
Salem is known to be one of the best competitive programmers in the region. However, he always finds a hard time understanding the concept of the hamming distance. The hamming distance of two numbers is defined as the number of different digits in their binary representations (leading zeros are used if necessary to make the binary representations have the same length). For example the hamming distance between 12 and 5 is 2 since their binary representations are 1100 and 0101 respectively and they differ in the first and fourth positions.
Recently, Salem got a problem that he needs your help to solve. He is given N integers and asked to get the maximum among the hamming distances of all possible pairs of the given integers.
The first line of the input will be a single integer T representing the number of test cases. Followed by T test cases. Each test case will start with a line with single integer (2 ≤ N ≤ 100) representing the number of the integers. Each of the following N lines contains one of the integers (1 ≤ Ai ≤ 10, 000) from the list of the integers.
Output
For each test case print one line consists of one integer
representing the maximum hamming distance between all possible pairs
from the given integers.
Examples
2
2
12
5
3
1
2
3
2
2
#include<iostream>
#include<cstdio>
using namespace std;
int check(int n)
{
int c=;
while(n)
{
n=n&(n-);
c++;
}
return c;
}
int main()
{
int n;
int a[];
cin>>n;
while(n--)
{
int n1;
cin>>n1;
int maxx=;
for(int i=;i<=n1;i++)
{
cin>>a[i];
}
for(int i=;i<=n1;i++)
{
for(int j=i+;j<=n1;j++)
{
int m=check(a[i]^a[j]);
// cout<<"m: "<<m<<endl;
if(m>maxx)
maxx=m;
}
}
cout<<maxx<<endl;
}
}
ACM 第十六天的更多相关文章
- javaSE第二十六天
第二十六天 414 1:网络编程(理解) 414 (1)网络编程:用Java语言实现计算机间数据的信息传递和资源共享 414 (2)网络编程模型 414 (3)网络编程的三要素 ...
- javaSE第十六天
第十六天 140 1:List的子类(掌握) 140 (1)List的子类特点 140 (2)ArrayList 141 A:没有特有功能需要学习 141 B:案例 ...
- 第三百五十六天 how can I 坚持
一年了,三百五十六天.写个算法算下对不对. 今天突然想买辆自行车了.云马智行车,还是捷安特,好想买一辆. 网好卡.貌似少记了一天呢,357了.好快. 睡觉了,还没锻炼呢,太晚了. 1458748800 ...
- IT第二十六天 - Swing、上周总结
IT第二十六天 上午 Swing 1.对于方法的参数如果是int数值类型,应该直接调用该类中的常量属性,而不应该直接填入数字 2.Toolkit类中定义的方法是可以直接访问本地计算机(操作系统)信息的 ...
- Python第二十六天 python装饰器
Python第二十六天 python装饰器 装饰器Python 2.4 开始提供了装饰器( decorator ),装饰器作为修改函数的一种便捷方式,为工程师编写程序提供了便利性和灵活性装饰器本质上就 ...
- OCM_第十六天课程:Section7 —》GI 及 ASM 安装配置 _安装 GRID 软件/创建和管理 ASM 磁盘组/创建和管理 ASM 实例
注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...
- 孤荷凌寒自学python第八十六天对selenium模块进行较详细的了解
孤荷凌寒自学python第八十六天对selenium模块进行较详细的了解 (今天由于文中所阐述的原因没有进行屏幕录屏,见谅) 为了能够使用selenium模块进行真正的操作,今天主要大范围搜索资料进行 ...
- 孤荷凌寒自学python第七十六天开始写Python的第一个爬虫6
孤荷凌寒自学python第七十六天开始写Python的第一个爬虫6 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 不过由于对python-docx模 ...
- 孤荷凌寒自学python第六十六天学习mongoDB的基本操作并进行简单封装5
孤荷凌寒自学python第六十六天学习mongoDB的基本操作并进行简单封装5并学习权限设置 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第十二天. 今天继续学习mongo ...
随机推荐
- python反射怎么用
反射: 通过字符串的形式对 对象 进行增删改查 setattr 设置某个属性的值 class A(object): def __init__(self): self.name = "sath ...
- PHP实现openSug.js参数调试
这是一款利PHP对百度搜索下拉框提示免费代码实现参数配置调试的程序源代码. 由想要对网站进行搜索下拉调试的站长朋友们进行方便.快速的效果演示,具体参考下面的PHP代码. 如何使用? 请新建一份PHP文 ...
- pip快速git项目安装
pip install git+https://github.com/xx/xx.git
- Leecode刷题之旅-C语言/python-349两个数组的交集
/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...
- 【转载++】C/C++错误分析errno,perror,strerror和GetLastError()函数返回的错误代码的意义
本文是上一篇“fopen返回0(空指针NULL)且GetLastError是0”的侧面回应.听赶来多么地正确和不容置疑,返回NULL时调用GetLastError来看看报错啊,但当时却返回了0,大家都 ...
- c指针学习
#include <stdio.h> void main() { int a1=10; int a2=11; int * pa1, * pa2; pa1 = &a1; pa2 = ...
- 两步搞定一台电脑同时开启多个tomcat
1. 修改tomcat中的某些参数,为了避免启动tomcat时出现冲突,编辑bin/startup.bat, 在文件第一行添加如下两行(必须第一行才有效) SET JAVA_HOME=C:\webso ...
- Fliptil_KEY
Fliptil(fliptile.pas/c/cpp) [问题描述] 约翰知道,那些高智力又快乐的奶牛产奶量特别高.所以他做了一个翻瓦片的益智游戏来娱乐奶牛. 在一个M×N的骨架上,每一个格子里都有一 ...
- 天津Uber优步司机奖励政策(12月28日到12月29日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 设置cell高度的两种方法(label高度的可变引起cell高度可变的情况)
第一种:(iOS8以后可用) 在Xib或stroyboard中(代码也可以) 利用AutoLayout设置好label的约束(比如可以设置四个边都距离屏幕50等方式,必须四个边都要固定好). 在代码部 ...