poj 2021 Relative Relatives

题目链接:http://poj.org/problem?id=2021

思路:

由于数据较小,采用O(N^2)的暴力算法,算出所有后代的年龄,再排序输出。

代码:

#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;
#define MAX_N ( 100 + 10 )

typedef struct Descendant
{
    char Name[30];
    int Age;
}Desd;

char Father[MAX_N][30];
char Child[MAX_N][30];
int Age[MAX_N];
Desd Descen[MAX_N];
int Cmp( Desd a, Desd b );

int main()
{
    int n, Count;

    Count = 0;
    strcpy( Descen[0].Name, "Ted" );
    Descen[0].Age = 100;

    scanf( "%d", &n );
    for ( int i = 0; i < n; ++i )
    {
        int Num, Index_Father, EndDescen;

        Index_Father = EndDescen = 0;
        Count++;

        scanf( "%d\n", &Num );
        for ( int j = 0; j < Num; ++j )
            scanf( "%s %s %d", Father[j], Child[j], &Age[j] );

        while ( Index_Father < Num )
        {
            for ( int m = 0; m < Num; ++m )
            {
                if ( strcmp( Descen[Index_Father].Name, Father[m] ) == 0 )
                {
                    EndDescen++;
                    strcpy( Descen[EndDescen].Name, Child[m] );
                    Descen[EndDescen].Age = Descen[Index_Father].Age - Age[m];
                }
            }
            Index_Father++;
        }

        sort( Descen, Descen + Num + 1, Cmp );

        printf( "DATASET %d\n", Count );
        for ( int n = 1; n <= Num; ++n )
            printf( "%s %d\n", Descen[n].Name, Descen[n].Age );
    }

    return 0;
}

int Cmp( Desd a, Desd b )
{
    if ( a.Age == b.Age )
        return strcmp( a.Name, b.Name ) < 0;
    else
        return a.Age > b.Age;
}
时间: 2024-11-09 01:26:37

poj 2021 Relative Relatives的相关文章

POJ 2021

#include <iostream> #include <string> #include <algorithm> #define MAXN 105 using namespace std; struct node { string name_f; string name; int f_c; int age; }; node _m[MAXN]; int find_pre(string _name); int n; bool op(node a,node b); int

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

poj2021 字符串处理 BFS

Relative Relatives Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3430   Accepted: 1504 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 surpr

[POJ 2407]Relatives(欧拉函数)

Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz. Input There are several

POJ 2407 Relatives 欧拉函数题解

最基本的欧拉函数: 欧拉函数:求小于n的与n互质的个数 欧兰函数公式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)-..(1-1/pn),其中p1, p2--pn为x的所有质因数 就是要求这样的式子啦,不过求这条式子,相信有很多种方法可以求,这个不是难题: 不过问题是如何巧妙地求,如何简洁地写出代码. 直接硬求,或者求出质因数之后求都不是巧妙的方法了,参考了下别人的代码才知道可以写的这么巧妙的. 下面程序可以说是连消带打地求式子结果,分解质因子,可以如此简明地把解

数论 - 欧拉函数模板题 --- poj 2407 : Relatives

Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if ther

POJ 2407 Relatives &amp;&amp; UVA 10299 Relatives(欧拉函数)

[题目链接]:click here~~ [题目大意]:欧拉函数:求少于或等于n的数中与n互素的数的个数:n <= 1,000,000,000. [思路]:裸欧拉函数,注意特判n==1的情况,n==1的情况下,应该输出0,poj依然判断1也可以过,但是老牌ojUVA必须是0才过,注意一下. 代码: #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>

POJ 2407 Relatives(欧拉函数)

Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11801   Accepted: 5780 Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if ther

POJ 2407 Relatives(欧拉函数入门题)

Relatives Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz. Input There are several t