鱼C论坛

 找回密码
 立即注册
查看: 837|回复: 3

[已解决]出生排序问题 求助!!!!

[复制链接]
发表于 2021-12-21 16:47:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>

struct fri
{
    char name[11];
    char brithday[10];
    char pNumber[18];
    int temp;
}tt;
int main()
{
    struct fri arr[10];
    int n;
    scanf("%d",&n);
    int i;
    for(i=0;i<n;i++)
    {
       scanf("%s %s %s",&arr[i].name,&arr[i].brithday,&arr[i].pNumber);
       int temp1=(arr[i].brithday[0]-'0')*1000+(arr[i].brithday[1]-'0')*100+(arr[i].brithday[2]-'0')*10+(arr[i].brithday[3]-'0')*1;
       int temp2=(arr[i].brithday[4]-'0')*10+(arr[i].brithday[5]-'0')*1;
       int temp3=(arr[i].brithday[6]-'0')*10+(arr[i].brithday[7]-'0')*1;
       arr[i].temp=(9999-temp1)*365+(12-temp2)*30+(30-temp3)*1;
    }
    int index;
    int j;
    for(i=0;i<n-1;i++)
    {
        index=i;
        tt=arr[i];
        for(j=i+1;j<n;j++)
        {
            if(arr[j].temp>=arr[index].temp) index=j;
        }
        arr[i]=arr[index];
        arr[index]=tt;
    }
    for(i=0;i<n;i++)
    {
        printf("%s %s %s\n",arr[i].name,arr[i].brithday,arr[i].pNumber);
    }
    return 0;
}
输入n个朋友的信息,包括姓名、生日、电话号码,本题要求编写程序,按照年龄从大到小的顺序依次输出通讯录。题目保证所有人的生日均不相同。

输入格式:

输入第一行给出正整数n(<10)。随后n行,每行按照“姓名 生日 电话号码”的格式给出一位朋友的信息,其中“姓名”是长度不超过10的英文字母组成的字符串,“生日”是yyyymmdd格式的日期,“电话号码”是不超过17位的数字及+、-组成的字符串。
以上是题目要去,我是这段代码看不懂
int temp1=(arr[i].brithday[0]-'0')*1000+(arr[i].brithday[1]-'0')*100+(arr[i].brithday[2]-'0')*10+(arr[i].brithday[3]-'0')*1;
       int temp2=(arr[i].brithday[4]-'0')*10+(arr[i].brithday[5]-'0')*1;
       int temp3=(arr[i].brithday[6]-'0')*10+(arr[i].brithday[7]-'0')*1;
       arr[i].temp=(9999-temp1)*365+(12-temp2)*30+(30-temp3)*1;
最佳答案
2021-12-21 17:15:34
本帖最后由 番杰 于 2021-12-22 11:44 编辑

1.
int temp1=(arr[i].brithday[0]-'0')*1000+(arr[i].brithday[1]-'0')*100+(arr[i].brithday[2]-'0')*10+(arr[i].brithday[3]-'0')*1;
分开看:
1)(arr[ i].brithday[0]-'0')*1000
其中arr[ i]中的i表示第几个人,brithday[0]表示年份的千位;
因为brithday使用的是char定义的,所以它里面存放的是字符型数据(ascii码值);
这里使用arr[ i].brithday[0]减去一个字符型的'0':就是把字符型的数据转换为整形;
举个例子:brithday[0]中存放的是'5'(它是字符型,所以实际存放的数值为35H)
而'0'对应的acsii数值为30;
所以arr[ i].brithday[0]-'0' = ‘5’ - ‘0’ = 35 - 30 = 5
就顺利的把字符5变成了数字5;
后面乘1000;前面也说了brithday[0]表示年份的千位;所以需要乘1000;

2)(arr[i ].brithday[1]-'0')*100
同理:brithday[1]表示年份的百位;

3)(arr[i ].brithday[2]-'0')*10
同理:brithday[2]表示年份的十位;

4)(arr[i ].brithday[3]-'0')*1
同理:brithday[3]表示年份的个位;

2.
int temp2=(arr[i].brithday[4]-'0')*10+(arr[i].brithday[5]-'0')*1;
这个就跟1一样:
brithday[4]表示月份的十位;
brithday[5]表示月份的个位;

3.
int temp3=(arr[i].brithday[6]-'0')*10+(arr[i].brithday[7]-'0')*1;
brithday[6]表示日期的十位;
brithday[7]表示日期的个位;

举例:若brithday[]中存放的数据分别为:{'2','0','2','1','1','2','2','1,}
那么经过该式:
temp1 = 2*1000 +0*100+2*10+1*1 = 2021(年份)
temp2 = 1*10+2*1 = 12(月份)
temp3 = 2*10+2*1 = 21(日期)

4.
arr[i].temp=(9999-temp1)*365+(12-temp2)*30+(30-temp3)*1;
这个用来根据生日来排序的;
就是看那个人的temp大就表示这个人出生的早;

举个例子:
小蓝:1999年1月1日出生
小绿:2000年1月1日出生

所以:
小蓝的temp = (9999-1999)*365+(12-1)*30+(30-1)*1 = 2,920,000 + 359 = 2920359
小绿的temp = (9999-2000)*365+(12-1)*30+(30-1)*1 = 2,919,635 + 359 = 2919994

可以看出小蓝的temp > 小绿的temp
所以可以看出temp大,就代表这个人出生的早。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-21 17:15:34 | 显示全部楼层    本楼为最佳答案   
本帖最后由 番杰 于 2021-12-22 11:44 编辑

1.
int temp1=(arr[i].brithday[0]-'0')*1000+(arr[i].brithday[1]-'0')*100+(arr[i].brithday[2]-'0')*10+(arr[i].brithday[3]-'0')*1;
分开看:
1)(arr[ i].brithday[0]-'0')*1000
其中arr[ i]中的i表示第几个人,brithday[0]表示年份的千位;
因为brithday使用的是char定义的,所以它里面存放的是字符型数据(ascii码值);
这里使用arr[ i].brithday[0]减去一个字符型的'0':就是把字符型的数据转换为整形;
举个例子:brithday[0]中存放的是'5'(它是字符型,所以实际存放的数值为35H)
而'0'对应的acsii数值为30;
所以arr[ i].brithday[0]-'0' = ‘5’ - ‘0’ = 35 - 30 = 5
就顺利的把字符5变成了数字5;
后面乘1000;前面也说了brithday[0]表示年份的千位;所以需要乘1000;

2)(arr[i ].brithday[1]-'0')*100
同理:brithday[1]表示年份的百位;

3)(arr[i ].brithday[2]-'0')*10
同理:brithday[2]表示年份的十位;

4)(arr[i ].brithday[3]-'0')*1
同理:brithday[3]表示年份的个位;

2.
int temp2=(arr[i].brithday[4]-'0')*10+(arr[i].brithday[5]-'0')*1;
这个就跟1一样:
brithday[4]表示月份的十位;
brithday[5]表示月份的个位;

3.
int temp3=(arr[i].brithday[6]-'0')*10+(arr[i].brithday[7]-'0')*1;
brithday[6]表示日期的十位;
brithday[7]表示日期的个位;

举例:若brithday[]中存放的数据分别为:{'2','0','2','1','1','2','2','1,}
那么经过该式:
temp1 = 2*1000 +0*100+2*10+1*1 = 2021(年份)
temp2 = 1*10+2*1 = 12(月份)
temp3 = 2*10+2*1 = 21(日期)

4.
arr[i].temp=(9999-temp1)*365+(12-temp2)*30+(30-temp3)*1;
这个用来根据生日来排序的;
就是看那个人的temp大就表示这个人出生的早;

举个例子:
小蓝:1999年1月1日出生
小绿:2000年1月1日出生

所以:
小蓝的temp = (9999-1999)*365+(12-1)*30+(30-1)*1 = 2,920,000 + 359 = 2920359
小绿的temp = (9999-2000)*365+(12-1)*30+(30-1)*1 = 2,919,635 + 359 = 2919994

可以看出小蓝的temp > 小绿的temp
所以可以看出temp大,就代表这个人出生的早。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-21 17:22:21 | 显示全部楼层
本帖最后由 jackz007 于 2021-12-21 17:24 编辑

        那几句就是为了把字符串变成数字,其实,根本不需要那么麻烦,字符串变数字可以使用 sscanf() 函数,直接变。
#include<stdio.h>

struct fri
{
    char name[11]           ;
    char birthday[10]       ;
    char pNumber[18]        ;
    int temp                ;
}                           ;

int main(void)
{
        struct fri arr[10] , t                                                                    ;
        int i , j , n                                                                             ;
        scanf("%d" , & n)                                                                         ;
        for(i = 0 ; i < n ; i ++) {
                scanf("%s %s %s" , arr[i] . name , arr[i] . birthday , arr[i] . pNumber)          ;
                sscanf(arr[i] . birthday , "%d" , & arr[i] . temp)                                ;
        }
        for(i = 0 ; i < n - 1 ; i ++) {
                for(j = i + 1 ; j && arr[j] . temp < arr[j - 1] . temp ; j --) {
                        t = arr[j - 1]                                                            ;
                        arr[j - 1] = arr[j]                                                       ;
                        arr[j] = t                                                                ;
                 }
        }
        printf("output is :\n")                                                                   ;
        for(i = 0 ; i < n ; i ++) printf("%s %s %s\n",arr[i].name,arr[i].birthday,arr[i].pNumber) ;
}
        编译、运行实况:
D:\0002.Exercise\C>g++ -o x x.c

D:\0002.Exercise\C>x
10
abc 19520801 123456
cde 19320103 1399987
efg 20080308 1379932
ghi 19411218 1560990
ijk 20100611 1301234
klm 20030501 1314321
mno 19910824 1389876
opq 19650911 1234567
qrs 19731028 6658788
stu 19881123 5876089
output is :
cde 19320103 1399987
ghi 19411218 1560990
abc 19520801 123456
opq 19650911 1234567
qrs 19731028 6658788
stu 19881123 5876089
mno 19910824 1389876
klm 20030501 1314321
efg 20080308 1379932
ijk 20100611 1301234

D:\0002.Exercise\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-23 09:11:18 | 显示全部楼层
jackz007 发表于 2021-12-21 17:22
那几句就是为了把字符串变成数字,其实,根本不需要那么麻烦,字符串变数字可以使用 sscanf() 函数 ...

我刚学不久,还没学到那里,等学到那里再回来看看你的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-28 06:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表