1108. Finding Average (20)

痛定思痛。 2022-05-28 07:42 221阅读 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

题目大意:

代码:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int main()
  5. {
  6. int i,j,n,m,k,t,l,p,x,num=0,digit=0;
  7. char str[100];
  8. double sum=0;
  9. scanf("%d",&n);
  10. for(i=0;i<n;i++)
  11. {
  12. scanf("%s",str);
  13. l=strlen(str);
  14. k=0;
  15. t=0;
  16. p=0;
  17. for(j=0;j<l;j++)
  18. {
  19. if(j==0)
  20. {
  21. if(str[j]=='-'||str[j]=='+')
  22. {
  23. k++;
  24. }
  25. else if(str[j]=='.'||(str[j]>='0'&&str[j]<='9'))
  26. {
  27. k++;
  28. if(str[j]=='.')
  29. {
  30. p++;
  31. for(x=j;x<l;x++)
  32. {
  33. if(str[x]>='0'&&str[x]<='9')
  34. {
  35. t++;
  36. digit++;
  37. }
  38. }
  39. break;
  40. }
  41. else
  42. {
  43. digit++;
  44. }
  45. }
  46. }
  47. else
  48. {
  49. if(str[j]>='0'&&str[j]<='9')
  50. {
  51. k++;
  52. digit++;
  53. }
  54. else if(str[j]=='.')
  55. {
  56. p++;
  57. k++;
  58. for(x=j;x<l;x++)
  59. {
  60. if(str[x]>='0'&&str[x]<='9')
  61. {
  62. t++;
  63. digit++;
  64. }
  65. }
  66. break;
  67. }
  68. }
  69. }
  70. if(p<=1&&k+t==l&&digit!=0)
  71. {
  72. double tmp=atof(str);
  73. if(tmp>=-1000&&tmp<=1000&&t<=2)
  74. {
  75. sum=sum+tmp;
  76. num++;
  77. }
  78. else
  79. {
  80. printf("ERROR: %s is not a legal number\n",str);
  81. }
  82. }
  83. else
  84. {
  85. printf("ERROR: %s is not a legal number\n",str);
  86. }
  87. }
  88. if(num==1)
  89. printf("The average of 1 number is %.2lf\n",sum);
  90. else if(num>1)
  91. printf("The average of %d numbers is %.2lf\n",num,sum/num);
  92. else
  93. printf("The average of 0 numbers is Undefined\n");
  94. return 0;
  95. }

发表评论

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

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

相关阅读

    相关 uva 1451——Average

    题意:给定一个长度为n 的01串,然后选一个长度至少为L的子串,使得子串的平均值最大。 思路:单调队列。如果把所有的前缀和都求出来,那么所求即为max((s\[j\