Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- int num;
- cin >> num;
- double result = 0, num0To10 = 0, num10To20 = 0, num20To30 = 0, num30To40 = 0, num40To50 = 0, invalidNum = 0, current;
- for (int i = 0; i < num; i++) {
- cin >> current;
- if (current >= 0 && current < 10) {
- result += current * 0.2;
- num0To10++;
- }
- else if (current >= 10 && current < 20) {
- result += current * 0.3;
- num10To20++;
- }
- else if (current >= 20 && current < 30) {
- result += current * 0.4;
- num20To30++;
- }
- else if (current >= 30 && current < 40) {
- result += 50;
- num30To40++;
- }
- else if (current >= 40 && current <= 50) {
- result += 100;
- num40To50++;
- }
- else {
- result /= 2;
- invalidNum++;
- }
- }
- cout.setf(ios::fixed);
- cout.precision(2);
- cout << result << "\n";
- cout << "From 0 to 9: " << 100.0 * num0To10 / num << "%\n";
- cout << "From 10 to 19: " << 100.0 * num10To20 / num << "%\n";
- cout << "From 20 to 29: " << 100 * num20To30 / num << "%\n";
- cout << "From 30 to 39: " << 100 * num30To40 / num << "%\n";
- cout << "From 40 to 50: " << 100 * num40To50 / num << "%\n";
- cout << "Invalid numbers: " << 100 * invalidNum / num << "%\n";
- return 0;
- }
- Решение с тернарен оператор:
- #include <iostream>
- using namespace std;
- int main() {
- int num;
- cin >> num;
- double result = 0, num0To10 = 0, num10To20 = 0, num20To30 = 0, num30To40 = 0, num40To50 = 0, invalidNum = 0, current;
- for (int i = 0; i < num; i++) {
- cin >> current;
- result +=
- current >= 0 && current < 10 ? current * 0.2 :
- current >= 10 && current < 20 ? current * 0.3 :
- current >= 20 && current < 30 ? current * 0.4 :
- current >= 30 && current < 40 ? 50 :
- current >= 40 && current <= 50 ? 100 : -1.0 * result / 2;
- current >= 0 && current < 10 ? num0To10++ :
- current >= 10 && current < 20 ? num10To20++ :
- current >= 20 && current < 30 ? num20To30++ :
- current >= 30 && current < 40 ? num30To40++ :
- current >= 40 && current <= 50 ? num40To50++ : invalidNum++;
- }
- cout.setf(ios::fixed);
- cout.precision(2);
- cout << result << "\n";
- cout << "From 0 to 9: " << 100.0 * num0To10 / num << "%\n";
- cout << "From 10 to 19: " << 100.0 * num10To20 / num << "%\n";
- cout << "From 20 to 29: " << 100 * num20To30 / num << "%\n";
- cout << "From 30 to 39: " << 100 * num30To40 / num << "%\n";
- cout << "From 40 to 50: " << 100 * num40To50 / num << "%\n";
- cout << "Invalid numbers: " << 100 * invalidNum / num << "%\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement