【1108】Finding Average (20分)【字符串处理】

简介: 【1108】Finding Average (20分)【字符串处理】【1108】Finding Average (20分)【字符串处理】
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
#include<string>
using namespace std;  
//利用ssprintf从字符串中读取相应格式的字符串
int main(){   
  int n,cnt=0;
  char a[50],b[50];
  double temp,sum=0.0;
  cin>>n;
  for(int i=0;i<n;i++){
    scanf("%s",a);
    sscanf(a,"%lf",&temp);
    sprintf(b,"%.2f",temp);
    int flag=0;
    for(int j=0;j<strlen(a);j++)
      if(a[j] != b[j])  flag=1;
    if(flag || temp< -1000 || temp>1000){
      printf("ERROR: %s is not a legal number\n",a);
      //continue;
    }else{
      sum += temp;
      cnt++;
    }
  }
  if(cnt ==1)
    printf("The average of 1 number is %.2f",sum);
  else if(cnt>1)
    printf("The average of %d numbers is %.2f",cnt,sum/cnt);
  else 
    printf("The average of 0 numbers is Undefined");
  system("pause");
    return 0;   
}
相关文章
|
C++
【PAT甲级 - C++题解】1145 Hashing - Average Search Time
【PAT甲级 - C++题解】1145 Hashing - Average Search Time
83 0
|
机器学习/深度学习 数据挖掘 Python
使用Python实现Hull Moving Average (HMA)
赫尔移动平均线(Hull Moving Average,简称HMA)是一种技术指标,于2005年由Alan Hull开发。它是一种移动平均线,利用加权计算来减少滞后并提高准确性。
411 0
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode Contest 186 5392. 分割字符串的最大得分 Maximum Score After Splitting a String
LeetCode Contest 186 5392. 分割字符串的最大得分 Maximum Score After Splitting a String
PAT (Basic Level) Practice (中文) 1010 一元多项式求导 (25 分)
PAT (Basic Level) Practice (中文) 1010 一元多项式求导 (25 分)
102 0
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
105 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
126 0
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
110 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
114 0
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
112 0