--- title: 'HW5 Answer' disqus: hackmd --- HW5 參考解答 === [TOC] ## 課堂實習6(檔名 ex06-05.c) Description ``` 請寫一 C 語言程式 從命令列第一個參數指定的檔案讀取一串數字 印出共幾個數字及總和。 例如:檔案中的數字為 3 12 34 6 7 輸出為: Ans=5,62 輸入說明: 請在cmd輸入指令 ex06-05.exe 3 12 34 6 7 第一個ex06-05.exe不變 後面數字個數、數值會改變 輸出說明: 請根據輸入的ex06-05.exe後面的個數與數值 算出總共輸入多少數字與總和 ``` Sample Input ``` ex06-05.exe 3 12 34 6 7 ``` Sample Output ``` 5,62 ``` ```gherkin= #include<stdio.h> int main(int argc,char *argv[]){ int i,sum=0; for(i=1;i<argc;i++) sum += atoi(argv[i]); printf("%d,%d",argc-1,sum); return 0; } ``` ## File practice(改)(檔名:hw05-01.c) Description ``` 建立一個txt檔,複製以下文本,儲存檔名為score.txt。 在動物王國裡,以下文本是12隻動物在小考時考出的成績,發現成績有點糟糕 所以請幫忙建立一個C程式,讀取.txt檔中的每隻動物的成績,並以開根號乘以10來做調分,但是分數>=80分就不要調分 並在最後輸出調分後的最高分以及平均 開啟新的txt,把新的資料存入在new_score.txt中 ``` 文本 ``` 鼠 87 牛 35 虎 63 兔 97 龍 100 蛇 80 馬 31 羊 28 猴 55 雞 26 狗 12 豬 46 ``` Sample Input ``` (請用以上文本,讀取12隻動物的成績) ``` Sample Output ``` (new_score.txt的內文) 鼠 87.00 牛 59.16 虎 79.37 兔 97.00 龍 100.00 蛇 80.00 馬 55.68 羊 52.92 猴 74.16 雞 50.99 狗 34.64 豬 67.82 The max score 100.00 Average 69.895 ``` ```gherkin= #include <stdio.h> #include <math.h> int main(){ FILE *fp,*nfp; int i; float score,total=0,max=0; char num[3]; if((fp = fopen("score.txt","r"))==NULL) printf("Error!"); if((nfp = fopen("new_score.txt","w+"))==NULL) printf("Error!"); for(i=0;i<12;i++){ fscanf(fp,"%s",num); fscanf(fp,"%f",&score); fprintf(nfp,"%s ",num); if(score < 80) score = sqrt(score)*10; total += score; if(max < score) max = score; fprintf(nfp,"%.2f\n",score); } fprintf(nfp,"The max score %.2f\nAverage %.3f",max,total/12); fclose(fp); fclose(nfp); return 0; } ``` ## Up and down(改)(檔名:hw05-02.c) Description ``` 利用遞迴方式,輸入一個正整數n,印出從n到1,再從1印回n 輸入說明: 一個正整數n代表要印到第n個 輸出說明: 在數字前面印出Level,從n印到1,再從1印到n 每一行尾端要換行 ``` Sample Input ``` 4 ``` Sample Output ``` Level 4 Level 3 Level 2 Level 1 Level 1 Level 2 Level 3 Level 4 ``` ```gherkin= #include <stdio.h> void up_and_down(int top,int n){ printf("Level %d\n",n); if(n > 1) up_and_down(top,n-1); printf("Level %d\n",n); return; } int main(){ int N; scanf("%d",&N); up_and_down(N,N); return 0; } ``` ## 河內塔(Honoi)(改)(檔名:hw05-03.c) Description ``` Goal is that move all disks from pole 1 to pole 3 Rules: 1. Only one disk can be moved at a time. 2. Each move consists of taking the upper disk from one of the stacks and placingit on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 3. No disk may be placed on top of a smaller disk. 輸入說明: 只有一個正整數代表在一開始pole 1總共有n個disks 輸出說明: 印出解河內塔問題的所有步驟 ``` [Steps of solution](http://www.dynamicdrive.com/dynamicindex12/towerhanoi.htm) Sample Input ``` 3 ``` Sample Output ``` Move the #1 disk from tower 1 to tower 3. Move the #2 disk from tower 1 to tower 2. Move the #1 disk from tower 3 to tower 2. Move the #3 disk from tower 1 to tower 3. Move the #1 disk from tower 2 to tower 1. Move the #2 disk from tower 2 to tower 3. Move the #1 disk from tower 1 to tower 3. ``` ```gherkin= #include <stdio.h> void hanoi(int n,int src,int temp, int dest){ if(n==1) printf("Move the #%d dick from tower %d to tower %d.\n",n,src,dest); else{ hanoi(n-1,src,dest,temp); printf("Move the #%d dick from tower %d to tower %d.\n",n,src,dest); hanoi(n-1,temp,src,dest); } } int main(){ int n; scanf("%d",&n); hanoi(n,1,2,3); return 0; } ``` ## 必殺技(改)(檔名:hw05-04.c) Description ``` 殺而打傳說是一款有趣的動作型遊戲 除了用十字鍵來移動角色外 更可以按下A、B、C三鍵使出三種不同的攻擊。 還可以按下W、Y、Z三鍵使出三種不同的走位。 除此之外,如果已經使用過了3次(含)以上的A、2次(含)以上B和1次(含)以上C的攻擊之後,按下X鍵可以使出必殺技,給予敵人極大的傷害。 而發動一次必殺技之後,之前A、B、C三種攻擊的次數則要重新計算 第二種必殺技的指令為以經使用過了2次(含)以上的A、5次(含)以上的W、剛好3次的Y、1次(含)以上的Z,按下X鍵可以使出第二種必殺技。 而發動一次必殺技之後,W、Y、Z三種攻擊的次數重新計算、A的攻擊次數不變 如果還沒有達到發動必殺技的條件就按下X鍵 則前面A、B、C、W、Y、Z六種攻擊的次數不需要重新計算。 若達成兩種必殺技條件,先以第二種必殺技為首先發動 小恩是這個遊戲的初學者 因為不懂必殺技的發動規則,所以就是A、B、C、W、X、Y、Z七個鍵亂按一通 現在給你小恩輸入的動作,請你幫他看到底會發動幾次的必殺技。 輸入說明: 輸入資料是一個由A、B、C、W、X、Y、Z七個字母組成最大長度為2000的字串 代表小恩所輸入的動作。 輸出說明: 請輸出小恩這一連串的輸入能發動幾次必殺技。 兩種必殺技其中一個發動都算一次 ``` Sample intput ``` 測資1: AAABBCX 測資2: ABCXABCXABCXABCX 測資3: ABCWXYABCWXYYZABCWWWXYZ ``` Sample output ``` 測資1: 1 測資2: 1 測資3: 2 ``` ```gherkin= #include <stdio.h> #include <string.h> int main(){ int i; char bottom[2001]; int len; int A=0,B=0,C=0,W=0,Y=0,Z=0; int pounch=0; scanf("%s",bottom); len = strlen(bottom); for(i=0;i<len;i++){ if(bottom[i] == 'X'){ if(A >= 2 && W>=5 && Y==3 && Z>=1){ pounch++; W=0; Y=0; Z=0; } else if(A >= 3 && B >= 2 && C >= 1){ pounch ++; A = 0;B = 0;C = 0; } }else if(bottom[i] == 'A'){ A++; }else if(bottom[i] == 'B'){ B++; }else if(bottom[i] == 'C'){ C++; }else if(bottom[i] == 'W'){ W++; }else if(bottom[i] == 'Y'){ Y++; }else if(bottom[i] == 'Z'){ Z++; } } printf("%d\n",pounch); return 0; } ``` ### 實際測資 Input 1 ``` XXCZCCYWACWYWCZCYAAZBCYBWCWCWZACBACXYBBZWYBYYWCAZWCZCZZYZCAYBBYWZZCCWAXZCYBBAYACBWWCZABWBAACCAZWYYWZCCCWBXWZABYYCYABAAYZBWBYWWYBAWBBWZAYBWCZXAACCXCYZZZWWBZZBCBBWWZWWZABCAAYZCZXBCBZCXAYBWYAWBYCYWCCWYZZYYWWYZZBBZXWAZWBCYBYYYZAWWCWYZWYAAYAABAYAYACZXAXAYAYZCYZWBYYXZCYXWBYCCBXBWYCCZYZXCWCWZYCACAYWZYZWWACBWZBACZYACYZWZWXYAZBAACZZBWCYWWYBBCAYAYZWBWWZABWCBXAWBZCBBZWAAYBYAYZZCCCWCCZZCACYWBBYXCZZZAYWCWCXBYYYABCBWCBCBWWYACYWWYAXBCYZWBZZYZCZWAWCYZBZBYWWACCACYAZAAXWZCWCBZYAACZXAZCYBBYXCYBZWBWBZCWZAXZAYCZYZBY ``` Output 1 ``` 13 ``` --- Input 2 ``` XYCZZCXWAZZBAWAZWBCBCYWCXYZYBAZZZAWXYBCZWCWACWWAZZYYCZWZCAZBYBYWABXYZBXWCWZBZBZAWXAAABBBBCWYBCCAYWXZYAYYCXBYWWCYYCZAZACWBXBYBABBWZWYBZWBAABYXBCBZABWCAZZYCWYCXAACAZCZCACWWWZYWYXZYBZZCCBCZYWBYBAYYYCAWBCZABWCAACZBXZCAXCWCCYBBYZBYCBCZYAZAYBZBYWBCCXAXWABAWBXBABYYAZCWCBBBYBCYYYCWBZZWBYXZZBZYCBCZYZYABBACWBABWYCWYZZBCXZBZXYZCZWZZCWAWBYCCZZXWACYACBWYZBACBBYXBAWWZBACYBZAACWZAAZZCZCCACBWBYAWCCXWBXAWWCCYZWZBWCCCBBBZZCCCXXZAZWYWWXYAWBBWWZYYZWWZBYWZZBZCBXCACYWCBZC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