北郵機試真題2009

題目1

求平均分node

輸入:兩行

第一行是分數個數n
第二行是這n個分數,以空格隔開編碼

輸出:

去掉最高分與最低分後的平均成績spa

 

#include<stdio.h>

float buf[1001];

void fun(int n){
    for(int i=0;i<n;i++){
        
        for(int j=0;j<n-i-1;j++){
            if(buf[j+1]<buf[j]){
                float t=buf[j+1];
                buf[j+1]=buf[j];
                buf[j]=t;
            }
        }
    }
}

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        if(n==0)break;
        
        for(int i=0;i<n;i++){
            scanf("%f",&buf[i]);
        }
        
        fun(n);
        
        float ans=0;
        
        for(int i=1;i<n-1;i++)
            ans+=buf[i];
        ans=ans/(n-2);
        
        printf("%.2f\n",ans);
    }
    return 0;
}

題目2

密碼

輸入:一行


第一行:26個小寫字母 以空格隔開 如 b a c e u f g h i j k l m n o p q r s t v w x y z d................. v y z r s q
第二行:一個字符串(大寫字母)  如: BUPTZcode

輸出:

B用第一行的第二個字母替換,U用第21個替換,P用第('A'-'P'+1)個替換..blog

#include<stdio.h>

char buf1[101];
char buf2[101];

int main(){
    
    char c;
    
    while(scanf("%c",&c)!=EOF){
        int j=1;
        if(c=='\n')scanf("%c",&c);//上次大循環結束c會讀入這個符號 
        while(c!='\n'){
        
            if(c>='a'&&c<='z'&&j<=26)buf1[j++]=c;
            scanf("%c",&c);
        
        }
    
        scanf("%s",buf2);
    
        for(int i=0;buf2[i]!=0;i++){
            int t=buf2[i]-'A'+1;
            buf2[i]=buf1[t];
    
        }
        printf("%s\n",buf2);
    }

return 0;
    } 

題目3

排序排序

輸入:四行

第一行:個數N1(2 3 4 5 6)
第二行:N1個數,升序排列(序列a)
第三行:個數N2(4 5 6 8 9)
第四行:N2個數,升序排列(序列b)字符串

輸出:N2行

2
3
4
5
5
第i行是,a中小於 b的第i個數 的數的個數it

#include<stdio.h>

int a[1001];
int b[1001];



int main(){
    int n1,n2;
    while(scanf("%d",&n1)!=EOF){
        for(int i=0;i<n1;i++){
            scanf("%d",&a[i]);
        }
        scanf("%d",&n2);
        for(int i=0;i<n2;i++){
            scanf("%d",&b[i]);
        }    
    int ans[n2];
    int i,j,t;
    for(i=0,j=0,t=0;i<n1&&j<n2;){
        
        if(a[i]<b[j]){
            t++;
            i++;
            for(int j1=j;j1<n2;j1++)ans[j1]=t;
        }

        else{
            
            j++;
        }
    }


    for(int i=0;i<n2;i++)
        printf("%d\n",ans[i]);
    }
    return 0;
} 

 題目4

哈夫曼編碼中  平均碼長=碼長×碼字出現的機率io

如:ABCDE 五個字符的出現次數分別爲50 20 5 10 15class

那麼,其哈夫曼編碼爲A:0   B:10   C:1110   D:1111   E:110

該哈夫曼編碼的平均碼長=(50*1+20*2+5*4+10*4+15*3)/100=1.95

輸入

有多組輸入,每組兩行

第一行:字符的個數 N

第二行:N 個以空格隔開的數,表示這 N 個字符中每一個字符出現次數

輸出

輸出該哈夫曼編碼的平均碼長,保留兩位小數

樣例輸入

5
50 20 5 10 15

樣例輸出

1.95
思路1
#include<stdio.h>
#include<queue>
using namespace std;


int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        priority_queue<int,vector<int>,greater<int> > Q;
        
        double sum=0;
        for(int i=0;i<n;i++){
            int t;
            scanf("%d",&t);
            sum+=t;
            Q.push(t);
            
        }
        
        double ans=0;
        
        while(Q.size()!=1){
            int t1,t2;
            t1=Q.top();
            Q.pop();
            t2=Q.top();
            Q.pop();
            ans+=t1+t2;
            Q.push(t1+t2);
            
        }
        

        printf("%.2lf\n",ans/sum);
        
    }
    
    return 0;
}

 思路2

#include<stdio.h>
#include<queue>
using namespace std;

struct node{
    int h;
    bool operator < (const node &a) const {
        return h>a.h;
    }
};

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        priority_queue<node> Q;
        
        double sum=0;
        for(int i=0;i<n;i++){
            node t;
            scanf("%d",&t.h);
            sum+=t.h;
            Q.push(t);
            
        }
        
        double ans=0;
        
        while(Q.size()!=1){
            int t1,t2;
            node tmp;
            t1=Q.top().h;
            Q.pop();
            t2=Q.top().h;
            Q.pop();
            ans+=t1+t2;
            tmp.h=t1+t2;
            Q.push(tmp);
            
        }
        

        printf("%.2lf\n",ans/sum);
        
    }
    
    return 0;
}
相關文章
相關標籤/搜索