--- tags: 一社一服務 --- # 108題目(A卷) [回目錄](https://hackmd.io/@PeiYun/CppTutorial) > 為了幫(強)助(迫)各位思考,以下僅提供<font color=#f00>部分程式碼</font>,有任何問題歡迎私訊[資訊社IG](https://www.instagram.com/dzif_22nd_/?hl=zh-tw) 1. 互質 ```cpp= int a, b, r; //a 被除數,b 除數,r餘數 while(true){ cin>>a>>b; r=a%b; // %取餘數 while(?){ a=b; b=r; r=a%b; } if(?){ cout<<"互質"<<endl; }else{ cout<<"不互質,最大公因數:"<<?<<endl; } } ``` 2. 閏年 * 西元年份除以4可整除,且除以100不可整除,為閏年。 * 西元年份除以400可整除,為閏年。 ```cpp= int a,b; // a, b為兩個年份 int count=0; //count 計算一共有幾個閏年 while( cin >> a>>b ) { // 西元年份除以4可整除,且除以100不可整除,為閏年。 /* 西元年份除以400可整除,為閏年。*/ for(int x=a; x<=b; x++){ if( x%400==0 ? ( x%4==0 ? x%100!=0 ) ){ cout << x << " "; count ++; } } cout<<endl<<count<<endl; } ``` 3. 雞兔同籠 ```cpp= int n; // 一共n隻腳 while(cin>>n){ int count=1; //count 計算第幾種可能 int x, y; // x隻雞 y 隻兔 for(y=0; ?; y++){ x=?; if(n==?){ cout<<count<<":"<<x<<":"<<y<<endl; count++; } } } ``` 4. 級數 ```cpp= int n; // 輸入一個數 while(cin>>n){ float sum=0; // sum計算加總 if(n%2==0){ //if偶數 for(int x=2; x<=n; x+=2){ sum+=pow(x, x-1); //pow:次方 記得include<cmath> } } else{ //奇數 for(int x=1; x<=n; x+=2){ sum+=sqrt(x); //sqrt:根號 } } cout<<sum<<endl; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up