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() – 从一个字符串中读进与指定格式相符的数据
sprintf() – 字符串格式化命令,主要功能是把格式化的数据写入某个字符串中

C++:

#include <iostream>
#include <string.h>
using namespace std;
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, "%.2lf",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 %.2lf", sum);
	} else if(cnt > 1) {
		printf("The average of %d numbers is %.2lf", cnt, sum / cnt);
	} else {
		printf("The average of 0 numbers is Undefined");
	}
	return 0;
}

 

function TDSR %This function simulate the concepts of Dynamic Source Routing %The function finds path from source node(node1) to destination node(node10) %The output of this function is the figure displaying network topology and %the selected path from source to destination,the average trust value of %the selected path and the number of hops clear; noOfNodes =10; figure(1); clf; hold on; R =5; % node transmission range sor =1;%source node des =10;%destination node X = [1 2 3 4 8 6 7 9 10 10];%nodes' x coordinates Y = [6 2 5 8 5 1 10 2 8 5];%nodes' y coordinates Z =[1 1 0.7 0.4 0.1 0.1 0.1 1 1 1];%nodes' trust values %plotting network topology for i = 1:noOfNodes plot(X(i), Y(i), '.'); text(X(i), Y(i), num2str(i)); for j = 1:noOfNodes distance = sqrt((X(i) - X(j))^2 + (Y(i) - Y(j))^2); if distance <= R % there is a link; matrix(i, j) =1; trust(i,j)=1-((Z(i)+Z(j))/2); line([X(i) X(j)], [Y(i) Y(j)], 'LineStyle', ':'); matriz(i,j)=distance; else matrix(i, j) =inf; trust(i,j)= inf; matriz(i,j)=inf; end end end [path, cost] = dijkstra(sor,des,trust);%finding the path from source to destination trusted_path=path; trusted_path_trust=1-cost; trusted_path_hops=length(path)-1; trusted_path_distance=0; for d=2:length(path) trusted_path_distance= trusted_path_distance + matriz(path(d-1),path(d)); end trusted_path_distance; %plotting the selected path for p =1:(length(path)-1) line([X(sor) X(path(1))],[Y(sor) Y(path(1))],'Color','r','LineWidth', 1) 'LineStyle'; '-'; line([X(path(p)) X(path(p+1))], [Y(path(p)) Y(path(p+1))]) 'Color','r','LineWidth'; 1; 'LineStyle';'-' end grid hold on return;
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值