1. 程式人生 > >PAT甲級 1108 Finding Average (20 分)字串處理

PAT甲級 1108 Finding Average (20 分)字串處理

1108 Finding Average (20 分)

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:

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

Sample Output 1:

ERROR: aaa is not a legal number
ERROR: 9999 is not a legal number
ERROR: 2.3.4 is not a legal number
ERROR: 7.123 is not a legal number
The average of 3 numbers is 1.38

Sample Input 2:

2
aaa -9999

Sample Output 2:

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

非法數字形式樣例中已經全部給出,注意只有一個合法數字的輸出形式。還有注意sscanf的使用!!!

#include <iostream>
#include <cstdio>
using namespace std;

bool Jundge(string str,double &x){
	int i=0,n1 = 0,n2 = 0;
	if(str[i]=='-')i++;
	for(;i<str.size();i++){
		if(str[i]=='.'){
			n1++;
			if(n1 >= 2)
				return false;
		}
		else if(str[i]>='0' && str[i]<='9'){
			if(n1)n2++;
		}else
			return false;	
	}
	if(n2 > 2)
		return false;
	sscanf(str.c_str(),"%lf",&x);//注意x要取地址
	if(x>=-1000 && x<=1000)
		return true;
	else
		return false;
}

int main()
{
    int n,cnt = 0;
    cin>>n;
    string str;
    double res = 0,x = 0;
    for(int i=0;i<n;i++){
        cin>>str;
        if(Jundge(str,x)){
        	res += x;
        	cnt++;
        }else{
        	printf("ERROR: %s is not a legal number\n",str.c_str());
        }
    }
    if(cnt == 0)
        printf("The average of 0 numbers is Undefined");
    else if(cnt == 1)
        printf("The average of 1 number is %.2f",res);
    else
        printf("The average of %d numbers is %.2f",cnt,res/cnt);

    return 0;
}

相關推薦

PAT甲級 1108 Finding Average 20 字串處理

1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes i

【笨方法學PAT1108 Finding Average 20

一、題目 The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that

pat 1108 Finding Average20

tst puts ram final aaa alc ont pri const 1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed t

PAT (Advanced Level) Practice 1108 Finding Average 20

#include<cstdio> #include<algorithm> #include<cstring> using namespace std; char s[100]; int len;double x; int check() {

1108 Finding Average 20

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

PAT A1108 Finding Average20

總結: 1.最坑的就是 printf("The average of %d number is %.2f", pp.size(), ps);當只有一個合法數字的時候輸出number 2. 程式碼: #include<iostream> #include<v

pat甲級 1152 Google Recruitment 20

In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the picture below) for recruitment. The content is super-sim

PAT甲級 1073 Scientific Notation 20 String的使用

1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very large numbers or very small numbers. Th

PAT甲級】1008 Elevator 20

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which flo

PAT 甲級 1041 Be Unique 20

1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule o

1035 Password 20 字串處理水題

題目 To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing

1061 Dating 20 (字串處理)

1061 Dating (20 分) Sherlock Holmes received a note with some strange strings: Let’s date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&

PTA 說反話-加強版20 (字串處理

給定一句英語,要求你編寫程式,將句中所有單詞的順序顛倒輸出。 輸入格式: 測試輸入包含一個測試用例,在一行內給出總長度不超過500 000的字串。字串由若干單詞和若干空格組成,其中單詞是由英文字母(大小寫有區分)組成的字串,單詞之間用若干個空格分開。 輸出格式: 每個測試用例的輸出佔一行,輸出倒序

pat 甲級 1108. Finding Average

這個必須寫, 想了我好多天,哼 1108. Finding Average (20) 時間限制 400 ms 記憶體限制 65536 kB 程式碼長度限制 16000 B 判題程式 Standard 作者 CHEN, Yue The ba

PAT】B1082 射擊比賽20

r+ 射擊 spa sca code ace algorithm ret pat 水提水題,直接貼代碼啦 #include<cstdio> #include<algorithm> using namespace std; struct ppp{

PAT】B1044 火星數字20

字符 == 火星文 names stdio.h ace %d algorithm main /* 火星文有兩位,第二位為0不輸出 */ #include<stdio.h> #include<algorithm> #include<str

PAT】B1043 輸出PATest20

test space using names break namespace turn int char /* */ #include<stdio.h> #include<algorithm> #include<string.h>

PAT】B1009 說反話20

() sca return ++ code const max pre pat #include<cstdio> #include<cstring> const int maxn=90; int main() { int num=0;

PAT】B1012 數字分類20

數字 include sca pat ont 輸入 pri amp print #include<stdio.h> int arr[1005]; int main(){ int N,yu,A1,A2,flag2=1,A3,A4,A4a,A5,A5a=0;

pat 1011 World Cup Betting20

test case sample nes run nta rip ams sin 2.0 1011 World Cup Betting(20 分) With the 2010 FIFA World Cup running, football fans the worl