1. 程式人生 > >2018華東師範大學計算機系機試題目程式碼

2018華東師範大學計算機系機試題目程式碼

Problem A

給一個小學生都會算的1位數與1位數運算的代數式,請你求出這個表示式的值。
表示式僅含+-*/四種運算,題目保證0不為除數。

Sample Input 1:

1+1

Sample Output 1:

2

Sample Input 2:

3*4

Sample OutPut2:

12

C語言解答

#include <stdio.h>

int main(void)
{   
    char op,n1,n2;
    int res;
    n1=getchar();
    op=getchar();
    n2=getchar();
    switch
(op) { case '+': res=(n1-'0')+(n2-'0'); break; case '-': res=(n1-'0')-(n2-'0'); break; case '*': res=(n1-'0')*(n2-'0'); break; case '/': res=(n1-'0')/(n2-'0'); break; default
: break; } printf("%d\n",res); return 0; }

Python解答

expr=input()
print(int(eval(expr)))

Problem B

現在小學生也在學習基本的程式設計,課程目標是讓小學生能夠有基本的演算法思想,並不涉及複雜的資料和實現細節與原理。LOGO語言就非常適合小學生學習,它通過繪圖的方式來直觀的表現出如何用程式程式碼控制事物。例如控制檯上初始給出一個點,使用語句FD 1/1 表示將控制檯上的點Forward 1/1的距離,即,向當前方向移動1的距離,這樣就畫出一條線段。語句LT 60則表示當前朝向向左轉60度,接著再使用語句FD 1/1就畫出一條與之前的直線夾角為120度的一條線段,這時控制檯上就有繪製出了一條折線段。
現在的任務是輸出一段能繪製分形的LOGO語言的程式程式碼。
如果你還對分形不瞭解,下面我們先介紹一下分形:
分形(Fractal) 是一個幾何形狀可以分成數個部分,且每一部分都(至少近似地)是整體縮小後的形狀,即具有自相似的性質。自然界中一定程度上具有分形的性質的事物有云朵、閃電、植物根系、雪花等等。著名的科赫曲線就是一種分形,它繪製的是形態類似雪花的圖案。
以下是0階到3階的科赫曲線:
科赫曲線


本題的任務只要求畫出科赫曲線的一部分即可,具體要求為:

輸入:
1行,1個數字n,表示圖形的階數(0<n<10)
輸出:
能繪製上述圖形的LOGO程式程式碼

如果你有遞迴的思想,那麼應該不難看出,這個問題就是一個遞迴的形式,我們應該先把題目描述轉化成演算法步驟:
繪製長為x的圖形:
如果x已經不能再分成x/3,就畫出長為x的直線。
先畫長為x/3的圖形,左轉60度,畫出長為x/3的圖形,左轉240度,畫出長為x/3的圖形,左轉60度,畫出長為x/3的圖形,畫完。
這樣就有了程度的基本框架,再把相應的LOGO語言的指令填入,就是此題的答案了。
本題C++和python的程式程式碼不再贅述

C語言解答

#include <stdio.h>
#include <math.h>

void Fractal(int n,int level)
{
    int p=pow(3,n);

    if(level==1)
    {
        printf("FD 1/%d\n",p);
        printf("LD 60\n");
        printf("FD 1/%d\n",p);
        printf("LD 240\n");
        printf("FD 1/%d\n",p);
        printf("LD 60\n");
        printf("FD 1/%d\n",p);
    }
    else
    {
        Fractal(n,level-1);
        printf("LD 60\n");
        Fractal(n,level-1);
        printf("LD 240\n");
        Fractal(n,level-1);
        printf("LD 60\n");
        Fractal(n,level-1);
    }
}

void output(int n)
{
    Fractal(n,n);
}

int main(void)
{
    int n;
    scanf("%d",&n);
    output(n);
    return 0;
}

Problem C

給出一個含有N (0 < N < 200000)個數字的數列,請你對它排序,每個數的範圍均處於[1050,1050]。負數前有負號‘-’,正數前沒有+號,每個數字不含前導0,零用一個0表示。
輸入:
2行,第1行有1個數字N,表示數列中資料的個數
第2行有N個數字,表示待排序的數列,數字間用空格分隔,題目保證每個數字在[1050,1050]範圍內。
輸出:
1行,將N個數字從小到大排序後的結果,數字間用空格分隔。
Sample Input :

5
991 -31 -1 5 -10000000000000000000000000000000000000000000000000

Sample Output:

-10000000000000000000000000000000000000000000000000 -31 -1 5 991

C語言解答

#include <stdio.h>
#include <string.h>

#define MAXDIGIT 57
#define MAXN 200007
typedef struct numNode ElemType;

struct numNode
{
    int isPositive;
    int digit;
    char value[MAXDIGIT];
};
ElemType a[MAXN];

int myCompare(ElemType a,ElemType b);
void sort(ElemType *a,int N);

int main(void)
{
    int i,N;

    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s",a[i].value);
        a[i].digit=strlen(a[i].value);
        if(a[i].value[0]=='-')
            a[i].isPositive=0;
        else
            a[i].isPositive=1;
    }

    sort(a,N);

    for(i=0;i<N;i++)
        printf("%s ",a[i].value);
    printf("\n");
    return 0;

}


int myCompare(ElemType a,ElemType b)
{
    if(a.isPositive && b.isPositive == 1){
        //both of a and b are positive 
        if(a.digit!=b.digit)
            return a.digit<b.digit;
        else
            return strcmp(a.value,b.value)<0;
    }
    else if(a.isPositive==0 && b.isPositive==0){
        if(a.digit!=b.digit)
            return a.digit>b.digit;
        else
            return strcmp(a.value+1,b.value+1)>0;
    }
    else
        return a.isPositive==0;
}


void QuickSort(ElemType *a,int lwbd,int upbd)
{

    int i=lwbd,j=upbd;
    if(i<j){
            ElemType tmp=a[i];
        while(i<j)
        {
            while(i<j && myCompare(tmp,a[j]))
                j--;
            if(i<j)
                a[i++]=a[j];
            while(i<j && myCompare(a[i],tmp))
                i++;
            if(i<j)
                a[j--]=a[i];
        }
        a[i]=tmp;
        QuickSort(a,lwbd,i-1);
        QuickSort(a,i+1,upbd);
    }

}


void sort(ElemType *a,int N)
{
    QuickSort(a,0,N-1);
}

Cpp解答:用到STL庫與運算子過載

#include <cstdio>
#include <cstring>
#include <algorithm>
using std::sort;
const int MAXDIGIT=50;

struct numNode{
    bool isPositive;
    int digit;
    char value[MAXDIGIT+2];

    bool operator<(const numNode &b) const
    {
        if(isPositive==true &&b.isPositive==true)
        {
            if(digit!=b.digit)
                return digit<b.digit;
            else
                return strcmp(value,b.value)<0;
        }
        else if( isPositive== false && b.isPositive==false)
        {
            if(digit!=b.digit)
                return digit>b.digit;
            else
                return strcmp(value,b.value)>0;
        }
        else
            return b.isPositive;
    }
};

int main(void)
{
    int i,N;
    scanf("%d",&N);
    numNode *s=new numNode [N];
    for(i=0;i<N;i++)
    {
        scanf("%s",s[i].value);
        s[i].digit=strlen(s[i].value);
        if(s[i].value[0]=='-')
            s[i].isPositive=false;
        else
            s[i].isPositive=true;
    }

    sort(s,s+N);

    for (i=0;i<N;i++)
        printf("%s ",s[i].value);
    printf("\n");
    delete [] s;
    return 0;

}

Problem D

有一個研究團隊,團隊分成許多研究小組,每個小組的一部分成員可能再分成小組。小組的成員只知道自己的組長是誰,而在同一個組長領導下的成員之間卻相互不認識。現在這個團隊希望有一個程式能統計一下各組長帶領小組的規模,即對每一個成員想知道自己及自己帶領下的小組有多少人。
輸入:
2行,第1行有1個數字N(0<N<2×105),代表小組的人數
第2行有N個數a1,a2,...,ai,...,aN,表示第i個人的領導是ai。團隊的領導用0表示,說明沒有人做他的組長。資料保證沒有環路。單獨的一個成員視為1個人的小組。
輸出:
1行,N個數字,表示第i名成員的團隊的規模
Sample Input:

0 1 2 1 2 2

Sample Output:

6 4 1 1 1 1

C語言:

待續

Problem E

所謂“螺旋矩陣”,是指從左上角第1個格子開始,按順時針螺旋方向從外圈向內逐個填充。給出一個數字N,將1至N^2填入一個N行N列的螺旋矩陣。
例如當N=4時,螺旋矩陣為

   1   2   3   4
  12  13  14   5
  11  16  15   6
  10   9   8   7

當N=5時,螺旋矩陣為

   1   2   3   4   5
  16  17  18  19   6
  15  24  25  20   7
  14  23  22  21   8
  13  12  11  10   9

我現在想知道每一行的蛇形矩陣的和,希望你能通過編寫程式求解。

輸入:
1行,1個數字N (1<N<2×105)
輸出:
N行,第i行表示蛇矩陣第i行的總和。

直接法:列出矩陣進行加和

不能通過所有測試,只能得出小資料的結果

#include <stdio.h>
#define DEBUG 1
#define MAXN 100 
// if MAXN reach the upperbound 2e+5, it must beyond the memory limit

int N;
/*
** N is the order of matrix
**
** all of the function will use it
**
** therefore, we defined N as globle variable
*/

int valid(int x,int y)
{
    if(x>=0 && x<N & y>=0 && y<N)
        return 1;
    else 
        return 0;
}

typedef long long resType;
resType accumulate(resType *a)
{
    int i;
    resType s=0;
    for (i=0;i<N;i++)
        s+=a[i];
    return s;
}

int main(void)
{

    int i,k,x,y;

    resType a[MAXN][MAXN];

    for(x=0;x<MAXN;x++)
        for(y=0;y<MAXN;y++)
            a[x][y]=0;

    scanf("%d",&N);

    int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    a[0][0]=1;

    k=0;x=0;y=0;
    for(i=2;i<=N*N;i++)
    {
        while(!(valid(x+dir[k][0],y+dir[k][1]) && a[x+dir[k][0]][y+dir[k][1]]==0))
            k=(k+1)%4;
        x=x+dir[k][0];
        y=y+dir[k][1];
        a[x][y]=i;
    }

#if DEBUG
// output the matrix
    for(x=0;x<N;x++){
        for(y=0;y<N;y++)
            printf("%4d",a[x][y]);
        printf("\n");
    }
#endif

    for(i=0;i<N;i++)
        printf("%d\n",accumulate(a[i]));

    return 0;


}

找到數學規律可覆蓋所有範圍,C++解答

#include <stdio.h>
int main(void)
{
    int i,N;
    long long spl,currD;
    const int deepD=8;
    scanf("%d",&N);

    spl=N*(N+1)/2;
    currD=4*(N-1)-1;
    printf("%lld\n",spl);
    for(i=N-1;i>0;i-=2)
    {
        spl+=currD*i+1;
        printf("%lld\n",spl);
        currD-=deepD;
    }
    currD=currD+deepD-10;

    if(N%2==1)
    {
        for(i=1;i<N;i+=2)
        {
            spl+=currD*i;
            printf("%lld\n", spl);
            currD-=deepD;
        }
    }
    else
    {
        for(i=2;i<N;i+=2)
        {
            spl+=currD*i;
            printf("%lld\n", spl);
            currD-=deepD;
        }
    }

    return 0;
}

python解答

N=int(input())
spl=int(N*(N+1)/2)  #sum per line
currD=4*(N-1)-1     #current distance
deepD=8             #2_order distance
print(spl)          #sum of the 1st line
for i in range(N-1,0,-2):
    spl+=currD*i+1      
    print(spl)
    currD-=deepD
currD=currD+deepD-10

if(N%2==1):
    for i in range(1,N,2):
        spl+=currD*i
        print(spl)
        currD-=deepD
else:
    for i in range(2,N,2):
        spl+=currD*i
        print(spl)
        currD-=deepD