题目描述:
In the past, when population grew, there was unexplored territory to inhabit. But now, almost all the habitable land has been explored. The world's population may reach 8.7 billion in 2033. It is clear that world population is a serious issue that needs careful attention. Human beings are unique to solve problems through cultural evolution. Facing the world population explosion in the near future, we must carry out the birth control program in order to save the mankind and save the world. And now we have the population amount of some country of the world in last year and this year. your task is to differ the countries whose population is changed from the data below and sort them in descending order and return the changing value and names of those countries whose population amount are not changed as well.
输入:
The input consists of only one test case, followed by 4 lines, the first line is a integer N indicating the amount of the countries, and then input last year’s and this year’s population amount of every country in the last two lines before the names of the countries is inputted in the second line.
输出:
You should output the countries firstly whose population are changed followed by the rest of those whose population are unchanged. If the changes are the same between two countries, lexicographic order should be used in your code.
样例输入
7 USA CHINA JAPAN KOREA CUBA ARGENTINA PERU 100 200 150 50 9 2 22 120 240 140 10 9 2 12
样例输出
40 CHINA 20 USA -10 JAPAN -10 PERU -40 KOREA 0 ARGENTINA 0 CUBA
代码
#include<iostream>
using namespace std;
#include<algorithm>
#include <string>
class country{
public:
string name; //名字
int last; //去年人口
int now; //今年人口
int s; //去年和今年的差
};
class mysort{ //仿函数,对正负和国名字符串排序
public:
bool operator ()(country a,country b)
{
if(a.s==b.s)
{
return a.name<b.name;
}
else{
return a.s>b.s;
}
}
};
country zheng[200];
country fu[200];
country ling[200];
int z=0,f=0,l=0;
int main()
{
int n;
cin>>n;
country arr[n];
for(int i=0;i<n;i++) //输入名字
{
cin>>arr[i].name;
}
for(int i=0;i<n;i++) //输入去年人口
{
cin>>arr[i].last;
}
for(int i=0;i<n;i++) //输入今年人口
{
cin>>arr[i].now;
}
for(int i=0;i<n;i++) //计算每个国家去年和今年人口的差
{
arr[i].s=arr[i].now-arr[i].last;
}
for(int i=0;i<n;i++) //根据s的正负进行归类
{
if(arr[i].s>0)
{
zheng[z]=arr[i];
z++;
}
if(arr[i].s<0)
{
fu[f]=arr[i];
f++;
}
if(arr[i].s==0)
{
ling[l]=arr[i];
l++;
}
}
sort(zheng,zheng+z,mysort());//排序
sort(fu,fu+f,mysort());
sort(ling,ling+l,mysort());
for(int i=0;i<z;i++) //输出
{
cout<<zheng[i].s<<" "<<zheng[i].name<<endl;
}
for(int i=0;i<f;i++)
{
cout<<fu[i].s<<" "<<fu[i].name<<endl;
}
for(int i=0;i<l;i++)
{
cout<<ling[i].s<<" "<<ling[i].name<<endl;
}
return 0;
}