# 黎明高中資訊社第一次平時考 --- ## 目錄: > [TOC] ## 說明: 社長或副社長將從itsa或者zero judge挑選出4至5題的題目,基本上都不會選有出現在資訊社資源目錄的,難度隨題號遞增,在社團課抑或週六課程時將驗收,選擇自己會寫的先做,目前都是你們能夠解決的。 ## 完成者填寫說明: 若已答題且正確完畢,請寫上自己的姓名,可以自己新增更多名次(如第一題有到11名,可以繼續增加),有空社長將抽查答對者的想法與程式,一切採自由心證,當然我回去看報表核對。 ## 時間: 2020/04/09至2020/04/21 <10 p.m>(已截止) ## 獎勵: 作3題取6人。 ![](https://i.imgur.com/GXz0DAp.jpg) ## 社長的話: > [name=風思] > 可能有點難喔~ > [color=#db8462] ## 解答: 已釋出參考解答與社員提供的解答。 --- ### <font color="#39FF33">1.[C_ST09-易] 星號矩形輸出 </font> #### =>[題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=1283&fbclid=IwAR3zdU_whcoJb7CK5WAZtFSHlAgbi1aZvuThl1sCy3b-ExMYoIJT2fddphQ) #### 問題敘述: 試寫一個程式,可讓使用者輸入矩形的長寬,並於螢幕上輸出為星號 * 所組成的矩形。 ### 完成者: 0. <社長> 1. <郭浩雲> > [time=Fri, Apr 10, 2020 2:16 PM] > 俊翔出來打球,你當球 2. <柯昀杰> > [time=Sat, Apr 11, 2020 6:08 PM] 3. <副社長> > [time=Sat, Apr 11, 2020 6:31 PM] 4. <張嘉元> > [time=Sun, Apr 12, 2020 8:19 AM] 5. <李晨維> > [time=Sun, Apr 12, 2020 6:06 PM] 6. <吳冠毅> > [time=Tue, Apr 14, 2020 11:00 AM] 7. <吳品範> > [time=Tue, Apr 14, 2020 6:54 PM] 8. <胡韡薰> > [time=Sat, Apr 18, 2020 12:52 PM] 9. <何承煜> > [time=Sat, Apr 18, 2020 12:52 PM] 10. <賴晨興> > [time=Sat, Apr 18, 2020 1:04 PM] 11. <王昱翔> > [time=Sat, Apr 18, 2020 1:20 PM] 12. <陳宜俊> > [time=Sat, Apr 18, 2020 1:35 PM] 13. <張家寶> > [time=Sat, Apr 18, 2020 4:49 PM] 14. <趙敬軒> > [time=Wed, Apr 22, 2020 11:42 AM] #### 社長提供的測資: <font color="#f00">這應該不用吧XD</font> ### 參考解答(社長提供): #### Java: ```java= import java.util.*; //[C_ST09-易] 星號矩形輸出 //2019,07,16;22:04 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int Length = sc.nextInt(); int Width = sc.nextInt(); for (int i = 0; i < Width; i++) { for (int j = 0; j < Length; j++) { System.out.print("*"); } System.out.println(); } } sc.close(); } } ``` --- ### <font color="#E3FF33">2.[C_MM100-易] 計程車收費標準 </font> #### =>[題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=1273) #### 問題敘述: 計程車有基本的起跳價格 S ,計程車每開 1 公里加收 K 元,不滿 1 公里以 1 公里計算,超過 M 公里時,每超過 1 公里加收 (K+5) 元,則搭乘計程車行進了 D 公里的總費用則為 T 。 #### 社長提供的測資: 較大的測資。 ``` input: 1. 8000 20 500 2000 2. 46564 345 3564 245343 ``` ``` output: 1. 55500 2. 85898794 ``` ### 完成者: 0. <社長> 1. <柯昀杰> > [time=Thu, Apr 9, 2020 9:15 PM] 2. <吳品範> > [time=Thu, Apr 9, 2020 9:27 PM] 3. <郭浩雲> > [time=Fri, Apr 10, 2020 2:46 PM] 4. <副社長> > [time=Sat, Apr 11, 2020 6:31 PM] 5. <李晨維> > [time=Sun, Apr 12, 2020 6:18 PM] 6. <張嘉元> > [time=Wed, Apr 15, 2020 10:55 PM] 7. <吳冠毅> > [time=Sat, Apr 18, 2020 1:00 PM] 8. <何承煜> > [time=Sat, Apr 18, 2020 1:14 PM] 9. <賴晨興> > [time=Sat, Apr 18, 2020 1:16 PM] 10. <胡韡薰> > [time=Sat, Apr 18, 2020 1:19 PM] 11. <王昱翔> > [time=Sat, Apr 18, 2020 1:36 PM] 12. <陳宜俊> > [time=Sat, Apr 18, 2020 2:11 PM] ### 參考解答(社長提供): #### C++: ```cpp= #include <iostream> #include <cmath> using namespace std; int main(){ int S,K,M,T; double D; while(cin >> S >> K >> M >> D){ D=ceil(D); if(D>M) T=S+(M*K)+(D-M)*(K+5); else T=S+D*K; cout << T << endl; } return 0; } ``` --- ### <font color="#FF7433">3.[C_MM057-易] 老鼠問題</font> #### => [題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=1890) #### 問題敘述: 房間裡一開始有n隻老鼠,每個月內一對老鼠能生育3隻老鼠,且每隻老鼠經過三個月即會死亡( 活了三個月的老鼠會在生完老鼠之後才死掉 ).請撰寫一個程式計算經過m個月後房間內老鼠的數量. ### 完成者: 0. <社長> 1. <柯昀杰> > [time=Sun, Apr 12, 2020 2:54 PM] 2. <郭浩雲> > [time=Tue, Apr 14, 2020 12:40 AM] 3. <李晨維>題目哪裡提及了要重複輸入... > [time=Thu, Apr 16, 2020 2:15 PM] 4. <張嘉元> > [time=Thu, Apr 16, 2020 2:32 PM] 5. <吳冠毅> > [time=Sat, Apr 18, 2020 2:06 PM] 6. <李弘唯>我就爛 ![](https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTqJ6ELbBh5R2KMQuDZSo7ofPfTxBlfBONsVRMZXRFW7Z17J2Zu&usqp=CAU) > [time=Tue, Apr 21, 2020 10:26 PM] #### 社長提供的測資: 可以數字再大一點,再大一點點,再大一點點.... ``` input: 87 8 78 7 ``` ``` output: 87 8 98817 78 7 37212 ``` ### 參考解答(社長提供): #### C++: ```cpp= #include <iostream> #include <string.h> #define MAX 41 using namespace std; long months[MAX]; long birthnum[MAX]; int main(){ int n,m; while(cin >> n >> m){ memset(months,0,sizeof(months)); memset(birthnum,0,sizeof(birthnum)); months[0]=birthnum[0]=n; for (int i = 1; i <= m; i++){ if (i >= 3){ birthnum[i] = (months[i - 1] / 2) * 3; months[i] = months[i - 1] + birthnum[i] - birthnum[i - 3]; } else{ birthnum[i] = (months[i - 1] / 2) * 3; months[i] = months[i - 1] + birthnum[i]; } } cout << n << " " << m << " " << months[m] << endl; } return 0; } ``` #### Java: ```java= import java.util.Arrays; import java.util.Scanner; //[C_MM057-易] 老鼠問題 //2020,03,17;10:24 public class Main { final static int MAX = 41; static long[] months = new long[MAX]; static long[] birthnum = new long[MAX]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { Arrays.fill(months, 0); Arrays.fill(birthnum, 0); int n = sc.nextInt(); int m = sc.nextInt(); months[0] = birthnum[0] = n; for (int i = 1; i <= m; i++) { if (i >= 3) { birthnum[i] = (months[i - 1] / 2) * 3; months[i] = months[i - 1] + birthnum[i] - birthnum[i - 3]; } else { birthnum[i] = (months[i - 1] / 2) * 3; months[i] = months[i - 1] + birthnum[i]; } } System.out.println(n + " " + m + " " + months[m]); } } } ``` ### 參考解答(社員提供): #### C++: ##### 柯昀杰 提供 ```cpp= #include <iostream> using namespace std; int main(){ int m,n,i; long long a[40]; while(cin>>n>>m){ a[0]=n; for(i=1;i<=m;i++){ if(i==3){ a[i]=a[(i-1)]/2*3+a[i-1]-a[(i-3)]; }else if(i>=4){ a[i]=a[(i-1)]/2*3+a[i-1]-a[(i-4)]/2*3; }else{ a[i]=a[(i-1)]/2*3+a[(i-1)]; } } cout<<a[0]<<" "<<m<<" "<<a[m]<<endl; } return 0; } ``` ##### 李晨維 提供 ```cpp= #include<iostream> using namespace std; int main() { long int n,m,moli[5]; while(cin>>n>>m){ for(int y=0;y<=4;y++){ moli[y]=0; } moli[1]=n; int n0=n; for(int i=1;i<=m;i++) { for(int x=4;x>=2;x--) { moli[x]=moli[x-1]; } if(n%2==0) { moli[1]=n/2*3; n=n+(n/2*3); }else if(n%2==1){ moli[1]=(n-1)/2*3; n=n+(n-1)/2*3; } n-=moli[4]; } cout<<n0<<" "<<m<<" "<<n<<endl; } return 0; } ``` ##### 張嘉元 提供 ```cpp= #include <iostream> using namespace std; int main(){ long long a[40],b; cin>>a[0]>>b; for(int c=1;c<=b;c++){ if(c<3){ a[c]=(a[c-1]/2)*3+a[c-1]; }else if(c==3){ a[c]=(a[c-1]/2)*3+a[c-1]-a[0]; }else if(c>=4){ a[c]=(a[c-1]/2)*3+a[c-1]-(a[c-4]/2)*3; } } cout<<a[0]<<" "<<b<<" "<<a[b]<<endl; return 0; } ``` --- ### <font color="FF5233">4.[C_MM86-易] 公因數問題</font> #### => [題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2298) #### 問題敘述: 二個或更多整正數之相同因數稱為公因數 。例如:有兩個正整數分別是 4 與 6 , 2 為其公因數因為 4= 2x2 ; 6=2x3 。請撰寫一個程式用來判斷輸入 n 個正整數之所有公因數 ,將它們列印在螢幕上。 ### 完成者: 0. <社長> 1. <李晨維>學弟都不睡覺@@ > [time=Mon, Apr 13, 2020 1:14 AM] 2. <郭浩雲> > [time=Mon, Apr 13, 2020 8:55 PM] 3. <張嘉元> > [time=Sat, Apr 18, 2020 2:53 PM] 4. <吳冠毅> > [time=Sat, Apr 18, 2020 3:08 PM] 5. <柯昀杰> > [time=Tue, Apr 21, 2020 2:23 PM] #### 社長提供的測資: 抱歉,社長的測資比較暴力。 ``` input: 2911 3977 3854 1476 ^Z ``` ``` output: Common factor in ascending order: 41 ``` ### 參考解答(社長提供): #### C++: ```cpp= #include <bits/stdc++.h> #define MAX 100 using namespace std; int nums[MAX]; int main(){ int count=0; bool isCommon=true; while(cin >> nums[count]) count++; sort(nums,nums+count); cout << "Common factor in ascending order:"; for(int i=2;i<=nums[0];i++){ isCommon=true; for(int j=0;j<count;j++){ if(nums[j]%i!=0 or nums[j]<i){ isCommon=false; break; } } if(isCommon) cout << " " << i; } cout << endl; return 0; } ``` #### Java: ```java= import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean isCommon = true; String input[] = sc.nextLine().split(" "); int[] nums = new int[input.length]; for (int i = 0; i < nums.length; i++) nums[i] = Integer.parseInt(input[i]); Arrays.sort(nums); System.out.print("Common factor in ascending order:"); for (int i = 2; i <= nums[0]; i++) { isCommon = true; for (int j = 0; j < nums.length; j++) { if (nums[j] % i != 0 || nums[j] < i) { isCommon = false; break; } } if (isCommon) System.out.print(" "+i); } System.out.println(); } } ``` ### 參考解答(社員提供): #### C++: ##### 李晨維 提供 ```cpp= #include<iostream> using namespace std; int GCD(int x, int y) { if { while((x %= y) && (y %= x)); return x + y; } int main() { int a[100],z=1,temp=0; while(cin>>a[z]){ z++; } z--; temp=a[1]; for(int i=1;i<=z-1;i++) { temp=GCD(temp, a[i+1]); } cout<<"Common factor in ascending order:"; for(int n=2;n<=temp;n++) { if(temp%n==0) cout<<" "<<n; } cout<<endl; return 0; } ``` ##### 張嘉元 提供 ```cpp= #include <iostream> using namespace std; int main(){ int a,b; cin>>a; while(cin>>b){ while(a!=0&&b!=0){ if(a>b){ a=a%b; }else{ b=b%a; } } if(a>b){ a=a; }else{ a=b; } } cout<<"Common factor in ascending order:"; for(int e=2;e<=a;e++){ if(a%e==0){ cout<<" "<<e; } } cout<<endl; return 0; } ``` ##### 柯昀杰 提供 ```cpp= #include <iostream> using namespace std; int gcd(int x,int y) { if(x%y==0) return y; else return gcd(y,x%y); } int main() { int x,y,i,result; cin>>x; while(cin>>y){ result=gcd(x,y); x=result; } cout<<"Common factor in ascending order:"; for(i=2;i<=x;i++){ if(x%i==0){ cout<<" "<<i; } } cout<<endl; return 0; } ``` --- ### <font color="#f00">5.[C_SL41-易] 老鼠點數</font> #### => [題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=36069) 然後這題很難喔。 #### 問題敘述: 傳說中,在台灣東部有一座老鼠村。裡面有一隻<font color="#f00">老鼠王(大頭#)</font>,老鼠王喜歡數數,並且將數過的數記錄下來。老鼠王數數的方式是:一排數字,依序從0到9,數數列中各別的數字出現了幾次,如果數字沒有出現,就不紀錄。例如1111,老鼠王會記錄成41,依此類推。如果數的次數大於1次,則會將上一次數數的結果作為數列,再數一次。每筆測資會給你兩個數字,第一個為原本的數列,第二個為老鼠王數數的次數。例如輸入1111 2,數一次變成41,數兩次變成1114,然後輸出最後數數的結果。 <font color="#f52">延伸閱讀:變動長度編碼法 Run-length Encoding, RLE </font> ### 完成者: 0. <社長> 1. <副社長> > [time=Thu, Apr 9, 2020 10:56 PM] 2. <郭浩雲> > [time=Fri, Apr 17, 2020 12:48 AM] > 爽啦,這次沒有被輸入方式陰 3. <吳冠毅> > [time=Tue, Apr 21, 2020 8:56 PM] > 還好寫出來了 4. <張嘉元> > [time=Wed, Apr 22, 2020 6:11 PM] #### 社長提供的測資: <font color="#f00">抱歉,社長的測資很暴力。</font> ``` input: 3 843271459 7 343531243535464635357224 12 12345678765426786423567864346576634253467 10 ``` ``` output: 712223141516271819 41421314251617 107122132415261718 ``` ### 參考解答(社團幹部群提供): #### C++: ##### 社長(曾俊翔) 提供 ```cpp= #include <bits/stdc++.h> using namespace std; int main(){ int n,times; int count[10]; string str; stringstream s(""); cin >> n; while(n-->0){ str=""; cin >> str >> times; while(times-->0){ memset(count,0,sizeof(count)); s.clear(); for(int i=0;i<str.length();i++) count[str.at(i)-'0']++; for(int i=0;i<10;i++){ if(count[i]) s << count[i] << i; } str=""; s >> str; } cout << str << endl; } return 0; } ``` ##### 前社長(郭浩雲) 提供 ```cpp= #include <iostream> #include <sstream> #include <string> using namespace std; int main(){ int n, time, chart[10]={0}, i; //n:有幾筆測資、time:數幾次、chart:記錄各數字出現次數 string str, output, temp; stringstream s1; cin >> n; for(;n>0;n--){ cin >> str >> time; for(;time>0;time--){ for(i=0;i<str.size();i++){ //統計字元出現次數 chart[str[i] - '0']++; } for(i=0;i<10;i++){ if(chart[i]>0){ s1 << chart[i]; //數字轉字元 s1 >> output; temp += output + char('0'+i); s1.str(""); s1.clear(); //清除並初始化s1 } chart[i] = 0; //歸零統計表 } str = temp; temp.clear(); //清除temp內容 } cout << str << endl; } return 0; } ``` #### Java: ##### 副社長(李弘唯) 提供 ```java= import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int loopTime = sc.nextInt(); for(int i = 0 ; i < loopTime ; i ++ ){ String st = sc.next(); int countTime = sc.nextInt(); int[] numberCount = new int[10]; for(int count = 0 ; count < countTime ; count ++ ) { char[] chArrays = st.toCharArray(); for (int j = 0; j < chArrays.length; j++) { numberCount[(int)chArrays[j]-(int)'0'] ++ ; } StringBuffer stringBuffer = new StringBuffer(20); for(int j = 0 ; j < 10 ; j ++ ){ if(numberCount[j] != 0) { stringBuffer.append(numberCount[j]); stringBuffer.append(j); } } numberCount = new int[10]; st = stringBuffer.toString(); } System.out.println(st); } } } ``` ### 參考解答(社員提供): #### Java: ##### 吳冠毅 提供 ```java= import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); for(int k=0;k<n;k++){ String str=sc.next(); int time=sc.nextInt(); for(int l=0;l<time;l++){ int[] arr=new int [10]; for(int i=0;i<str.length();i++){ char result=str.charAt(i); for(int j=48;j<58;j++){ if((int)result==j) arr[j-48]=arr[j-48]+1; } } str=""; for(int i=0;i<10;i++){ if(arr[i]!=0){ String head=Integer.toString(arr[i]); String tail=Integer.toString(i); str=str+head+tail; } } } System.out.println(str); } sc.close(); } } ``` #### C++: ##### 張嘉元 提供 ```cpp= #include <iostream> using namespace std; int main(){ char c; int d,g; cin>>g; for(int h=0;h<g;h++){ int a[10]={0,0,0,0,0,0,0,0,0,0},b[10]={0,0,0,0,0,0,0,0,0,0}; while((c=getchar())!=' '){ if(c=='1'){ a[1]++; }else if(c=='2'){ a[2]++; }else if(c=='3'){ a[3]++; }else if(c=='4'){ a[4]++; }else if(c=='5'){ a[5]++; }else if(c=='6'){ a[6]++; }else if(c=='7'){ a[7]++; }else if(c=='8'){ a[8]++; }else if(c=='9'){ a[9]++; }else if(c=='0'){ a[0]++; } } cin>>d; for(int e=0;e<d;e++){ if(e==0){ for(int f=0;f<10;f++){ if(a[f]!=0&&d==1){ cout<<a[f]<<f; } } }else if(e%2==0){ for(int f=0;f<10;f++){ a[f]=0; } for(int f=0;f<10;f++){ if(b[f]!=0){ a[f]++; } if(b[f]==1){ a[1]++; }else if(b[f]==2){ a[2]++; }else if(b[f]==3){ a[3]++; }else if(b[f]==4){ a[4]++; }else if(b[f]==5){ a[5]++; }else if(b[f]==6){ a[6]++; }else if(b[f]==7){ a[7]++; }else if(b[f]==8){ a[8]++; }else if(b[f]==9){ a[9]++; }else if(b[f]>=10){ if(b[f]/10==1){ a[1]++; }else if(b[f]/10==2){ a[2]++; }else if(b[f]/10==3){ a[3]++; }else if(b[f]/10==4){ a[4]++; }else if(b[f]/10==5){ a[5]++; }else if(b[f]/10==6){ a[6]++; }else if(b[f]/10==7){ a[7]++; }else if(b[f]/10==8){ a[8]++; }else if(b[f]/10==9){ a[9]++; } if(b[f]%10==1){ a[1]++; }else if(b[f]%10==2){ a[2]++; }else if(b[f]%10==3){ a[3]++; }else if(b[f]%10==4){ a[4]++; }else if(b[f]%10==5){ a[5]++; }else if(b[f]%10==6){ a[6]++; }else if(b[f]%10==7){ a[7]++; }else if(b[f]%10==8){ a[8]++; }else if(b[f]%10==9){ a[9]++; }else if(b[f]%10==0){ a[0]++; } } } }else if(e%2!=0){ for(int f=0;f<10;f++){ b[f]=0; } for(int f=0;f<10;f++){ if(a[f]!=0){ b[f]++; } if(a[f]==1){ b[1]++; }else if(a[f]==2){ b[2]++; }else if(a[f]==3){ b[3]++; }else if(a[f]==4){ b[4]++; }else if(a[f]==5){ b[5]++; }else if(a[f]==6){ b[6]++; }else if(a[f]==7){ b[7]++; }else if(a[f]==8){ b[8]++; }else if(a[f]==9){ b[9]++; }else if(a[f]>=10){ if(a[f]/10==1){ b[1]++; }else if(a[f]/10==2){ b[2]++; }else if(a[f]/10==3){ b[3]++; }else if(a[f]/10==4){ b[4]++; }else if(a[f]/10==5){ b[5]++; }else if(a[f]/10==6){ b[6]++; }else if(a[f]/10==7){ b[7]++; }else if(a[f]/10==8){ b[8]++; }else if(a[f]/10==9){ b[9]++; } if(a[f]%10==1){ b[1]++; }else if(a[f]%10==2){ b[2]++; }else if(a[f]%10==3){ b[3]++; }else if(a[f]%10==4){ b[4]++; }else if(a[f]%10==5){ b[5]++; }else if(a[f]%10==6){ b[6]++; }else if(a[f]%10==7){ b[7]++; }else if(a[f]%10==8){ b[8]++; }else if(a[f]%10==9){ b[9]++; }else if(a[f]%10==0){ b[0]++; } } } } } if(d%2==0){ for(int f=0;f<10;f++){ if(b[f]!=0){ cout<<b[f]<<f; } } }else if(d%2!=0&&d!=1){ for(int f=0;f<10;f++){ if(a[f]!=0){ cout<<a[f]<<f; } } } cout<<endl; } return 0; } ``` --- ## <font color="#335EFF">統計表<font color="#335EFF"> | 第一題 | 第二題 | 第三題 | 第四題 | *第五題* | 全對者 | |:------:|:------:|:------:|:------:|:--------:|:------:| | 15 | 13 | 7 | 6 | 5 | 4 | --- > [time=Sun, Apr 26, 2020 7:03 PM] ###### tags: `社內考試`