# 109 C ::: danger 所有程式碼皆不保證為正確解答 解題前請先看清楚題目,再自行修正以下程式碼使其符合題目要求 :slightly_smiling_face: 如果抓到BUG也非常歡迎留言告訴大家怎麼修正喔(反白之後就能留言了) ::: 第一題 ``` cpp= #include<iostream> using namespace std; int main(){ while(true){ cout<<"\t20506\t吳佩芸\n\tc卷\t第一題\n"; cout<<"輸入一小於等於30的正整數,輸出其圖型\n"; int n,s; bool check=true; cin>>n; s=n; if(n>30||n<=0){ cout<<"輸入的數字有誤\n"; check=false; } if(check){ for(int i=1; i<=n; i++){ for(int k=s; k>0; k--){ cout<<"*"; } s--; cout<<endl; } } } return 0; } ``` 第二題 sol 1 ```cpp= #include<iostream> using namespace std; int main(){ while(true){ cout<<"\t20506\t吳佩芸\n\tc卷\t第二題\n"; cout<<"輸入一正整數,輸出薪水\n"; float h; cin>>h; float sum; if(h<=60){ sum=h*160; }else if(h<=75){ sum=((h-60)*1.25+60)*160; }else{ sum=((h-76+1)*1.75+15*1.25+60)*160; } cout<<sum<<endl; } return 0; } ``` sol 2 by 資訊社中二小學弟-王中靖 :sunglasses: ```cpp= #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { float n; int a,b,c; cout<<"請輸入超商店員工作的時數:"; cin>>n; if(n>=0) { a=160*n; b=160*0.25*(n-60); c=160*0.5*(n-75); if (n<=60) { cout<<"超商店員的薪水是:"<<a<<"元\n"; } else if(n<=75) { cout<<"超商店員的薪水是:"<<a+b<<"元\n"; } else { cout<<"超商店員的薪水是:"<<a+b+c<<"元\n"; } } system("PAUSE"); return EXIT_SUCCESS; } ``` 第三題 ::: warning 這題我用了一個自訂函式來處理i是不是質數 如果對自訂函式有興趣可以參考[此網站](https://www.csie.ntu.edu.tw/~b98902112/cpp_and_algo/cpp02/user_defined_function.html)以了解更多 在還不懂自訂函式之前先別直接照抄程式碼喔,可以想辦法把6~13的程式碼的功能放回主程式中,就不用使用到自訂函式囉。 ::: ```cpp= #include<iostream> #include <cmath> using namespace std; bool isprime(); isprime(int i){ for (int k = 2; k < i; k++) { if (i % k == 0) { return false; } } return true; } int main(){ while(true){ cout<<"\t20506\t吳佩芸\n\tc卷\t第三題\n"; cout<<"輸入一大於3的正整數n,輸出小於n的最大質數跟小於n的第二大質數\n"; int n,max=2,sec; bool check=true; cin>>n; if(n<=3){ cout<<"輸入的數字有誤/n"; check=false; } if(check){ int tmp; for(int i=3; i<=n; i++){ if(isprime(i)){ if(i>max){ tmp=max; max=i; i=tmp; } } } cout<<"最大:"<<max<<",第二大:"<<tmp<<endl; } } return 0; } ``` 第四題 ```cpp= #include<iostream> #include<time.h> #include<cstdlib> using namespace std; int main(){ int guess, ans, ub, lb; srand(time(NULL)); ans=99+rand()%(1000-99+1); ub=1000; lb=99; while(true){ cout<<"\t20506\t吳佩芸\n\tc卷\t第四題\n"; cout<<"請在99到1000中猜一個數字\n"; while(cin>>guess){ if(guess==ans){ cout<<"correct\n"; break; }else if(guess<lb || guess>ub){ cout<<"error\n"; }else if(guess>ans){ ub=guess; cout<<lb<<"to "<<ub<<endl; }else{ lb=guess; cout<<lb<<"to "<<ub<<endl; } } } return 0; } ``` 不知道那裡冒出來的第五題 題目:每次剪掉1/2段繩子,請問要剪幾次才會<3,並求出剩餘長度 ```cpp= #include<iostream> using namespace std; int main(){ int n, count=0; while(cin>>n){ while(n>3){ n/=2; count++; } cout<<"cut "<<count<<" times,left: "<<n<<endl; } return 0; } ```
×
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