(PAT 1108) Finding Average (字符串处理)

蔚落 2022-03-19 03:58 220阅读 0赞

The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−1000,1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then N numbers are given in the next line, separated by one space.

Output Specification:

For each illegal input number, print in a line ERROR: X is not a legal number where X is the input. Then finally print in a line the result: The average of K numbers is Y where K is the number of legal inputs and Y is their average, accurate to 2 decimal places. In case the average cannot be calculated, output Undefined instead of Y. In case K is only 1, output The average of 1 number is Y instead.

Sample Input 1:

  1. 7
  2. 5 -3.2 aaa 9999 2.3.4 7.123 2.35

Sample Output 1:

  1. ERROR: aaa is not a legal number
  2. ERROR: 9999 is not a legal number
  3. ERROR: 2.3.4 is not a legal number
  4. ERROR: 7.123 is not a legal number
  5. The average of 3 numbers is 1.38

Sample Input 2:

  1. 2
  2. aaa -9999

Sample Output 2:

  1. ERROR: aaa is not a legal number
  2. ERROR: -9999 is not a legal number
  3. The average of 0 numbers is Undefined

解题思路:

直接无脑if else就行了,这里的坑点是要仔细读题,注意最后的number和numbers!

关于string转float 用stof就可以了

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. using namespace std;
  5. int N;
  6. int main() {
  7. cin >> N;
  8. string tnum;
  9. float sum = 0.0;
  10. int nn = 0;
  11. for (int i = 0; i < N; ++i) {
  12. int countpoint = 0;
  13. bool countpointDug = true;
  14. bool islegal = true;
  15. int numbernum = 0;
  16. int countnumber = 0;
  17. bool zeroflag = true;
  18. cin >> tnum;
  19. for (int j = 0; j < tnum.length(); ++j) {
  20. //判断负号
  21. if (tnum[j] == '-') {
  22. if (j == 0) {
  23. continue;
  24. }
  25. else {
  26. islegal = false;
  27. break;
  28. }
  29. }
  30. //判断小数点
  31. if (tnum[j] == '.') {
  32. countpoint++;
  33. if (countpoint > 1) {
  34. islegal = false;
  35. break;
  36. }
  37. continue;
  38. }
  39. //判断是否是字符
  40. if (tnum[j] >= 0 + '0' && tnum[j] <= 9 + '0') {
  41. numbernum++;
  42. if (countpoint == 1) countnumber++;
  43. if (countpoint == 0 && numbernum > 4) {
  44. islegal = false;
  45. break;
  46. }
  47. if (countpoint == 1 && countnumber > 2) {
  48. islegal = false;
  49. break;
  50. }
  51. }
  52. else {
  53. islegal = false;
  54. break;
  55. }
  56. }
  57. if (!islegal) {
  58. cout << "ERROR: "<<tnum<<" is not a legal number" << endl;
  59. }
  60. else {
  61. float dres = stof(tnum);
  62. if(dres<-1000 || dres > 1000) cout << "ERROR: " << tnum << " is not a legal number" << endl;
  63. else {
  64. sum += dres;
  65. nn++;
  66. }
  67. }
  68. }
  69. if (nn == 0) {
  70. printf("The average of 0 numbers is Undefined\n");
  71. }
  72. else if (nn == 1) {
  73. printf("The average of 1 number is %.2f\n",sum); //坑点在这里!!请仔细读题
  74. }
  75. else{
  76. printf("The average of %d numbers is %.2f", nn, sum / nn);
  77. }
  78. return 0;
  79. }

发表评论

表情:
评论列表 (有 0 条评论,220人围观)

还没有评论,来说两句吧...

相关阅读