日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

华师大c语言作业答案,2018华东师范大学计算机系机试题目代码

發布時間:2024/4/17 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 华师大c语言作业答案,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

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

輸出:

能繪制上述圖形的LOGO程序代碼

如果你有遞歸的思想,那么應該不難看出,這個問題就是一個遞歸的形式,我們應該先把題目描述轉化成算法步驟:

繪制長為x的圖形:

如果x已經不能再分成x/3,就畫出長為x的直線。

先畫長為x/3的圖形,左轉60度,畫出長為x/3的圖形,左轉240度,畫出長為x/3的圖形,左轉60度,畫出長為x/3的圖形,畫完。

這樣就有了程度的基本框架,再把相應的LOGO語言的指令填入,就是此題的答案了。

本題C++和python的程序代碼不再贅述

C語言解答

#include

#include

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] [ ? 10 50 , 10 50 ]。負數前有負號‘-’,正數前沒有+號,每個數字不含前導0,零用一個0表示。

輸入:

2行,第1行有1個數字N,表示數列中數據的個數

第2行有N個數字,表示待排序的數列,數字間用空格分隔,題目保證每個數字在[?1050,1050] [ ? 10 50 , 10 50 ]范圍內。

輸出:

1行,將N個數字從小到大排序后的結果,數字間用空格分隔。

Sample Input :

5

991 -31 -1 5 -10000000000000000000000000000000000000000000000000

Sample Output:

-10000000000000000000000000000000000000000000000000 -31 -1 5 991

C語言解答

#include

#include

#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"%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"%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.digitelse

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(iwhile(iwhile(iif(iwhile(iif(i1);

QuickSort(a,i+1,upbd);

}

}

void sort(ElemType *a,int N)

{

QuickSort(a,0,N-1);

}

Cpp解答:用到STL庫與運算符重載

#include

#include

#include

using std::sort;

const int MAXDIGIT=50;

struct numNode{

bool isPositive;

int digit;

char value[MAXDIGIT+2];

bool operatorconst numNode &b) const

{

if(isPositive==true &&b.isPositive==true)

{

if(digit!=b.digit)

return digitelse

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;iscanf("%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;iprintf("%s ",s[i].value);

printf("\n");

delete [] s;

return 0;

}

Problem D

有一個研究團隊,團隊分成許多研究小組,每個小組的一部分成員可能再分成小組。小組的成員只知道自己的組長是誰,而在同一個組長領導下的成員之間卻相互不認識。現在這個團隊希望有一個程序能統計一下各組長帶領小組的規模,即對每一個成員想知道自己及自己帶領下的小組有多少人。

輸入:

2行,第1行有1個數字N(0

第2行有N個數a1,a2,...,ai,...,aN a 1 , a 2 , . . . , a i , . . . , a N,表示第i個人的領導是ai a i。團隊的領導用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行,第i行表示蛇矩陣第i行的總和。

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

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

#include

#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=0 && yreturn 1;

else

return 0;

}

typedef long long resType;

resType accumulate(resType *a)

{

int i;

resType s=0;

for (i=0;ireturn s;

}

int main(void)

{

int i,k,x,y;

resType a[MAXN][MAXN];

for(x=0;xfor(y=0;y0;

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;xfor(y=0;yprintf("%4d",a[x][y]);

printf("\n");

}

#endif

for(i=0;iprintf("%d\n",accumulate(a[i]));

return 0;

}

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

#include

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;i2)

{

spl+=currD*i;

printf("%lld\n", spl);

currD-=deepD;

}

}

else

{

for(i=2;i2)

{

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

總結

以上是生活随笔為你收集整理的华师大c语言作业答案,2018华东师范大学计算机系机试题目代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。