算法筆記第三章練習題

人口普查

某城鎮進行人口普查,得到了全體居民的生日。現請你寫個程序,找出鎮上最年長和最年輕的人。

這裏確保每個輸入的日期都是合法的,但不一定是合理的——假設已知鎮上沒有超過 200 歲的老人,而今天是 2014 年 9 月 6 日,所以超過 200 歲的生日和未出生的生日都是不合理的,應該被過濾掉。

輸入格式:

輸入在第一行給出正整數 N,取值在(0,10​5​​];隨後 N 行,每行給出 1 個人的姓名(由不超過 5 個英文字母組成的字符串)、以及按 yyyy/mm/dd(即年/月/日)格式給出的生日。題目保證最年長和最年輕的人沒有並列。

輸出格式:

在一行中順序輸出有效生日的個數、最年長人和最年輕人的姓名,其間以空格分隔。

輸入樣例:

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

輸出樣例:

3 Tom John

思路:

讀入日期時判斷是否是合法日期,如果是,令num++,再與oldest、youngest比較,然後更新。

注意如果合法日期個數爲0的情況,如果漏掉這個情況會多輸出空格導致測試點3格式錯誤。

# include <cstdio>
struct  person
{
    char name[10];
    int yy,mm,dd;
}oldest,youngest,temp,left,right;
//oldest和youngest存放最年長和最年輕的人,left 和right用來存放左右邊界

bool LessEqu(person a,person b)
{
    //如果a 的日期小於等於b返回true
    if (a.yy != b.yy) return a.yy <= b.yy;
    else if (a.mm!= b.mm) return a.mm <=b.mm;
    else  return a.dd <= b.dd;
}
bool MoreEqu(person a,person b)
{
    //如果a的日期大於等於b返回true
    if (a.yy != b.yy) return a.yy >= b.yy;
    else if (a.mm!= b.mm) return a.mm >=b.mm;
    else  return a.dd >= b.dd;
}
void init()
{
    youngest.yy = left.yy =1814;
    oldest.yy = right.yy = 2014;
    youngest.mm = oldest.mm = left.mm = right.mm = 9;
    youngest.dd = oldest.dd = left.dd = right.dd = 6;
}
int main()
{
    init();     //初始化youngest oldest ,left right的值
    int n,num = 0;    //num 存放合法日期人數
    scanf("%d",&n);
    for(int i = 0;i < n; i++)
    {
        scanf("%s %d/%d/%d",temp.name,&temp.yy,&temp.mm,&temp.dd);
        if (MoreEqu(temp,left)&& LessEqu(temp,right))  //日期合法
        {
            num ++;
            if (LessEqu(temp,oldest))  oldest = temp;  //更新oldest
            if (MoreEqu(temp,youngest)) youngest = temp; //更新youngest

        }

    }
    if (num == 0) printf("0\n");     //所有人日期都不合法輸出0
    else
    {
        printf("%d %s %s\n",num,oldest.name,youngest.name);
        return 0;
    }
}

world  cup betting

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets.

Chinese Football Lottery provided a "Triple Winning" game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results -- namely W for win, T for tie, and L for lose. There was an odd assigned to each result. The winner's odd would be the product of the three odds times 65%.

For example, 3 games' odds are given as the following:

 W    T    L
1.1  2.5  1.7
1.2  3.0  1.6
4.1  1.2  1.1

To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd game, and T for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1*3.0*2.5*65%-1)*2 = 37.98 yuans (accurate up to 2 decimal places).

Input

Each input file contains one test case. Each case contains the betting information of 3 games. Each game occupies a line with three distinct odds corresponding to W, T and L.

Output

For each test case, print in one line the best bet of each game, and the maximum profit accurate up to 2 decimal places. The characters and the number must be separated by one space.

Sample Input

1.1 2.5 1.7
1.2 3.0 1.6
4.1 1.2 1.1

Sample Output

T T W 37.98
#  include <cstdio>
char s[3]  ={'W','T','L'} ;   //s[0] = W...
int main()
{
    double ans = 1.0, tmp ,a;
    int idx;         //記錄每行最大數字的下標
    for (int i= 0;i < 3;i++)
    {
        tmp =0.0;
    for (int j = 0;j < 3;j++)
    {
        scanf("%lf",&a);
        if (a>tmp)
        {
            tmp = a;
            idx = j;
        }
    }
    ans *= tmp;          //按公式累乘
    printf("%c",s[idx]) ; //輸出對應的比賽結果
    }
    printf("%.2f",(ans*0.65-1)*2) ;  //輸出最大收益
    return 0;
}

Sign In and Sign OUT

At the beginning ofevery day, the first person who signs in the computer room will unlock thedoor, and the last one who signs out will lock the door. Given the records ofsigning in's and out's, you are supposed to find the ones who have unlocked andlocked the door on that day.

InputSpecification:

Each input filecontains one test case. Each case contains the records for one day. The casestarts with a positive integer M, which is the total number of records,followed by M lines, each in the format:

ID_numberSign_in_time Sign_out_time

where times aregiven in the format HH:MM:SS, and ID number is a string with no more than 15characters.

OutputSpecification:

For each test case,output in one line the ID numbers of the persons who have unlocked and lockedthe door on that day. The two ID numbers must be separated by one space.

Note: It isguaranteed that the records are consistent. That is, the sign in time must beearlier than the sign out time for each person, and there are no two personssign in or out at the same moment.

Sample Input:

3

CS30111115:30:28 17:00:10

SC302123408:00:00 11:25:25

CS30113321:45:00 21:58:40

Sample Output:

SC3021234CS301133

題目翻譯:

1006.簽到和簽出

在每天的開始,第一個在機房簽到的人會負責開門,最後一個簽出的人負責鎖門。提供簽到和簽出的記錄,你應該找出當天開門的那個人和鎖門的那個人。

 

輸入說明:

每個輸入文件包含一個測試實例。每個實例包含某一天的記錄。實例開頭是一個正整數M,代表記錄的個數。後面是M行,每行格式如下:

ID_number Sign_in_time Sign_out_time

其中時間的格式爲HH:MM:SS;ID number是一個字符串,不會超過15個字符。、

 

輸出說明:

對於每個測試實例,在一行中輸出當天開門的和鎖門的人的ID number。兩個ID number中間必須用一個空格隔開。

注意:記錄被保證是一致的。意思是指,每個人簽到的時間一定早於簽出的時間,而且不會有兩個人同一時間簽到或者簽出。
 

# include <iostream>
# include <cstdio>
#include <cstring>
using namespace std;
struct pNode
{
    char id[20];
    int hh,mm,ss;    //ans1存放最早簽到時間,ans2存放最晚簽到時間

}temp,ans1,ans2;
bool great (pNode node1,pNode node2) //node1的時間大於node2的時間就返回true

{
    if(node1.hh != node2.hh) return node1.hh > node2.hh;
    if(node1.mm !=node2.mm) return node1.mm > node2.mm;
    return node1.ss > node2.ss;
}
int main()
{
    int n;
    scanf("%d",&n);
    ans1.hh = 24,ans1.mm = 60,ans1.ss = 60;          //把初始簽到時間設置爲最大
    ans2.hh = 0,ans2.mm = 0,ans2.ss = 0;             //把初始簽到時間設置爲最小
    for (int i = 0;i < n;i ++)
    {
        //先讀入簽到時間
        scanf("%s %d:%d:%d",temp.id,&temp.hh,&temp.mm,&temp.ss);
        if(great(temp,ans1)==false)ans1 = temp;//ans1作爲更小的簽到時間,temp作爲籤離時間讀入
        scanf("%s %d:%d:%d",temp.id,&temp.hh,&temp.mm,&temp.ss);
        if(great(temp,ans1)==true )ans2 = temp;
    }
    printf("%s %s\n",ans1.id,ans2.id);
    return 0;
}

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student's name, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF-gradeM. If one such kind of student is missing, output "Absent" in the corresponding line, and output "NA" in the third line instead.

Sample Input 1:

3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

Sample Output 1:

Mary EE990830
Joe Math990112
6

Sample Input 2:

1
Jean M AA980920 60

Sample Output 2:

Absent
Jean AA980920
NA

# include <cstdio>
struct  person
{
    char name[15];    //姓名
    char id[15];      //ID
    int score;      //分數
}M ,F,temp  ;          //M爲男生最低分數信息,F爲女生最低分數的信息
void init()
{
    M.score = 101;     //初始化男生最低分的最大值爲101
    F.score = -1;      //初始化女生最高分的最小值爲-1;
}
int main()
{
    init(); //數據初始化
    int n;
    char gender;
    scanf("%d",&n);
    for (int i = 0;i <n; i++)
    {
        scanf("% s %c %s %c",temp.name,&gender,temp.id,&temp.score);
        if(gender == 'M'&& temp.score< M.score)
        {
            M =temp;    //男生且分數低於當前M 則更新M
        }
        else if(gender =='F'&&temp.score> F.score)
        {
            F =temp;    //女生且分數高當前F,則更新

        }

    }
    if(F.score == -1)
        printf("Absent\n");    //沒有女生
    else printf("%s %s\n",M.name,M.id);
    if(M.score == 101)
        printf("Absent\n");    //沒有男生
    else printf("%s %s\n",M.name,F.id);
    if(F.score == -1 || M.score ==101) printf("NA\n");    //沒有男生或女生
    else printf("%d\n",F.score - M.score);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章