SWUST OJ #580 The World Population Explosion C++实现

题目描述:

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;
	
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力不做闲鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值