Algorithm
Drought
By Neilor Tonin, URI Brazil
Due to the continuous drought that happened recently in some regions of Brazil, the Federal Government created an agency to assess the consumption of these regions in order to verify the behavior of the population at the time of rationing. This agency will take some cities (for sampling) and it will verify the consumption of each of the townspeople and the average consumption per inhabitant of each town.
Input
The input contains a number of test's cases. The first line of each case of test contains an integer N (1 ≤ N ≤ 1 * 10 6), indicating the amount of properties. The following N lines contains a pair of values X (1 ≤ X ≤ 10) and Y ( 1 ≤ Y ≤ 200) indicating the number of residents of each property and its total consumption (m3). Surely, no residence consumes more than 200 m3 per month. The input's end is represented by zero.
Output
For each case of test you must present the message “Cidade# n:”, where n is the number of the city in the sequence (1, 2, 3, ...), and then you must list in ascending order of consumption, the people's amount followed by a hyphen and the consumption of these people, rounding the value down. In the third line of output you should present the consumption per person in that town, with two decimal places without rounding, considering the total real consumption. Print a blank line between two consecutives test's cases. There is no blank line at the end of output.
Input Sample | Output Sample |
3 |
Cidade# 1: |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int arr[300];
int main(int argc, char const *argv[])
{
int i, j, n, c = 1, a, b, ta, tp, fp;
double ip;
bool bo = false;
while(scanf("%d", &n) && n)
{
if(bo) printf("n");
bo = true;
ta = tp = 0;
memset(arr, 0, sizeof arr);
for (i = 0; i < n; ++i)
{
scanf("%d %d", &a, &b);
ta += b;
tp += a;
arr[b/a] += a;
}
printf("Cidade# %d:n", c); c++;
for(i = 0, j = 0; i < 300; i++>
{
if(arr[i] > 0){
if(j != 0)
printf(" ");
printf("%d-%d", arr[i], i);
j++;
}
}
printf("n");
fp = (int) (modf ((double)ta/tp, &ip) * 100);
if(fp < 10) printf("Consumo medio: %d.0%d m3.n", (int)ip, (int)fp);
else printf("Consumo medio: %d.%d m3.n", (int)ip, (int)fp>;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output