Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- double p1 = 0.0, p2 = 0.0, p3 = 0.0, p4 = 0.0, p5 = 0.0;
- int num;
- cin >> num;
- int number;
- for (int i = 0; i < num; i++) {
- cin >> number;
- if (number < 200) {
- p1++;
- }
- else if (number < 400) {
- p2++;
- }
- else if (number < 600) {
- p3++;
- }
- else if (number < 800) {
- p4++;
- }
- else {
- p5++;
- }
- }
- cout.setf(ios::fixed);
- cout.precision(2);
- cout << p1 / num * 100 << "%" << endl;
- cout << p2 / num * 100 << "%" << endl;
- cout << p3 / num * 100 << "%" << endl;
- cout << p4 / num * 100 << "%" << endl;
- cout << p5 / num * 100 << "%" << endl;
- return 0;
- }
- Решение с речник, тернарен оператор и foreach:
- #include <iostream>
- #include<string>
- #include<map>
- using namespace std;
- int main() {
- map <string, double> histogram = { {"p1", 0.0}, {"p2", 0.0}, {"p3", 0.0}, {"p4", 0.0}, {"p5", 0.0} };
- int num;
- cin >> num;
- int number;
- for (int i = 0; i < num; i++) {
- cin >> number;
- string p =
- number < 200 ? "p1" :
- number < 400 ? "p2" :
- number < 600 ? "p3" :
- number < 800 ? "p4" : "p5";
- histogram[p]++;
- }
- cout.setf(ios::fixed);
- cout.precision(2);
- for (string i : {"p1", "p2", "p3", "p4", "p5"}) {
- cout << histogram[i] / num * 100 << "%" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement