ACM练习题(day001)

来源:互联网 发布:ae软件图标 编辑:程序博客网 时间:2024/06/08 12:25

多边形重心问题

时间限制:3000 ms  |  内存限制:65535 KB
难度:5
描述
在某个多边形上,取n个点,这n个点顺序给出,按照给出顺序将相邻的点用直线连接, (第一个和最后一个连接),所有线段不和其他线段相交,但是可以重合,可得到一个多边形或一条线段或一个多边形和一个线段的连接后的图形; 
如果是一条线段,我们定义面积为0,重心坐标为(0,0).现在求给出的点集组成的图形的面积和重心横纵坐标的和;
输入
第一行有一个整数0<n<11,表示有n组数据;
每组数据第一行有一个整数m<10000,表示有这个多边形有m个顶点;
输出
输出每个多边形的面积、重心横纵坐标的和,小数点后保留三位;
样例输入
330 10 20 331 10 00 141 10 00 0.50 1
样例输出
0.000 0.0000.500 1.000

0.500 1.000

#include <iostream>#include <iomanip>using namespace std;   //叉乘求各个向量构成的面积                      //重心是面积的加权平均数struct Point{double px, py;};int main3(){int t;Point ps[10010], tmp;int i;cin >> t;while (t--){double area=0, pice = 0;tmp.px = 0;tmp.py = 0;int n;cin >> n;for (i = 0; i < n; i++)cin >> ps[i].px >> ps[i].py;ps[i].px = ps[0].px;ps[i].py = ps[0].py;for (i = 0; i <n; i++){pice = ps[i].px*ps[i + 1].py - ps[i + 1].px*ps[i].py;area += pice;tmp.px += pice*(ps[i].px + ps[i + 1].px);tmp.py += pice*(ps[i].py + ps[i + 1].py);}if (area == 0.0)cout << "0.000 0.000\n";elsecout << fixed << setprecision(3) << (area > 0 ? (area / 2.0) : (area / -2.0)) << " " << (tmp.px + tmp.py) / area / 3.0<<endl;}return 0;}

ASCII码排序

时间限制:3000 ms  |  内存限制:65535 KB
难度:2
描述
输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。
输入
第一行输入一个数N,表示有N组测试数据。后面的N行输入多组数据,每组输入数据都是占一行,有三个字符组成,之间无空格。
输出
对于每组输入数据,输出一行,字符中间用一个空格分开。
样例输入
2qweasd
样例输出
e q wa d s
#include <iomanip>#include <iostream>using namespace std;int main(){int t;cin >> t;while (t--){char tmp;char str[4];cin >> str;for (int i = 0; i < 2; i++){for (int j = 1; j < 3; j++){if (str[i]>str[j]){tmp = str[i];str[i] = str[j];str[j] = tmp;}}}cout << str[0] << ' ' << str[1] << ' ' << str[2] << endl;}return 0;}



Binary String Matching

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘1001110110’ while the pattern string A is ‘11’, you should output 3, because the pattern A appeared at the posit
输入
The first line consist only one integer N, indicates N cases follows. In each case, there are two lines, the first line gives the string A, length (A) <= 10, and the second line gives the string B, length (B) <= 1000. And it is guaranteed that B is always longer than A.
输出
For each case, output a single line consist a single integer, tells how many times do B appears as a substring of A.
样例输入
31110011101101011100100100100011010110100010101011 
样例输出
303 
#include <cstring>#include <iostream>using namespace std;int main(){int t;cin >> t;while (t--){int count = 0;char subStr[11];char str[1010];cin >> subStr >> str;for (int i = 0; i < strlen(str)-strlen(subStr)+1; i++){int j = 0;for (; j < strlen(subStr); j++){if (subStr[j] != str[i + j])break;}if (j >= strlen(subStr))count++;}cout << count << endl;}return 0;}



喷水装置(一)

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它为中心的半径为实数Ri(0<Ri<15)的圆被湿润,这有充足的喷水装置i(1<i<600)个,并且一定能把草坪全部湿润,你要做的是:选择尽量少的喷水装置,把整个草坪的全部湿润。
输入
第一行m表示有m组测试数据
每一组测试数据的第一行有一个整数数n,n表示共有n个喷水装置,随后的一行,有n个实数ri,ri表示该喷水装置能覆盖的圆的半径。
输出
输出所用装置的个数
样例输入
252 3.2 4 4.5 6 101 2 3 1 2 1.2 3 1.1 1 2
样例输出
25
#include <functional>//greater<double>()  STL定义运算函数#include <cmath>#include <algorithm>#include <iostream>using namespace std;int main(){double dbR[606];int t;cin >> t;while (t--){double length = 20, width = 2;int n;cin >> n;for (int i = 0; i < n; i++)cin >> dbR[i];sort(dbR, dbR + n, greater<double>());int j = 0;while (length>0)//贪心法 每一次都用相对最大的喷水装置{length -= 2 * sqrt(dbR[j] * dbR[j] - 1);j++;}cout << j << endl;}return 0;}


街区最短路径问题

时间限制:3000 ms  |  内存限制:65535 KB
难度:4
描述
一个街区有很多住户,街区的街道只能为东西、南北两种方向。

住户只可以沿着街道行走。

各个街道之间的间隔相等。

用(x,y)来表示住户坐在的街区。

例如(4,20),表示用户在东西方向第4个街道,南北方向第20个街道。

现在要建一个邮局,使得各个住户到邮局的距离之和最少。

求现在这个邮局应该建在那个地方使得所有住户距离之和最小;

输入
第一行一个整数n<20,表示有n组测试数据,下面是n组数据;
每组第一行一个整数m<20,表示本组有m个住户,下面的m行每行有两个整数0<x,y<100,表示某个用户所在街区的坐标。
m行后是新一组的数据;
输出
每组数据输出到邮局最小的距离和,回车结束;
样例输入
231 12 11 252 9 5 2011 91 11 20
样例输出
244



#include <memory.h>#include <algorithm>#include <iostream>using namespace std;int main(){int nX[101], nY[101];int t;cin >> t;while (t--){memset(nY, 0, sizeof(nY));memset(nX, 0, sizeof(nX));int sum = 0;int m;cin >> m;for (int i = 0; i < m; i++)cin >> nX[i] >> nY[i];sort(nX, nX + m);sort(nY, nY + m);for (int i = 0; i < m / 2; i++)sum += nX[m - i - 1] - nX[i] + nY[m - i - 1] - nY[i];cout << sum << endl;}return 0;}


原创粉丝点击