2021-12-23-SWUSTOJ580: The World Population Explosion(结构体之间变量的交换方法,strcmp用法案列,利用for循环的复杂排序情况)

题目描述:

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

思路:

在做这个题之前我们先来学习一下C语言的一个库函数

这里面最重要的一点应当是

 思路:

 如果在排序的for循环里两个if语句弄反了运行出来的结果就会造成最后两个的错误:

if语句顺序正确的情况下运行出来的结果应当是:

 

源代码:

#include<stdio.h>
#include<string.h>
struct worldpopulation
{
    char name[100];
    int lastyear;
    int thisyear;
    int dvalue;
};
int main()
{
    struct worldpopulation country[100];
    struct worldpopulation tmp;//这里的空结构体非常重要,用于交换两个结构体里面的内容
    int n = 0;
    int i = 0;
    int j = 0;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%s",country[i].name );//数组就是一个地址,数组不需要加&符号
    }
    for (i = 0; i < n; i++)
    {
        scanf("%d", &country[i].lastyear);//而int类型的数需要加&符号
    }
    for (i = 0; i < n; i++)
    {
        scanf("%d", &country[i].thisyear);
    }
    for (i = 0; i < n; i++)
    {
        country[i].dvalue = country[i].thisyear - country[i].lastyear;
    }
    for (i = 0; i < n - 1; i++)//i之所以要小于n-1而不小于n是因为最后一次比较时i要为倒数第二个数
    {
        for (j = i; j < n ; j++)//j之所以要小于n是因为最后一次比较时j可以到达最后一个数
        {
            if (country[i].dvalue < country[j].dvalue)
            {
                tmp = country[i];
                country[i] = country[j];
                country[j] = tmp;
            }//通过这一个步骤会完成降序排序
            if (country[i].dvalue == 0)
            {
                tmp = country[i];
                country[i] = country[j];
                country[j] = tmp;
            }//这个等于0的情况下非常重要,因为始终要让等于0的情况往后面排,所以通过不断地往后交换可让0最终出现在后面
            //这里有一个十分需要注意的点
            //在排序完成后,应当先判断是否等于0的情况进行交换
            //这是为什么呢,我们来分析一下
            //如果出现了两组数据又等于0并且又相等的时候,我们必须先将0往后排,排完了过后再来判断两组数据等于0相等的情况,然后再通过首字母大小来交换
            //如果我们先判断了两个相等的情况,也就是第三个if语句在第二个语句后面的时候
            //就会造成在最后一次if语句的时候,两个等于0的情况又被交换一次,就无法满足首字母的字符顺序这个情况
            //所以这里一定要注意最后这两个if语句的优先级
            if (country[i].dvalue == country[j].dvalue&&strcmp(country[i].name, country[j].name) > 0)
            {
                tmp = country[i];
                country[i] = country[j];
                country[j] = tmp;
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%d %s\n", country[i].dvalue, country[i].name);
    }
    return 0;

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

~|Bernard|

你的鼓励是我写下去最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值