Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1217
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1217
Getline Two - Fruits
By Neilor Tonin, URI Brazil
Timelimit: 1
Ms. Parcinova, Mangojata's mom, asked to help her to calculate the consumption of fruit in the house and the amount spent per day on these fruits. Mangojata now must to do a program from a table that his mother was using for annotations by almost a year. In this table, Ms. Parcinova noted the amount of days and then the spent money each day with these fruits, considering always one KG of each fruit.
Input
The first line of input contains an integer N (1 ≤ N ≤ 365) indicating the number of test cases that follows. Each test case is composed of two lines. The first line contains a floating point number V (0.10 ≤ V ≤ 20.00) indicating the amount spent on the second line contains the name of each fruit that owns Parcinova bought.
Output
For each test case, print how many kilograms of fruits Ms. Parcinova bought in each day, with corresponding message like following example. At the end, print the average consumption in kg by day with 2 decimal numbers and the average of money spent by day, like following example.
Note.: all letters must be printed in lowercase with exception of "R" of "R$"
Sample Input | Sample Output |
3 |
day 1: 3 kg |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
float kilos(char *);
void main ()
{
char lixo;
char fruta[10000] = { 0 };
unsigned short casos, i, kilo;
float preco, mediaKilo, mediaPreco;
scanf("%hu", &casos);
scanf("%c", &lixo);
mediaKilo = mediaPreco = 0;
for (i = 0; i < casos; ++i)
{
scanf("%f", &preco);
mediaPreco += preco;
scanf("%c", &lixo);
fgets(fruta, 10000, stdin);
kilo = kilos(fruta);
mediaKilo += kilo;
printf("day %hu: %hu kg\n", i + 1, kilo);
}
printf("%.2f kg by day\n", mediaKilo / (casos * 1.0f));
printf("R$ %.2f by day\n", mediaPreco / casos * 1.0f);
}
float kilos(char *fruta)
{
char *tmp;
unsigned short qtsFrutas = 0;
tmp = strtok(fruta, " ");
qtsFrutas++;
do
{
tmp = strtok('\0', " ");
if (tmp)
qtsFrutas++;
} while (tmp);
return qtsFrutas;
}
Copy The Code &
Try With Live Editor
Input
9.58
Mamao Maca Melao
2.65
Melancia
9.54
Pera Uva Goiaba
Output
day 2: 1 kg
day 3: 3 kg
2.33 kg by day
R$ 7.26 by day
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <stdio.h>
int main(){
int n, i, j, num, kg;
float preco, total;
char frutas[10000];
scanf("%d",&n);
total = 0.0;
kg = 0;
for(i = 0; i < n; i++){
num = 0;
scanf("%f%*c",&preco);
total += preco;
scanf("%[^\n]%*c",&frutas);
for(j = 0; frutas[j] != '\0'; j++){
if(frutas[j] == 32) num++;
}
kg += num+1;
printf("day %d: %d kg\n",i+1,num+1);
}
printf("%.2f kg by day\n",(float)kg/n);
printf("R$ %.2f by day\n",total/n);
return 0;
}
Copy The Code &
Try With Live Editor
Input
9.58
Mamao Maca Melao
2.65
Melancia
9.54
Pera Uva Goiaba
Output
day 2: 1 kg
day 3: 3 kg
2.33 kg by day
R$ 7.26 by day
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader in;
StringBuilder out = new StringBuilder();
File file = new File("in");
if (file.exists())
in = new BufferedReader(new FileReader(file));
else
in = new BufferedReader(new InputStreamReader(System.in));
String line, lines[];
int n = Integer.parseInt(in.readLine()), aux;
double sum1 = 0, sum2 = 0;
for ( int i = 0; i < n; i++ )
{
sum1 += Double.parseDouble(in.readLine());
aux = in.readLine().split(" ").length;
sum2 += aux;
out.append("day "+(i+1)+": "+aux+" kg\n");
}
sum1 = sum1/(double)(n);
sum2 = sum2/(double)(n);
out.append(String.format("%.02f", sum2)+ " kg by day\n");
out.append("R$ "+String.format("%.02f", sum1)+ " by day\n");
System.out.print(out);
}
}
Copy The Code &
Try With Live Editor
Input
9.58
Mamao Maca Melao
2.65
Melancia
9.54
Pera Uva Goiaba
Output
day 2: 1 kg
day 3: 3 kg
2.33 kg by day
R$ 7.26 by day
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("node:fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n", 2 * 365 + 1)
function main() {
const NUM_TEST_CASES_FROM_INPUT = Number.parseInt(input.shift(), 10)
const output = []
const total = {
days: 0,
price: 0,
mass: 0
}
while (input.length > 0 && total.days <= NUM_TEST_CASES_FROM_INPUT) {
const [[price], [...fruits]] = input.splice(0, 2).map(line => line.split(" "))
total.days += 1
total.price += Number.parseFloat(price)
total.mass += fruits.length
output.push(`day ${total.days}: ${fruits.length} kg`)
}
output.push(
`${(total.mass / total.days).toFixed(2)} kg by day`,
`R$ ${(total.price / total.days).toFixed(2)} by day`
)
console.log(output.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
9.58
Mamao Maca Melao
2.65
Melancia
9.54
Pera Uva Goiaba
Output
day 2: 1 kg
day 3: 3 kg
2.33 kg by day
R$ 7.26 by day
#5 Code Example with Python Programming
Code -
Python Programming
n = int(input())
sv = sq = 0
for i in range(n):
sv += float(input())
q = len(str(input()).split())
sq += q
print('day {}: {} kg'.format(i + 1, q))
print('{:.2f} kg by day'.format(float(sq / n)))
print('R$ {:.2f} by day'.format(float(sv / n)))
Copy The Code &
Try With Live Editor
Input
9.58
Mamao Maca Melao
2.65
Melancia
9.54
Pera Uva Goiaba
Output
day 2: 1 kg
day 3: 3 kg
2.33 kg by day
R$ 7.26 by day