Algorithm
Problem Name: beecrowd | 2486
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2486
C Mais ou Menos?
By João Marcos Salvanini Bellini de Moraes, IFSULDEMINAS Brazil
Timelimit: 1
Lately, several people are going to Dr. Cláudia Café com Leite to know if they are consuming the recommended daily amount of vitamin C. This exhausted her so she asked you to write a program for her that, given the daily intake of foods rich in vitamin C by a person, returns how much this person has to consume more or less to achieve the recommended amount.
In order to do such, you can use the following table:
Foods rich in Vitamin C | Amount of Vitamin C |
---|---|
suco de laranja | 120 mg |
morango fresco | 85 mg |
mamao | 85 mg |
goiaba vermelha | 70 mg |
manga | 56 mg |
laranja | 50 mg |
brocolis | 34 mg |
Consider the recommended daily intake of vitamin C is between 110 mg and 130 mg, inclusive.
Input
Each test case consists of an integer T (1 ≤ T ≤ 7) indicating that the person daily intakes T foods among the 7 foods from the table. The next T lines with an integer N and a food (lowercase and no blank spaces) indicates that the person intakes an amount N of that food. Read input until T = 0.
Output
For each test case (T), if the intake exceeds the recommended limit, print "Menos X mg", in which X represents how much less the person must consume to reach the recommended limit; if the intake doesn't reach the recommended amount, print "Mais X mg", in which X represents how much more the person must consume to reach the recommended amount; if the intake is between the recommended amount range, print "X mg", in which X represents the daily amount of vitamin C intaken by the person.
Input Sample | Output Sample |
2 2 suco de laranja 3 mamao 1 3 brocolis 2 1 manga 1 laranja 1 1 suco de laranja 0 |
Menos 365 mg Mais 8 mg Mais 4 mg 120 mg |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
int check(int a, char str[])
{
char c;
int x;
if (strcmp(str, "suco de laranja")==0)
c='s';
else if (strcmp(str, "morango fresco")==0)
c='f';
else if (strcmp(str, "mamao")==0)
c='o';
else if (strcmp(str, "goiaba vermelha")==0)
c='g';
else if (strcmp(str, "manga")==0)
c='m';
else if (strcmp(str, "laranja")==0)
c='l';
else if (strcmp(str, "brocolis")==0)
c='b';
switch (c)
{
case 's':
x=a*120;
break;
case 'f':
x=a*85;
break;
case 'o':
x=a*85;
break;
case 'g':
x=a*70;
break;
case 'm':
x=a*56;
break;
case 'l':
x=a*50;
break;
case 'b':
x=a*34;
break;
}
return x;
}
void calculate(int x)
{
int n;
if (x >= 110 && x < = 130)
printf("%i mg\n", x);
else if (x < 110)
{
n = 110-x;
printf("Mais %i mg\n", n);
}
else
{
n = x-130;
printf("Menos %i mg\n", n);
}
}
int main(void)
{
char c, str[50];
int i, j, a, b, n;
while (1)
{
scanf("%i", &n);
b = 0;
if (n == 0)
break;
for (i = 0; i < n; ++i)
{
scanf("%i %[^\n\r]", &a, str);
b+=check(a, str);
}
calculate(b);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <string>
using namespace std;
int main(){
int total, talim, talimtemp, valor, i, cvalor[8] = {120, 85, 85, 70, 56, 50, 34};
string C, c[8] = {"suco de laranja", "morango fresco", "mamao", "goiaba vermelha", "manga", "laranja", "brocolis"};
while (cin >> total and total != 0) {
valor = 0;
while (total--) {
cin >> talim;
getchar();
getline(cin, C);
for (i = 0;i < 8; i++) {
if (C == c[i]) {
valor += cvalor[i] * talim;
}
}
}
if (valor > 109 and valor < 131) {
cout << valor << " mg" << endl;
}else if (valor < 110) {
cout << "Mais " << 110-valor << " mg" << endl;
}else {
cout << "Menos " << valor-130 << " mg" << endl;
}
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
let number = lines.shift();
let sum = 0;
while(number != 0){
for(let i = 0; i < number; i++){
let [a, b] = lines.shift().trim().split(" ");
switch(b){
case "suco":
sum += parseInt(a) * 120;
break;
case "morango":
sum += parseInt(a) * 85;
break;
case "mamao":
sum += parseInt(a) * 85;
break;
case "goiaba":
sum += parseInt(a) * 70;
break;
case "manga":
sum += parseInt(a) * 56;
break;
case "laranja":
sum += parseInt(a) * 50;
break;
case "brocolis":
sum += parseInt(a) * 34;
break;
}
}
if(sum >= 110 && sum <= 130){
console.log(`${sum} mg`>;
}
else if(sum > 130){
console.log(`Menos ${(130 - sum) * -1} mg`)
}
else if(sum < 110){
console.log(`Mais ${110 - sum} mg`)
}
sum = 0;
number = lines.shift(>;
}
Copy The Code &
Try With Live Editor
Input
Output