1108. Finding Average (20)

本文介绍了一种算法,用于从一组输入中筛选出合法的数值(位于-1000到1000之间且精确到小数点后两位),并计算这些合法数值的平均值。文章还提供了非法输入的错误提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

参照别人代码 :

http://blog.youkuaiyun.com/wanmeiwushang/article/details/51586162        

https://www.liuchuo.net/archives/617

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、付费专栏及课程。

余额充值