您的位置:首页 > 其它

poj 2021 Relative Relatives

2014-07-30 13:04 141 查看
Description
Today is Ted's 100th birthday. A few weeks ago, you were selected by the family to contact all of Ted's descendants and organize a surprise party. To make this task easier, you created an age-prioritized list of everyone descended
from Ted. Descendants of the same age are listed in dictionary order.

The only materials you had to aid you were birth certificates. Oddly enough, these birth certificates were not dated. They simply listed the father's name, the child's name, and the father's exact age when the baby was born.

Input
Input to this problem will begin with line containing a single integer n indicating the number of data sets. Each data set will be formatted according to the following description.

A single data set has 2 components:
Descendant Count - A line containing a single integer X (where 0 < X < 100) indicating the number of Ted's descendants.

Birth Certificate List - Data for X birth certificates, with one certificate's data per line. Each certificate's data will be of the format "FNAME CNAME FAGE" where:

FNAME is the father's name.

CNAME is the child's name.

FAGE is the integer age of the father on the date of CNAMEs birth.

Note:
Names are unique identifiers of individuals and contain no embedded white space.

All of Ted's descendants share Ted's birthday. Therefore, the age difference between any two is an integer number of years. (For those of you that are really picky, assume they were all born at the exact same hour, minute, second, etc... of their birth
year.)

You have a birth certificate for all of Ted's descendants (a complete collection).

Output
For each data set, there will be X+1 lines of output. The first will read, "DATASET Y", where Y is 1 for the first data set, 2 for the second, etc. The subsequent X lines constitute your age-prioritized list of Ted's descendants
along with their ages using the format "NAME AGE". Descendants of the same age will be listed in dictionary order.

http://poj.org/problem?id=2021
 
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct people
{
int age;
char name[20];
}dec;

int cmp(const void *a,const void *b)
{
dec *deca = (dec *)a;
dec *decb = (dec *)b;
if(deca->age != decb->age) return decb->age - deca->age;
else 	return strcmp(deca->name,decb->name);
}

int main()
{
int n, count, fage[100];
char fname[100][20],cname[100][20];
dec a[100];
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&count);
for(int j = 0; j < count; j++)
scanf("%s %s %d",fname[j],&cname[j],&fage[j]);
a[0].age = 100;
strcpy(a[0].name,"Ted");
int m = 1;
for(int j = 0; j < m; j++)
{
for(int k = 0; k < count; k++)
{
if(strcmp(a[j].name,fname[k]) == 0)
{
a[m].age = a[j].age - fage[k];
strcpy(a[m].name,cname[k]);
m++;
}
}
}
qsort(a,m,sizeof(a[0]),cmp);
printf("DATASET %d\n",i);
for(int l = 1; l < m; l++)
printf("%s %d\n",a[l].name,a[l].age);

}
system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: