# 109 D ::: danger 所有程式碼皆不保證為正確解答 解題前請先看清楚題目,再自行修正以下程式碼使其符合題目要求:slightly_smiling_face: 如果抓到BUG也非常歡迎留言告訴大家怎麼修正喔(反白之後就能留言了) ::: 第一題   第二題 ```cpp= #include<iostream> using namespace std; int main(){ while(true){ cout<<"\t20506\t吳佩芸\n\td卷\t第二題\n"; cout<<"輸入金額,算找回的錢\n"; int payable, paid, change; cout<<"應付金額:"; cin>>payable; cout<<"\n實際付款金額:"; cin>>paid; if(paid<payable){ cout<<"\n輸入錯誤\n"; }else if(paid==payable){ cout<<"不用找錢\n"; }else{ change=paid-payable; cout<<"應找"<<change<<"元\n"; cout<<"1000元: "<<change/1000<<"張\n"; change-=1000*(change/1000); cout<<"500元: "<<change/500<<"張\n"; change-=500*(change/500); cout<<"100元: "<<change/100<<"張\n"; change-=100*(change/100); cout<<"50元: "<<change/50<<"張\n"; change-=50*(change/50); cout<<"10元: "<<change/10<<"張\n"; change-=10*(change/10); cout<<"5元: "<<change/5<<"張\n"; change-=5*(change/5); cout<<"1元: "<<change/1<<"張\n"; } } return 0; } ``` 第三題 ```cpp= #include <iostream> using namespace std; int main() { int i, j, n, sum = 0; while(true){ cout<<"\t20506\t吳佩芸\n\td卷\t第三題\n"; cout<<"請輸入n:"; cin>>n; sum = 0; for(j = 1; j < n; j++){ if( n%j == 0){ sum += j; } } if(sum == n){ cout<<n<<"為完美數\n"; }else { cout<<n<<"不是完美數\n"; } } return 0; } ``` 第四題 ```cpp= #include<iostream> #include<time.h> #include<cstdlib> using namespace std; int main(){ int guess, ans, ub, lb; srand(time(NULL)); ub=1000; lb=99; while(true){ cout<<"\t20506\t吳佩芸\n\td卷\t第四題\n"; cout<<"請在99到1000中猜一個數字\n"; ans=99+rand()%(1000-99+1); 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; } ``` 自訂範圍 ```cpp= #include<iostream> #include<time.h> #include<cstdlib> using namespace std; int main(){ int guess, ans, ub, lb; srand(time(NULL)); while(true){ cout<<"\t20506\t吳佩芸\n\td卷\t第四題\n"; cin>>lb>>ub; cout<<"請在"<<lb<<"到"<<ub<<"中猜一個數字\n"; ans=lb+rand()%(ub-lb+1); 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; } ```
×
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