欧几里德的微笑
小明从小的梦想就是成为一名像欧几里德一样的数学家,虽然他以前老是让我们帮他解决问题,现在他正在进行一场考试,这是考试的最后一个问题。
现在给你三个点a,b,c,能不能找出一个旋转点和旋转角度,使得如果这三个点绕着旋转点旋转一定的角度后,点a到达点b原来的位置,点b到达点c原来的位置。
现在小明想让你告诉他有没有合适的旋转点和旋转角度,如果有则输出Yes,否则输出No
由于三个点不会出现重叠的情况,所以就判断两个要点:
- 三个点以A->B->C的形式形成一个
等腰三角形
,也就是说|AB|==|BC| 三点不共线
,也就是说不满足向量AB!=向量BC即可
tips:
不能使用除运算、sqrt、pow等会丢失精度的运算,所以实现以上两点时要注意
#include <iostream>
using namespace std;
long long distance(long long ax, long long ay, long long bx, long long by);
int main()
{
int n;
cin >> n;
for(int i = 1;i <= n;i++)
{
long long x[3], y[3];
for(int j = 0;j < 3;j++)
{
cin >> x[j] >> y[j];
}
bool dis = false;
long long a = distance(x[0], y[0], x[1], y[1]);
long long b = distance(x[1], y[1], x[2], y[2]);
if(a == b)
{
dis = true;
}
bool line = false;
long long tempy1 = y[1] - y[0];
long long tempx1 = x[1] - x[0];
long long tempy2 = y[2] - y[0];
long long tempx2 = x[2] - x[0];
long long xp = tempy1 * tempx2;
long long yp = tempy2 * tempx1;
if((xp - yp) == 0)
{
line = true;
}
if(dis && !line)
{
cout << "Case #" << i << ": Yes" << endl;
}
else
{
cout << "Case #" << i << ": No" << endl;
}
}
}
long long distance(long long ax,long long ay,long long bx,long long by)
{
long long x = (ax - bx) * (ax - bx);
long long y = (ay - by) * (ay - by);
return x + y;
}
猫叔的计算器
最近猫叔很怀恋高中的计算器,但是猫叔自从学习了编程以后就不用计算器了,所以猫叔打算利用自己的规则打造一个属于自己的计算器!
经过多天的研究,猫叔终于研究了出来,首先要给计算器一个数x,表示初始数,然后给予”+-*/”这四个符号中的其中一个,接着是一个数字对初始数据进行以计算机中的整数形式计算规则计算得到一个新的数,后面的操作以此类推!最后就会得到猫叔想要的结果!猫叔的眼睛不是很好,你可以告诉猫叔答案是多少吗?
对+-*/进行字符串切割
,然后把运算符和数字分开放在两个vector当中,然后进行运算
赛后发现挺蠢的,可以通过getchar() != '\n'
获得运算符,然后通过scanf
获取运算符后的数字,不需要这么麻烦…
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
vector<string> split(string str, string separator);
long long str2int(string string_temp);
vector<char> chars;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
int first;
string str;
cin >> first;
cin >> str;
long long total = first;
vector<string> ints = split(str, "+-*/");
vector<string>::iterator ints_iter = ints.begin();
for (vector<char>::iterator chars_iter = chars.begin(); chars_iter != chars.end(); chars_iter++){
switch (*chars_iter)
{
case '+':
total += str2int(*ints_iter);
ints_iter++;
break;
case '-':
total -= str2int(*ints_iter);
ints_iter++;
break;
case '*':
total *= str2int(*ints_iter);
ints_iter++;
break;
case '/':
total /= str2int(*ints_iter);
ints_iter++;
break;
}
}
chars.clear();
cout << "Case #" << i << ":" << total << endl;
}
}
vector<string> split(string str, string separator) {
vector<string> result;
int cutAt;
while ((cutAt = str.find_first_of(separator)) != str.npos) {
chars.push_back(str[cutAt]);
if (cutAt>0) {
result.push_back(str.substr(0, cutAt));
}
str = str.substr(cutAt + 1);
}
if (str.length()>0) {
result.push_back(str);
}
return result;
}
long long str2int(string string_temp)
{
long long num;
stringstream ss;
ss << string_temp;
ss >> num;
return num;
}
寻找旅馆的学长
学长在一个夜黑风高的晚上出去了emmmm,进入正题,学长需要选择一家旅店,但他有着选择恐惧症,有时候想要依据距离来选择旅店而有的时候却想要依据价格来选择旅店,学长打开某APP开始搜索,已知APP的搜索数据,请你设计一个程序来减轻学长的恐惧症,将最优方案调至成三个,请依照学长的需求提供三个最优方案给他。
- 当以价格优先时,如果最优价格相同,则再以距离优先。
- 当以距离优先时,如果最优距离相同,则再以价格优先。
考对sort
的使用,写好两种不同优先级
的排序方法进行了
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct hotel
{
int m;
int n;
};
bool compareM(hotel hotel1, hotel hotel2);
bool compareN(hotel hotel1, hotel hotel2);
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
int m[100000], n[10000], count = 0;
vector<hotel> hotels;
while (true)
{
cin >> m[count] >> n[count];
if (m[count] == 0 && n[count] == 0)
{
break;
}
hotel hotel = { m[count],n[count] };
hotels.push_back(hotel);
count++;
}
int flag;
cin >> flag;
vector<hotel>::iterator hotel_iter = hotels.begin();
cout << "Case #" << i << ":" << endl;
if (flag == 0)
{
sort(hotels.begin(), hotels.end(), compareM);
cout << (*hotel_iter).m << " " << (*hotel_iter).n << endl;
hotel_iter++;
cout << (*hotel_iter).m << " " << (*hotel_iter).n << endl;
hotel_iter++;
cout << (*hotel_iter).m << " " << (*hotel_iter).n << endl;
}
else
{
sort(hotels.begin(), hotels.end(), compareN);
cout << (*hotel_iter).m << " " << (*hotel_iter).n << endl;
hotel_iter++;
cout << (*hotel_iter).m << " " << (*hotel_iter).n << endl;
hotel_iter++;
cout << (*hotel_iter).m << " " << (*hotel_iter).n << endl;
}
}
}
bool compareM(hotel hotel1, hotel hotel2)
{
if (hotel1.m < hotel2.m)
{
return true;
}
else if (hotel1.m == hotel2.m && hotel1.n < hotel2.n)
{
return true;
}
return false;
}
bool compareN(hotel hotel1,hotel hotel2)
{
if(hotel1.n < hotel2.n)
{
return true;
}
else if(hotel1.n == hotel2.n && hotel1.m < hotel2.m)
{
return true;
}
return false;
}
猫叔的烦恼
在寒冷的冬天,外面有很多的流浪猫,猫叔看着它们无家可归都把它们接到了家里,但是猫叔是南方人家里没有任何的取暖设施,只能帮流浪猫们铺地毯取暖,地毯是矩形的,一共有n张,编号从1-n,现在将这些地毯依次铺好,每个地毯依次叠加起来,铺完n张地毯以后,猫叔把流浪猫们放在一个(a,b)坐标上,想看看,流浪猫休息的地方上最上面的地毯是哪张?你能帮帮猫叔吗?
判断哪个地毯放在最上方就行了,时刻保持最优值
,赛后发现蠢了,我是从下往上判断的,其实从上往下判断还能提前break掉
…
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
int num;
cin >> num;
int x[num];
int y[num];
int h[num];
int w[num];
for(int j = 0;j < num;j++){
cin >> x[j] >> y[j] >> h[j] >> w[j];
}
int cat_x,cat_y;
cin >> cat_x >> cat_y;
int top = -1;
for(int j = 0;j < num;j++){
if(x[j] <= cat_x && cat_x <= (x[j] + h[j]) &&
y[j] <= cat_y && cat_y <= (y[j] + w[j])){
top = j + 1;
}
}
cout << "Case #" << i << ": " << top << endl;
}
}