Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1540
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1540
Energy Planning
By Normandes Jr, UFU Brazil
Timelimit: 1
You are contributing for a summit that will help forecast the growing up of eletrical energy in Brazil, making the eletric power be available for a long time in the future.
To be able do that, you have the following information:
- during the year of 2010 the consume average was 104.326 GWh.
- In 2013 the consume was 127.755 GWh.
You should calculate the anual growing up rate for many situations and forcasting, you can assume that this growing up is linear. In this case, the rate was 7809.66 GWh per year.
Input
The first input line has an integer number N (1 <= N <= 1000) that is the total test cases.
The following N lines are made by the integers A, B (B > 0), C and D (D > 0) separated by one blank space. The number A is the year, B is the consume of A year. The number C is another year and number D is the consume of B year.
Output
For each test case, it should be printed the growing up rate with only two decimal places, separated by comma and truncated - no rounding.
Sample Input | Sample Output |
1 |
7809,66 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <math.h>
double pa(double, double, unsigned short);
int main (void)
{
unsigned short casos;
unsigned short anoIni, anoFim;
double consumoIni, consumoFim, resultado, fracao;
int inteiro;
scanf("%hu", &casos);
while (casos--)
{
// O problema da questão é mais a impressão do que o cálculo em si;
// Para imprimir um número decimal com vírgula é necessário uma gambiarra;
// Primeiro separar as partes inteira e fracionaria em variáveis diferentes;
// Depois multiplicar a parte fracionária por 100 para que esta também fique inteira;
// No fim, imprimir os dois resultados separados por uma vírgula no próprio printf;
// Atenção para os 0's à esquerda na parte que era para ser a fracionária;
scanf("%hu %lf %hu %lf", &anoIni, &consumoIni, &anoFim, &consumoFim);
resultado = pa(consumoIni, consumoFim, (anoFim - anoIni));
inteiro = resultado;
fracao = trunc((resultado - inteiro)*100);
printf("%d,%02.f\n", inteiro, fracao);
}
}
double pa(double a1, double an, unsigned short posicao)
{
// Simples caso de PA;
return ((an - a1) / posicao);
}
Copy The Code &
Try With Live Editor
Input
2010 104326 2013 127755
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define MAXV 200100
#define PI 3.14159
#define TWOPI 2 * PI
using namespace std;
typedef vector<int> vi;
typedef pair < int, int> ii;
typedef pair<int, ii> iii;
typedef long long int64;
int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
while (n--) {
double a, b, c, d;
cin >> a >> b >> c >> d;
double ans = trunc(((d - b) / (c - a)) * 100) / 100;
ostringstream buffer;
buffer << fixed << setprecision(2) << ans;
string pA = buffer.str();
pA[pA.size() - 3] = ',';
cout << pA << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
2010 104326 2013 127755
Output
#3 Code Example with Python Programming
Code -
Python Programming
casos = int(input())
for caso in range(casos):
a, b, c, d = [int(i) for i in input().split()]
exibir = "%.3lf" % ((d - b) / float(c - a))
novoexbir = [i for i in exibir]
novoexbir.pop()
print("".join(novoexbir).replace(".", ","))
Copy The Code &
Try With Live Editor
Input
2010 104326 2013 127755
Output