【发布时间】:2015-07-16 15:39:23
【问题描述】:
有一个名为 URI 的网站有很多问题需要你解决,我正在解决这个问题,但显然发生了一些事情,我的程序输入错误,我不知道为什么。这是 URI 问题:
Maria has just started as graduate student in a medical school and she's needing
your help to organize a laboratory experiment which she is responsible about.
She wants to know, at the end of the year, how many animals were used in this
laboratory and the percentage of each type of animal is used at all.
This laboratory uses in particular three types of animals: frogs, rats and
rabbits. To obtain this information, it knows exactly the number of experiments
that were performed, the type and quantity of each animal is used in each
experiment.
Input
The first line of input contains an integer N indicating the number of test
cases that follows. Each test case contains an integer Amount (1 ≤ Amount ≤ 15)
which represents the amount of animal used and a character Type ('C', 'R' or
'S'), indicating the type of animal:
- C: Coelho (rabbit in portuguese)
- R: Rato (rat in portuguese)
- S: Sapo (frog in portuguese)
Output
Print the total of animals used, the total of each type of animal and the
percentual of each one in relation of the total of animals used. The percentual
must be printed with 2 digits after the decimal point.
Input example:
10
10 C
6 R
15 S
5 C
14 R
9 C
6 R
8 S
5 C
14 R
Output example:
Total: 92 cobaias
Total de coelhos: 29
Total de ratos: 40
Total de sapos: 23
Percentual de coelhos: 31.52 %
Percentual de ratos: 43.48 %
Percentual de sapos: 25.00 %
这是我的代码:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <iomanip>
#include <stdio.h>
using namespace std;
int main() {
/**
* Escreva a sua solução aqui
* Code your solution here
* Escriba su solución aquí
*/
char input[5],num_char[2];
int coelho = 0, rato = 0, sapo = 0,op, num, index;
cin >> op;//Get how many time the loop will happen
for(int i=0;i<op;i++){
index = 0;
fflush(stdin);
cin.getline(input,4);
//Copy the number part of the input to the num_char[] and converts it to int
while(input[index] != ' '){
num_char[index] = input[index];
index++;
}
num = atoi(num_char);
//Get what animal is being tested
if(input[index+1] == 'R') rato +=num;
else if(input[index+1] == 'C') coelho +=num;
else if(input[index+1] == 'S') sapo +=num;
}
//Print output
cout << fixed << setprecision(2);
cout << "Total: " << rato+sapo+coelho << " cobaias" << endl;
cout << "Total de coelhos: " << coelho << endl;
cout << "Total de ratos: " << rato << endl;
cout << "Total de sapos: " << sapo << endl;
cout << "Percentual de coelhos: " << coelho*100.0/(rato+sapo+coelho) << "%" << endl;
cout << "Percentual de ratos: " << rato*100.0/(rato+sapo+coelho) << "%" << endl;
cout << "Percentual de sapos: " << sapo*100.0/(rato+sapo+coelho) << "%" << endl;
return 0;
}
这是问题的网页: https://www.urionlinejudge.com.br/judge/en/problems/view/1094
【问题讨论】:
-
首先,不要在
stdin上调用fflush,这在技术上是未定义的行为。其次,您实际上并不需要getline(既不是成员函数也不是独立函数),因为所有数据行只包含一个整数和一个字符,您可以使用普通输入运算符>>。 -
不是 cin >> 字符串容易受到缓冲区溢出的影响吗?我被教导要尽可能避免使用 >> 运算符。而且 fflush(stdin) 消除了内存垃圾可能遇到的许多问题。至少我大学第一年他们就是这么教的
-
为什么要读取字符串?输入包含 整数 和 字符(但在读取字符时要小心,不要读取数字和字母之间的空格)。此外,如果读入
std::string,它将根据需要增加其大小。