只A了一个,后面全WA了
代码如下
using namespace std;
struct Person
{
int number;
int score;
};
struct Person person[5001];
int main()
{
int n,m;
int total;
cin >> n >> m;
total = (int) (m*1.5 + 0.5);
for(int i = 1; i <= n; i++)
cin >> person[i].number >> person[i].score;
for(int i = 1; i <= n-1; i++)
for(int j = 1; j <= n-i; j++)
{
struct Person temp;
if(person[j].score < person[j + 1].score)
{
temp = person[j];
person[j] = person[j + 1];
person[j + 1] = temp;
}
if(person[j].score == person[j + 1].score && person[j].number > person[j + 1].number )
{
temp = person[j];
person[j] = person[j + 1];
person[j + 1] = temp;
}
}
cout << person[total].score << " " << total << endl;
for(int i = 1; i <= total; i++)
cout << person[i].number << " " << person[i].score << endl;
return 0;
}
样例过了,但不知道哪里错了,求教,谢谢!
为什么你的total要加0.5,你错误的原因在于,这个0.5,我看不懂你这个零点五的作用在哪,于是我删了。
你的程序错的关键在于total的错误定位,也就是分数线定错了,我删掉了0.5之后发现,样例只输出了4个人,意味着,当分数相同的时候,名次稍后就不会录入(当然此时的名次是按照他们的号码来排序的,太不公平了),我对你的代码进行了两个操作,一,删零点五,二,排序后重定位total。代码如下(注意,样例只是样例,不能只看样例,还需要自己想遍可能的情况)
#include<iostream> using namespace std; struct Person { int number; int score; }; struct Person person[5001]; int main() { int n,m; int total; cin >> n >> m; total = int(m*1.5); //this time doesn't wrong for(int i = 1; i <= n; i++) cin >> person[i].number >> person[i].score; for(int i = 1; i <= n-1; i++) for(int j = 1; j <= n-i; j++) { struct Person temp; if(person[j].score < person[j + 1].score) { temp = person[j]; person[j] = person[j + 1]; person[j + 1] = temp; } if(person[j].score == person[j + 1].score && person[j].number > person[j + 1].number ) { temp = person[j]; person[j] = person[j + 1]; person[j + 1] = temp; } } //look here! while(person[total].score==person[total+1].score) total++; cout << person[total].score << " " << total << endl; for(int i = 1; i <= total; i++) cout << person[i].number << " " << person[i].score << endl; return 0; }
perfect
谢谢谢谢!我加 0.5 是因为题干里讲 m * 1.5 后向下取整。(比如 3 * 1.5 == 4.5 ,向下取整应该等于 5 ,所以我加了个 0.5 ,如果 4 * 1.5 + 0.5 == 6.5 ,强制类型转换后依然等于 6 ,这样正好可以向下取整 )
@goalltheway: 你理解的向下取整理解错了,向下取整的意思是4.5-》4
@Conan-jine: 噗。。好吧。谢谢!
这是大水题呀
一开始没审好题。。。