Data structure exercise set assignment code (Chapter 1)

LauJiYeoung 2022-05-22 12:06:19 阅读数:394

datastructureexercisesetassignment

1.16- Three integer sorting

Description
Write an algorithm , Output three integers read in sequence from arrogant to small ,x,y and z Value
Input
Input x,y and z
Output
Output to small sort

#include<stdio.h>
void swap(int *x,int *y){

int t=*x;
*x=*y;
*y=t;
}
int main(){

int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if(x<y)swap(&x,&y);
if(y<z)swap(&y,&z);
if(x<y)swap(&x,&y);
printf("%d %d %d",x,y,z);
return 0;
}

1.17-k Order Fibonacci sequence

Description
It is known that k The first order Fibonacci sequence is defined as
 Insert picture description here
Trial preparation k The second order of the perbonacci sequence m Function algorithm of item value ,k and m Both appear in the function parameter table in the form of value call .
Input
Input is k and m(m from 0 Start , f 0 f_0 f0 Corresponding m=0)
Output
Output No m Item value

#include<stdio.h>
/*int a[100][100],b[100][100],c[100][100],k,m; void copy(int (*a)[100],int (*b)[100]); void operate(int (*a)[100],int (*b)[100]){ int i,j,l; int c[100][100]; for(i=1;i<=k;++i){ for(j=1;j<=k;++j){ c[i][j]=0; } } for(i=1;i<=k;++i){ for(j=1;j<=k;++j){ for(l=1;l<=k;++l){ c[i][j]+=a[i][l]*b[l][j]; } } } copy(a,c); } void init(int (*a)[100]){ int i,j; for(i=1;i<=k;++i){ for(j=1;j<=k;++j){ a[i][j]=0; } } } void copy(int (*a)[100],int (*b)[100]){ int i,j; for(i=1;i<=k;++i){ for(j=1;j<=k;++j){ a[i][j]=b[i][j]; } } } void power(int (*a)[100],int k){ int tmp[100][100],i; init(tmp); for(i=1;i<=k;++i){ tmp[i][i]=1; } while(k){ if(k&1){ operate(tmp,a); } operate(a,a); k>>=1; } copy(a,tmp); } int main(){ scanf("%d%d",&k,&m); if(m<k){ printf("0"); return 0; } else{ if(k==m){ printf("1"); return 0; } } init(a); a[1][k]=1; init(b); int i; for(i=1;i<=k-1;++i){ b[i+1][i]=1; } for(i=1;i<=k;++i){ b[i][k]=1; } power(b,m-k+1); operate(a,b); printf("%d",a[1][k]); return 0; }*/
int a[100000]={
},m,i,j,k;
int main(){

scanf("%d%d",&k,&m);
a[k-1]=1;
for(i=k;i<=m;++i){

for(j=i-k;j<i;++j){

a[i]+=a[j];
}
}
printf("%d",a[m]);
}

1.18- Track and field competition

Description

Suppose there is A,B,C,D,E Five colleges and universities hold track and field competition , The individual scores of all colleges and universities have been stored in the computer , And form a table , The form of each row in the table is

Project name - Gender - School name - achievement

Write algorithms , Process the above form , Take statistics of male students in Colleges and universities 、 Total female score and total group score , And the output

Input
The input contains multiple lines , Each row represents a row in the table , Each field is separated by a space

M On behalf of the boy ,F On behalf of girls

Output
Output each school's male , Total female score and total group score , In the following order

The first line outputs the school A Total score of boys

The second line outputs the school A The total score of girls

The third line outputs the school A Total group score

The fourth line outputs the school B Total score of boys

If there are no boys or girls in a school , Then skip the corresponding number of lines ( Like school A No girls , Then skip the second line )

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char p,g,s;
int score,i;
int vis[5];
int S[6][3];
int main(){

while(scanf("%c ",&p)!=EOF){

scanf("%c %c %d\n",&g,&s,&score);
vis[s-'A']=1;
if(g=='M'){

S[s-'A'][0]+=score;
}
else S[s-'A'][1]+=score;
}
for(i=0;i<5;++i){

if(S[i][0]+S[i][1]==0)continue;
if(S[i][0]!=0){

printf("%c M %d\n",i+'A',S[i][0]);
}
if(S[i][1]!=0){

printf("%c F %d\n",i+'A',S[i][1]);
}
printf("%c %d\n",i+'A',S[i][0]+S[i][1]);
}
return 0;
}

1.20- Find the value of univariate polynomial

Description

Write an algorithm to find univariate polynomial P n ( x 0 ) = ∑ i = 0 n a i x 0 i P_n(x_0)=\sum_{i=0}^{n}a_ix_0^i Pn(x0)=i=0naix0i

Value P n ( x 0 ) P_n(x_0) Pn(x0)

Input
Enter the first behavior x 0 x_0 x0 and n, Space off , n > = 0 n>=0 n>=0
Enter the second line a i ( i = 0 , 1 , . . . , n ) , − 1000 < = a i < = 1000 a_i(i=0,1,...,n),−1000<=a_i<=1000 ai(i=0,1,...,n),1000<=ai<=1000 Space off

Output
Output P n ( x 0 ) P_n(x_0) Pn(x0) Value

#include<stdio.h>
int Quick_Power(int x,int k){

int ret=1;
while(k){

if(k&1){

ret=ret*x;
}
x=x*x;
k>>=1;
}
return ret;
}
int x0,n,i,sum=0;
int main(){

scanf("%d%d",&x0,&n);
for(i=0;i<=n;++i){

int a;
scanf("%d",&a);
sum+=a*Quick_Power(x0,i);
}
printf("%d",sum);
return 0;
}
copyright:author[LauJiYeoung],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/142/202205211805585467.html