1108. Finding Average (20)

  • 题目见这里

  • 20分的题目,怎么我花的时间比30分的都长,渣渣如我,都快弄不清正式考试的时候是先做20分的题目还是先做30分的题目呢,QAQ。

  • 言归正传,这其实是一个普通的字符串处理题目,和PAT(Advanced Level)其他题目一样,并没有什么实质的难度,主要注意下面5个细节:
    1). 合法字符串最长为8位(-1000.00)
    2). 只有一个符号的判断
    3). 整数位(>2位时)是否有前缀0
    4). 小数位不大于2
    5). 确定小数部分后,对小数部分的特殊处理(如:2.3.)
#include <stdio.h>
#include <string.h>
#define LEN 55
#define ILLEGAL 1001

double Resolve(char *num){ //QAQ:竟然一开始返回int 
    double res;
    int i,len,mul,flag;
    char sign = 'U'; //未知 
    len = strlen(num);
    if(len>8) return ILLEGAL; //-1000.00
    res = 0.0;
    if(num[0]=='+' || num[0]=='-') sign = num[0];
    else if(num[0]>='0' && num[0]<='9') res = num[0]-'0';
    else return ILLEGAL;
    if(sign!='U' && len==1) return ILLEGAL; //只有一个符号 
    mul=10,flag=0; 
    for(i=1;i<len;i++){
        //此处加flag是处理形如:2.3.4.555这样的数据
        //将第一个逗号后的字符全都丢给小数部分处理 
        if(!flag && num[i]=='.'){ 
            flag = 1;
            continue;
        } 
        if(!flag){ //处理整数位 
            if(num[i]>='0' && num[i]<='9'){
                if(i<=2){
                    if(i==1 && num[i-1]=='0') break;
                    if(i==2){
                        if(sign!='U' && num[i-1]=='0') break;//无符号时在i=1时已经判断合格 
                    }
                }
                res = 10*res+(num[i]-'0');
            }   
            else break;
        }
        else{ //处理小数位 
            if(num[i]>='0' && num[i]<='9'){
                if(mul==1000) //小数位>2
                    break; 
                res = res+(num[i]-'0')/(double)mul;
                mul *= 10;
            }
            else break;
        }
    }
    if(i<len) return ILLEGAL;
    if(sign=='-') res = -res;
    if(res>=-1000 && res<=1000) return res;
    else return ILLEGAL;
}

int main(){
//  freopen("Data.txt","r",stdin);
    char num[LEN];
    int i,n,rN;
    double res,sum;
    scanf("%d",&n);
    getchar();
    for(i=0,sum=0.0,rN=0;i<n;i++){
        scanf("%s",num);
        res = Resolve(num);
        if(res==ILLEGAL) 
            printf("ERROR: %s is not a legal number\n",num);
        else{
            rN ++;
            sum += res;
        } 
    } 
    if(rN==0) printf("The average of 0 numbers is Undefined\n");
    else if(rN==1) printf("The average of 1 number is %.2lf\n",sum);
    else printf("The average of %d numbers is %.2lf\n",rN,sum/rN);
    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、付费专栏及课程。

余额充值