資源限制時間限制:1.0s 內存限制:256.0MB問題描述 提示用戶輸入被除數(dividend)和除數(divisor),若除數爲0,則提示用戶重新輸入,直至除數非零爲止。最後輸出商。程序建議大家將被除數、除數和商都定義爲整形。
輸入被除數提示語句爲:Please enter the dividend:
輸入除數提示語句爲:Please enter the divisor:
提示除數爲0需要重新輸入的語句爲: Error: divisor can not be zero! Please enter a new divisor:
建議:大家直接複製上述語句,以免出現不必要的錯誤。輸入格式 被除數 除數
注:若除數爲零,則需要連續輸入除數直至其非零爲止。輸出格式 提示性語句 商
注意每個提示性語句輸出後需要換行,具體參考下面的樣例輸出。樣例輸入一個滿足題目要求的輸入範例。
例1:
10 2
例2:
20 0 0 4樣例輸出與上面的樣例輸入對應的輸出。
例1:
Please enter the dividend:
Please enter the divisor:
5
例2:
Please enter the dividend:
Please enter the divisor:
Error: divisor can not be zero! Please enter a new divisor:
Error: divisor can not be zero! Please enter a new divisor:
5數據規模和約定 無
解題思路:
這道題目很簡單,就是按照題設所給的要求進行答題即可,需要注意的室輸入的文字的大小寫問題,輸出換行問題,以及遇到零的時候需要循環的問題。代碼如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int m, n;
cin >> m;
cout << "Please enter the dividend:" << endl;
cout << "Please enter the divisor:" << endl;
while(1){
cin >> n;
if (n != 0){
break;
}else{
cout << "Error: divisor can not be zero! Please enter a new divisor:" << endl;
}
}
cout << m / n;
return 0;
}