【基础】海岛寻宝
时间限制: 1.000 Sec 内存限制: 16 MB
题目描述
某个海岛上埋藏着多件宝物,每件宝物都有一个确切的位置,宝物的位置用一对数(x,y)来表示。其中 x表示该宝物离海洋中某个指定地点的水平距离,y表示该宝物离海洋中某个指定地点的垂直距离。已知宝物离海洋某个指定地点的直线距离L可以由如下公式计算:
海洋探险队的任务是:找出名称包含某种特征字符串的所有宝物,并按直线距离由近到远的顺序把它们的位置记录下来,以方便将来取出宝物。假若你是海洋探险队的一员,你能编程完成这一工作吗?
输入
共有n+2行。
第1行为要寻宝物的特征字符串;
第2行为岛上的宝物数n( 0 < n <= 100 );
第3行至第n+2行为每件宝物的位置数据和宝物名称。
输出
按距离由近到远输出所找到宝物的位置,每件宝物的位置数据占一行。若找不到宝物,则全以“-1”输出(输出n个-1)。
样例
输入1 复制
ep
2
1.5 2.8 goden
2.4 5 word
输出1 复制
-1 -1
输入2 复制
ner
3
5 2.4 liner
2.5 8.3 suerp
1.5 2 winervis
输出2 复制
1.5 2
5 2.4
错误代码:
#include <bits/stdc++.h>
using namespace std;
struct ikun {
double x;
double y;
string name;
};
bool compareDistance(const ikun& t1, const ikun& t2) {
return sqrt(pow(t1.x, 2) + pow(t1.y, 2)) < sqrt(pow(t2.x, 2) + pow(t2.y, 2));
}
int main() {
string feature;
int n;
cin >> feature >> n;
vector<ikun> treasures(n);
for (int i = 0; i < n; i++) {
cin >> treasures[i].x >> treasures[i].y >> treasures[i].name;
}
vector<ikun> foundTreasures;
for (const auto& treasure : treasures) {
if (treasure.name.find(feature) != string::npos) {
foundTreasures.push_back(treasure);
}
}
if (foundTreasures.empty()) {
cout << "-1 -1" << endl;
} else {
sort(foundTreasures.begin(), foundTreasures.end(), compareDistance);
for (const auto& treasure : foundTreasures) {
cout << treasure.x << " " << treasure.y << endl;
}
}
return 0;
}
你稍等一会,代码马上给你,思路:字符串先判断,在算距离,最后排序输出
代码:
#include<bits/stdc++.h>
using namespace std;
struct node{
double x,y;
double dis;
bool flag=false;
string name;
}a[10005];
bool cmp1(node t1,node t2)
{
return t1.flag>t2.flag;
}
bool cmp2(node t1,node t2)
{
return t1.dis<t2.dis;
}
int main()
{
string str;
int n;
cin>>str>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].x>>a[i].y>>a[i].name;
a[i].dis=sqrt(pow(a[i].x,2)+pow(a[i].y,2));
}
int num=0;
for(int i=1;i<=n;i++)
{
int b=a[i].name.find(str);
if(b!=string::npos)
{
a[i].flag=true;
num++;
}
}
sort(a+1,a+n+1,cmp1);
sort(a+1,a+num+1,cmp2);
for(int i=1;i<=n;i++)
if(a[i].flag) cout<<a[i].x<<" "<<a[i].y<<endl;
else cout<<"-1 ";
return 0;
}
#include <bits/stdc++.h>
using namespace std;
struct Treasure {
double x;
double y;
string name;
};
double distance(const Treasure& t) {
return sqrt(pow(t.x, 2) + pow(t.y, 2));
}
int main() {
string feature;
int n;
cin >> feature >> n;
vector<Treasure> treasures{
{1.1, 2.2, "金币"},
{3.3, 4.4, "钻石"},
{5.5, 6.6, "银币"}
};
auto found = find_if(treasures.begin(), treasures.end(),
[&feature](const auto& t) {
return t.name.find(feature) != string::npos;
});
if (found == treasures.end()) {
cout << "-1 -1";
} else {
sort(found, treasures.end(),
[](const auto& t1, const auto& t2) {
return distance(t1) < distance(t2);
});
for (const auto& t : found) {
cout << t.x << " " << t.y << "\n";
}
}
}
结果
1.
#include <bits/stdc++.h>
using namespace std;
struct Ikun {
double x;
double y;
string name;
};
bool CompareDistance(const Ikun& t1, const Ikun& t2) {
return sqrt(pow(t1.x, 2) + pow(t1.y, 2)) < sqrt(pow(t2.x, 2) + pow(t2.y, 2));
}
int main() {
string feature;
int numTreasures;
cin >> feature >> numTreasures;
vector<Ikun> treasures(numTreasures);
for (int i = 0; i < numTreasures; i++) {
cin >> treasures[i].x >> treasures[i].y >> treasures[i].name;
}
vector<Ikun> foundTreasures;
for (const auto& treasure : treasures) {
if (feature.empty() || treasure.name.find(feature) != string::npos) {
foundTreasures.push_back(treasure);
}
}
if (foundTreasures.empty()) {
for (int i = 0; i < numTreasures; i++) {
cout << "-1 -1" << endl;
}
} else {
sort(foundTreasures.begin(), foundTreasures.end(), CompareDistance);
for (const auto& treasure : foundTreasures) {
cout << treasure.x << " " << treasure.y << endl;
}
}
return 0;
}