Печать
Категория: For
Просмотров: 3261

For 6. Дано вещественное число — цена 1 кг конфет. Вывести стоимость 1.2, 1.4, ... , 2 кг конфет.

Решение на Python 3

import random

price = round(random.uniform(0, 100),2)
print('Цена 1 кг конфет: ', price)
print()
for i in range(0,5):
x = 1.2 + 0.2*i
print('Стоимость {0:.1f} кг: {1:.2f}'.format(x, x * price))
print()

Решение на C++

#include <bits/stdc++.h>
using namespace std;

int main() {
srand((int)time(0));
float price;
price = (rand() % 10000) / 100.0;
cout << "Price for 1 kg: " << price << endl;
double cost = 0;
for(double i = 1.2; i <= 2; i+=0.2) {
cost = i * price;
cout << std::fixed;
cout << std::setprecision(1);
cout << i <<" kg : ";
cout << std::setprecision(2);
cout << "price -> " << cost << endl;
}
return 0;
}