# 黎明高中資訊社第二次平時考 --- ## 目錄: > [TOC] ## 說明: 社長或副社長將從itsa或者zero judge挑選出4至5題的題目,基本上都不會選有出現在資訊社資源目錄的,難度隨題號遞增,在社團課抑或週六課程時將驗收,選擇自己會寫的先做,目前都是你們能夠解決的。 ## 完成者填寫說明: 若已答題且正確完畢,請寫上自己的姓名,可以自己新增更多名次(如第一題有到11名,可以繼續增加),有空社長將抽查答對者的想法與程式,一切採自由心證,當然我回去看報表核對。 ## 時間: 2020/04/29至2020/05/14 <10 p.m>(已截止) ## 獎勵: 點券或飲料 六人 > [name=風思] 俊翔一隻 > [name=前社長] ## 社長的話: 各位表現還不錯呀。 > [name=風思] > [color=#f55f] ## 解答: 已釋出。 --- ### 1.<font color="#39FF33">[C_ST14-易] 數字直角三角形輸出</font> #### =>[題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=676) #### 問題敘述: 撰寫一個程式,給你一個數字,請你參考範例輸入輸出的形式顯示圖形樣式。 ### 完成者: 0. <社長> > [time=Wed, Apr 29, 2020 10:07 PM] 1. <李晨維> > [time=Thu, Apr 30, 2020 1:59 PM]又被重複輸入陰... 2. <張嘉元> > [time=Thu, Apr 30, 2020 2:23 PM] 3. <郭浩雲> > [time=Fri, May 1, 2020 3:31 PM] 4. <柯昀杰> > [time=Sat, May 2, 2020 1:01 PM] 5. <賴晨興> > [time=Sat, May 2, 2020 1:16 PM] 6. <吳冠毅> > [time=Sat, May 2, 2020 1:33 PM] 7. <林承鋒> > [time=Sat, May 2, 2020 1:49 PM] 8. <胡韡薰> > [time=Sat, May 2, 2020 2:33 PM] 9. <吳品範> > [time=Sat, May 2, 2020 2:35 PM] 10. <陳宜俊> > [time=Sat, May 2, 2020 3:46 PM] 11. <王昱翔> > [time=Tue, May 5, 2020 10:31 AM] 12. <何承煜> > [time=Wed, May 6, 2020 11:20 AM] #### 社長提供的測資: 無。 ### 參考解答(社長提供): #### Java: ```java= import java.util.*; //[C_ST14-易] 數字直角三角形輸出 //2019,08,07;10:46 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 1; i <= n; i++) { for (int j = n - i; j > 0; j--) System.out.print(" "); if (i % 2 != 0) { for (int k = 1; k <= i; k++) System.out.print(k); } else { for (int l = i; l > 0; l--) { System.out.print(l); } } System.out.println(); } sc.close(); } } ``` ### 參考解答(社員提供): #### C: #### 胡韡薰<高二> 提供 ```cpp= #include<stdio.h> int main(){ int n,i,j,k,l,m; while(scanf("%d",&n)!=EOF){ for(i=1;i<=n;i++){ if(i%2==1){ for(l=n;l>=i+1;l--){ printf(" "); } for(j=1;j<=i;j++){ printf("%d",j); } printf("\n"); } else{ for(m=i;m<=n-1;m++){ printf(" "); } for(k=i;k>=1;k--){ printf("%d",k); } printf("\n"); } } } } ``` --- ### 2.<font color="#E3FF33">[C_ST57-易] 迴文</font> #### =>[題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=8122) #### 問題敘述: 請輸入字串並判斷其是否迴文,例如: aba 或 12321 皆為迴文, ahaa 或 56421 則為非迴文。 ### 完成者: 0. <社長> > [time=Wed, Apr 29, 2020 10:07 PM] 1. <李晨維> > [time=Thu, Apr 30, 2020 2:39 PM] 2. <郭浩雲> > [time=Fri, May 1, 2020 3:47 PM] 3. <吳品範> > [time=Sat, May 2, 2020 2:00 PM] 4. <吳冠毅> > [time=Sat, May 2, 2020 2:00 PM] 5. <張嘉元> > [time=Sat, May 2, 2020 2:32 PM] 6. <陳宜俊> > [time=Sat, May 2, 2020 3:12 PM] 7. <胡韡薰> > [time=Sat, May 2, 2020 3:31 PM] 8. <柯昀杰> > [time=Tue, May 5, 2020 2:34 PM] #### 社長提供的測資: 無。 ### 參考解答(社長提供): #### C: ```cpp= #include <stdio.h> #include <string.h> #define MAX 1000 char str[MAX]; int isPalindrome(char str[]){ int len=strlen(str); char *front=str; char *rear=str+len-1; int flag=1; for(int i=0; i<len/2; i++){ if(*front==*rear){ front++; rear--; } else{ flag=0; break; } } return flag; } int main() { while(scanf("%s",str)!=EOF){ if(isPalindrome(str)) printf("yes\n"); else printf("no\n"); } return 0; } ``` #### C++: ```cpp= #include <bits/stdc++.h> using namespace std; int main(){ string restr,str; while(cin >> restr){ str=restr; reverse(restr.begin(),restr.end()); if(str==restr) cout << "yes" << endl; else cout << "no" << endl; } return 0; } ``` #### Java: ```java= import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str, restr; StringBuffer strBuf = new StringBuffer(""); while (sc.hasNext()) { strBuf.delete(0, strBuf.capacity()); str = sc.nextLine(); strBuf.append(str); restr = strBuf.reverse().toString(); if (str.equals(restr)) System.out.println("yes"); else System.out.println("no"); } } } ``` ### 參考解答(社員提供): #### C: #### 胡韡薰<高二> 提供 ```cpp= #include <stdio.h> #include <string.h> int main(void) { int n,i,flag=0; char str[1000]; scanf("%s",str); int length = strlen(str); for(i=0;i<length/2;i++){ if(str[i]==str[length-i-1]) flag=0; else{ flag=1; break; } } if(flag==0){ printf("yes\n"); } else printf("no\n"); } ``` --- ### 3.<font color="#FF7433">[C_MM87-易] 最小公倍數問題</font> #### =>[題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2302) #### 問題敘述: 若一個整數同時為幾個整數的倍數時,我們稱這個數為這 幾個 數的 公倍數 。例如:有四個正整數分別是 2, 3, 4, 6 , 12 為所有公倍數其中之一 。其中 12 為最小值又稱為最小公倍數。請撰寫一個程式用來判斷輸入 n 個正整數之最小公倍數 ,將它們列印在螢幕上。 ### 完成者: 0. <社長> > [time=Wed, Apr 29, 2020 10:08 PM] 1. <李晨維> > [time=Sat, May 2, 2020 1:14 PM] 2. <張嘉元> > [time=Sat, May 2, 2020 1:32 PM] 3. <吳冠毅> > [time=Tue, May 5, 2020 10:29 AM] 4. <吳品範> > [time=Fri, May 8, 2020 8:33 PM] 5. <郭浩雲> > [time=Thu, May 14, 2020 3:35 PM] > 法科,又被 E-tutor 的輸入陰 #### 社長提供的測資: ``` input: 876 543 45464 ^Z ``` ``` output: 1802147496 ``` ### 參考解答(社團幹部群提供): #### C++: #### 社長(曾俊翔)提供 ```cpp= #include <bits/stdc++.h> #define MAX 100 using namespace std; int nums[MAX]; int main(){ int count=0,k=1,result; bool hasFound=true; while(cin >> nums[count]) count++; sort(nums,nums+count); while(true){ result=nums[0]*k++; hasFound=true; for(int i=1;i<count;i++){ if(result%nums[i]){ hasFound=false; break; } } if(hasFound) break; } cout << "Lowest common multiple: " <<result << endl; return 0; } ``` #### 前社長(郭浩雲)提供 ```cpp= //Least Common Multiple (LCM) #include <iostream> #include <algorithm> using namespace std; int main() { int N=0,i=0,j=0,factor=1; //factor因數 unsigned long long multiply=1; bool coprime = false; //coprime互質 int num[20]={0}; while(cin >> num[N]){ //本題試過以輸入string,再依ASCII碼轉存int陣列方式,惟E-tutor的輸入方式無法通過 N++; //輸入結束時要發送 EOF 訊號(Unix、Linux系統下按Enter再按Ctrl+D) } while(!coprime){ //想法來自短除法,另一想法也不錯:LCM必為最小數之倍數,同時亦可被所有數整除 sort(num,num+N+1); for(i=0;i<=N;i++){ if(factor == 1){ for(j=2;j<=num[i];j++){ if(num[i]%j == 0){ factor = j; break; } } } if(num[i]%factor == 0) num[i] /= factor; } if(factor == 1) coprime = true; multiply *= factor, factor=1; } cout << "Lowest common multiple: " << multiply << '\n'; return 0; } ``` ### 參考解答(社員提供): #### C: #### 吳品範<高二> 提供 ```cpp= #include <stdio.h> int main() { int n,a[1000],k=0,i,j,c,d,e,flag=0; while(scanf("%d",&n)!=EOF) { a[k++]=n; } int b[k]; for(i=0;i<k;i++) { b[i]=a[i]; } for(i=0;i<k;i++) { for(j=i+1;j<k;j++) { if(b[i]>b[j]) { c=b[i]; b[i]=b[j]; b[j]=c; } } } for(d=1;d>0;d++) { for(e=1;e<k;e++) { if(b[0]*d%b[e]==0) { flag=1; } else { flag=0; break; } } if(flag==1) { printf("Lowest common multiple: %d\n",b[0]*d); break; } } return 0; } ``` #### C++: #### 李晨維<高一> 提供: ```cpp= #include<bits/stdc++.h> using namespace std; int gcd(int a, int b){ if ( b) while((a %= b) && (b %= a)); return a + b; } int main() { int a[100],temp=0,n=1; while(cin>>a[n]){ n++; }n--; temp=gcd(a[1],a[2]); for(int i=2;i<=n;i++) { temp=gcd(temp,a[i]); } for(int m=1;m<=n;m++) { if(temp%a[m]==0){ continue; } else{ temp=temp*(a[m]/(gcd(temp,a[m]))); } } cout<<"Lowest common multiple: "<<temp<<endl; return 0; } ``` #### 張嘉元<高一> 提供 ```cpp= #include <iostream> using namespace std; int lcm(int a ,int b){ int x=a,y=b; while(a!=0&&b!=0){ if(a>b){ a=a%b; }else{ b=b%a; } } if(a>0){ return (x*y)/a; }else{ return (x*y)/b; } } int main(){ int a,b; cin>>a; while(cin>>b){ a=lcm(a,b); } cout<<"Lowest common multiple: "<<a<<endl; return 0; } ``` --- ### 4.<font color="FF5233">[C_MM120-中] 執行質因數分解</font> #### =>[題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=1442) #### 問題敘述: 執行質因數分解,寫一個程式執行質因數分解。 ### 完成者: 0. <社長> > [time=Wed, Apr 29, 2020 10:08 PM] 1. <張嘉元> > [time=Sat, May 2, 2020 1:09 PM] > 被1陰了 2. <李晨維> > [time=Sat, May 2, 2020 1:32 PM] 3. <柯昀杰> > [time=Sun, May 3, 2020 8:37 PM] 4. <吳冠毅> > 該死的重複輸入 > [time=Wed, May 6, 2020 11:02 AM] 5. <吳品範> > [time=Mon, May 11, 2020 6:29 PM] #### 社長提供的測資: ``` input: 123456789 ``` ``` output: 123456789=3*3*3607*3803 ``` ### 參考解答(社團幹部群提供): #### C++: #### 社長(曾俊翔)提供 ```cpp= #include <bits/stdc++.h> using namespace std; int main(){ int n,count; bool isFirOut; while(cin >> n){ isFirOut=false; if(n!=1) cout << n << "="; else{ cout << 1 << "=" << 1 << endl; continue; } for(int i=2; (i<=n and n!=1); i++){ count=0; while((n%i)==0){ n/=i; count++; } if(count>0){ for(int j=0; j<count; j++){ if(j==0 and isFirOut==false) cout << i; else cout << "*" << i; } isFirOut=true; } } cout << endl; } return 0; } ``` ### 參考解答(社員提供): #### C: #### 吳品範<高二> 提供 ```cpp= #include <stdio.h> int main(void) { int n,i,j,k; while(scanf("%d",&n)!=EOF) { if(n==1) { printf("1=1\n"); } else { for(i=2;i>0;i++) { if(n%i==0) { printf("%d=",n); printf("%d",i); n=n/i; break; } } for(j=2;j<=n;j++) { while(1) { if(n%j==0) { printf("*%d",j); n=n/j; } else { break; } if(n==1) { break; } } if(n==1) { break; } } printf("\n"); } } } ``` #### C++: #### 張嘉元<高一> 提供 ```cpp= #include <iostream> using namespace std; int main(){ int a; while(cin>>a){ cout<<a<<"="; if(a==1){ cout<<a<<endl; }else{ for(int b=2;b<=a;b++){ while(a%b==0&&a!=1){ if(a==b){ cout<<b<<endl; a=a/b; }else{ cout<<b<<"*"; a=a/b; } } } } } return 0; } ``` #### 李晨維<高一> 提供 ```cpp= #include<bits/stdc++.h> using namespace std; int main() { long long a,fn=0; while(cin>>a) { fn=0; if(a==1){ cout<<"1=1"<<endl; continue; } while(a>1) { cout<<a<<"="; for(int i=2;i<=a;i++) { while(a%i==0){ if(fn){ cout<<"*"<<i; }else{ cout<<i; } fn++; a/=i; } } } cout<<endl; } return 0; } ``` #### 柯昀杰<高一> 提供 ```cpp= #include <iostream> using namespace std; int main(){ int n,i; while(cin>>n){ cout<<n<<"="; if(n==1){ cout<<n; }else{ for(i=2;n/i>=1;i++){ if(n%i==0){ cout<<i; n=n/i; while(n/i>=1){ cout<<"*"; if(n%i==0){ cout<<i; n=n/i; }else if(n%i!=0){ break; } } } } } cout<<endl; } return 0; } ``` --- ### 5.<font color="#f00">[C_MM084-易] 尋找最佳商品問題</font> #### =>[題目連結](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=1155) #### 問題敘述: 假設給定一個檔案記錄著 n 筆商品項目資料:商品編號,人氣值 F ,價格 C 。 分數計算方式為: FC = F/C (四捨五入小數點第4位) 對 FC 值由大到小排序,越大越好,輸出 FC 最好的商品。 ### 完成者: 0. <社長> > [time=Wed, Apr 29, 2020 10:08 PM] 1. <副社長> > 我就棒 >[time=Sat, May 2, 2020 2:24 PM] 2. <張嘉元> > [time=Sat, May 2, 2020 10:59 PM] 3. <李晨維> > [time=Wed, May 6, 2020 11:18 AM]萬惡的double和int型別轉換 4. <吳冠毅> > [time=Wed, May 6, 2020 11:58 AM] 5. <郭浩雲> > [time=Wed, May 6, 2020 4:10 PM] > 被輸出位數害到,雪特 6. <吳品範> > [time=Tue, May 12, 2020 7:22 PM] > 該死的小數點 #### 社長提供的測資: 無。 ### 參考解答(社長提供): #### C++: #### 社長(曾俊翔)提供 ```cpp= #include <bits/stdc++.h> #define MAX 20 using namespace std; typedef struct node{ int no; double FC; } products; bool cmp(products x,products y){ if(x.FC==y.FC) return (x.no>y.no); else return (x.FC>y.FC); } products arr[MAX]; int main(){ int N; double F,C; while(cin >> N){ for(int i=0; i<N; i++){ cin >> arr[i].no >> F >> C; arr[i].FC= F/C; } sort(arr,arr+N,cmp); cout << "The best production number is " << arr[0].no << endl; cout << "FC is " << fixed << setprecision(4) << arr[0].FC << endl; for(int i=0; i<N; i++) cout << arr[i].no << " " << fixed << setprecision(4) << arr[i].FC << endl; } return 0; } ``` #### 前社長(郭浩雲)提供 ```cpp= #include <iostream> #include <cmath> //四捨五入round #include <iomanip> //控制輸出位數 using namespace std; struct product{ int id=0; double F=0.0,C=0.0,FC=0.0; }; int main() { int N=0, count=0; cin >> N; struct product A[N],temp; bool sort = false; for(int i=0;i<N;i++){ cin >> A[i].id >> A[i].F >> A[i].C; A[i].FC = A[i].F / A[i].C; A[i].FC = round(A[i].FC * 10000); A[i].FC /= 10000; } while(sort==false){ count = 0; for(int i=1;i<N;i++){ if(A[i-1].FC < A[i].FC){ temp = A[i-1] ,A[i-1] = A[i] ,A[i] = temp; }else count++; } if(count == N-1) sort = true; } cout << "The best production number is " << A[0].id << endl << "FC is " << fixed << setprecision(4) << A[0].FC << endl; for(int i=0;i<N;i++){ cout << A[i].id << " "; cout << fixed << setprecision(4) << A[i].FC << endl; } return 0; } ``` #### Java: #### 副社長(李弘唯)提供 ```java= import java.io.FileNotFoundException; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { Scanner sc = new Scanner(System.in); /* File file= new File("C:\\Users\\user\\IdeaProjects\\untitled\\out\\production\\untitled\\input.txt"); sc = new Scanner(new FileInputStream(file)); */ int time = sc.nextInt(); FC[] fc = new FC[time] ; for(int i = 0 ; i < time ; i++ ){ fc[i] = new FC(sc.nextInt(),sc.nextInt(),sc.nextInt()); } sort(fc); System.out.printf("The best production number is %d\n",fc[fc.length-1].getIndex()); System.out.printf("FC is %.4f\n",fc[fc.length-1].getFC()); for(int i = time - 1 ; i >= 0 ;i--){ System.out.println(String.format("%d %.4f", fc[i].getIndex(),fc[i].getFC())); } } public static void sort(FC[] fcs){ for(int i = 0 ; i < fcs.length ; i++ ){ for(int j = 1;j < fcs.length-i ; j++){ if(fcs[j].getFC() < fcs[j-1].getFC()){ FC temp = fcs[j-1]; fcs[j-1] = fcs[j]; fcs[j] = temp; } } } } } class FC{ private double FC; private int index; public FC(int index , int a, int b ) { this.index = index; FC = (double) a /(double) b ; } public double getFC() { return FC; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public void setFC(double FC) { this.FC = FC; } } ``` ### 參考解答(社員提供): #### C: #### 吳品範<高二> 提供 ```cpp= #include <stdio.h> int main(void) { int n,i,j,k,l; double d,e; scanf("%d",&n); double a[n],b[n]; int m[n]; for(i=0;i<n;i++) { scanf("%d %lf %lf",&m[i],&a[i],&b[i]); } double p[n]; for(j=0;j<n;j++) { p[j]=a[j]/b[j]; } for(k=0;k<n;k++) { for(l=k+1;l<n;l++) { if(p[k]<p[l]) { d=p[k]; p[k]=p[l]; p[l]=d; e=m[k]; m[k]=m[l]; m[l]=e; } } } printf("The best production number is %d\n",m[0]); printf("FC is %.4lf\n",p[0]); for(i=0;i<n;i++) { printf("%d %.4lf\n",m[i],p[i]); } return 0; } ``` #### C++: #### 張嘉元<高一> 提供 ```cpp= #include <iostream> #include <iomanip> using namespace std; int main (){ int a,c=0,d=0; cin >> a; double mem[20][3]={0},tot[20]={0},ran[20]={0}; for(c=0;c<a;c++){ for(d=0;d<3;d++){ cin >> mem[c][d]; } tot[c]=(float)mem[c][1]/mem[c][2]; } for(c=0;c<a;c++){ ran[c]=ran[c]+1; for(d=0;d<a;d++){ if(tot[c]<tot[d]){ ran[c]=ran[c]+1; } } } for(c=0;c<a;c++){ if(ran[c]==1){ cout<<"The best production number is "<<mem[c][0]<<"\nFC is "<<fixed<<setprecision(4)<<tot[c]<<"\n"; cout.unsetf(ios::fixed); } } for(c=0;c<a;c++){ for(d=0;d<a;d++){ if(c+1==ran[d]){ cout<<mem[d][0]<<" "<<fixed<<setprecision(4)<<tot[d]<<"\n"; cout.unsetf(ios::fixed); } } } return 0; } ``` #### 李晨維<高一> 提供 ```cpp= #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; double f[n+1],c[n+1]; int tn=0,num[n+1],rank[n+1],k=1; double fc[n+1],fct[n+1]; for(int i=1;i<=n;i++){ cin>>tn; cin>>f[tn]>>c[tn]; fc[tn]=(1.0* f[tn])/(1.0*c[tn]); fct[tn]=fc[tn]; } fct[0]=0; sort(fct,fct +n+1); for(int b=n;b>=1;b--){ //排名 for(int a=1;a<=n;a++) //原資料 { if(fct[b]==fc[a]){ rank[k]=a; k++; } } } cout<<"The best production number is "<<rank[1]<<endl<<"FC is "; cout<<fixed<<setprecision(4)<<fct[n]<<endl; for(int b=n;b>0;b--){ cout<<rank[n+1-b]<<" "<<fct[b]*1.0<<endl; } } ``` --- ## <font color="#3AAA">統計表 | 第一題 | 第二題 | 第三題 | 第四題 | 第五題 | 全對者 | |:------:|:------:|:------:|:------:|:------:|:---:| | 13 | 9 | 6 | 6 | 7 | 6 | --- > [time=Fri, May 15, 2020 12:58 AM] ###### tags: `社內考試`