P5706 Distribute Cola again
Description
Currently, we have Cola, t milliliter, and we're gonna distribute with n students. Each student needs 2 cups. Now we need to know how many milliliters can each student get and how many cups we need totally? Input a real number t and an integer number n, separate them by space.
0 <= t <= 10000 and no more than 3 decimal places, 1 <= n <= 1000
Input format
none
Output format
none
Example
500.0 3
166.667
6
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Resolve
#include <iostream>
#include <iomanip>
using namespace std;
int water() {
double milliliter;
int classmate;
cin >> milliliter >> classmate;
cout << fixed << setprecision(3) << milliliter / classmate << endl;
cout.unsetf(ios_base::fixed);
cout << setprecision(6) << classmate*2;
return 0;
}
What I learned
If a number is too large to print significant digits that we want even using the setprecision function, so many systems use scientific counting to solve this problem.
here is a program:
// This program asks for sales figures for three days.
// The total sales are calculated and displayed in a table.
#include <iostream>
#include <iomanip> // Header file needed to use stream manipulators
using namespace std;
int main()
{
double day1, day2, day3, total;
// Get the sales for each day
cout << "Enter the sales for day 1: ";
cin >> dayl;
cout << "Enter the sales for day 2: ”;
cin >> day2;
cout << "Enter the sales for day 3: ”;
cin >> day3;
// Calculate total sales
total = day1 + day2 + day3;
// Display the sales figures
cout << "\nSales Figures\n";
cout << "-------------\n" ;
cout << setprecision (5);
cout << "Day 1: " << setw(8) << day1 << endl;
cout << "Day 2: " << setw(8) << day2 << endl;
cout << "Day 3: " << setw(8) << day3 << endl;
cout << "Total: " << setw(8) << total << endl;
return 0;
}
The output of the above program after type in large numbers:
Enter the sales for day 1: 145678.99
Enter the sales for day 2: 205614.85
Enter the sales for day 3: 198645.22
Sales Figures
-------------
Day 1: 1.4568e+005
Day 2: 2.0561e+005
Day 3: 1.9865e+005
Total: 5.4994e+005
In case this happens, we can use another stream operator, fixed, which represents the output should be expressed on a fixed point or exact decimal point way:
cout << fixed
The most powerful way is when fixed and setprecision are used together. setprecision will make different work. It will show special digits after the decimal point, rather than mean the whole digits.
An example that rewrites the 22nd line of the last program:
cout << fixed << setprecision(2);
Result:
Enter the sales for day 1: 321.57
Enter the sales for day 2: 269. 60
Enter the sales for day 3: 307.00
Sales Figures
-------------
Day 1: 321.57
Day 2: 269.60
Day 3: 307.00
Total: 898.17

浙公网安备 33010602011771号