---
# System prepended metadata

title: Lab5 參考解答

---

# Lab5 參考解答

# Adventure of Forest
> Author: TA
```c=
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>

char name[1000000][31];

int compare(const void * a, const void * b){

    int flag = strcmp(a, b);

    if(flag > 0){
        return 1;
    }
    else if(flag < 0){
        return -1;
    }
    else{
        return 0;
    }

}

int main(){

    int n;
    int i, j;

    scanf("%d", &n);
    getchar();
    getchar();

    while(n--){

        char temp[31] = {0};
        int num = 0;
        bool flag = false;
        int count = 0;

        for(i = 0; i < 1000000; i++){
            memset(name[i], 0, 31);
        }

        for(i = 0; ;){
            gets(name[i]);
            if(name[i][0] == 0){
                break;
            }
            else{
                i++;
                num++;
            }
        }

        qsort (name, num, 31, compare);

        for(i = 0; i < num;){
            if(!flag){
                printf("%s ", name[i]);
                flag = true;
                count++;
            }
            else{
                if(i + 1 != num && strcmp(name[i], name[i + 1]) == 0){
                    count++;
                }
                else{
                    printf("%.4lf\n", ((double)count / (double)num) * 100);
                    flag = false;
                    count = 0;
                }
                i++;
            }
        }

        if(n){
            printf("\n");
        }
    }
    printf("\n");
    return 0;
}
```

# You are a hardworking bee
> Author: TA

```c=
#include<stdio.h>
int d[5001][2000] = { 0 };
int main()
{

 d[1][0] = 1;
 int i, j;
 for (i = 2; i <= 5000; i++)/*大數運算 運算方法如下,先算出5000項,設定每項最大2000個位數*/
 {
  for (j = 0; j < 2000; j++)
  {
   d[i][j] += d[i - 1][j] + d[i - 2][j];
   d[i][j + 1] += d[i][j] / 10;
   d[i][j] %= 10;
  }
 }

 int n;
 while (scanf("%d", &n) != EOF)
 {
  printf("The Fibonacci number for %d is ", n);

  for (i = 1999; i >= 0; i--) /*第n項的橫排陣列 從最後一個開始讀取*/
  {
   if (d[n][i] != 0)
    break;
  }
  if (i == -1)  /*當n=1時的輸出*/
   printf("0");
  else
   for (; i >= 0; i--)
    printf("%d", d[n][i]);  /*倒輸出整個橫排*/
  printf("\n");
 }
 return 0;
}
```