2019 Southwest Jiaotong University C language test

Posted by anser316 on Fri, 06 Mar 2020 12:26:27 +0100

1. Input an integer n, and then input n groups of subscript sequence number and string in turn. It is required to delete the characters corresponding to the sequence number subscript of the string and output them in turn.

Standard input:

2

1

abcdef

3

ABCDEF

Standard output:

acdef

ABCEF

#include<stdio,h>
int main(){
    int n,index[100],i,j;
    char str[100][100];
    
    scanf("%d",&n);
    for(i=0;i<n;i++){
         scanf("%d",&index[100]);
         getchar();
        gets(str[i]);
   }
   for(i=0;i<n;i++){
       j=index[i];
       while(str[i][j]!='\0'){
           str[i][j]=str[i][++j];
       }
       puts(str[i]);
       printf("\n");
   }
}

2. Define a floating-point array with a capacity of 10. It is required to output the average value of these 10 numbers, and then arrange the elements larger than the average value in the first place in the original order, and the elements smaller than the average value in the last place in the original order (the result is reserved for one decimal place).

Standard input:

1 2 3 4 5 6 7 8 9 10

Standard output:

5.500000

6.0 7.0 8.0 9.0 10.0 1.0 2.0 3.0 4.0 5.0 

#include<stdio.h>
int main(){
    float num[10],ave=0.0,res[10];
    int i,j=0,k=4;
    for(i=0;i<10;i++){
        scanf("%d",&num[i]);
        ave+=num[i];
    }
    ave/=10;
    printf("%f\n",ave);
    for(i=0;i<10;i++){
        if(num[i]>ave){
            res[j++]=num[i];
        }else{
            res[k++]=num[i];
        }
    }
    res[k]='\0';
    puts(res);
    return 0;
}

3. Enter three 3-bit integers, and then output a new number. Its hundreds are the hundreds of the first number, its tens are the tens of the second number, and its bits are the ones of the third number.

Standard input:

234 567 789

Standard output:

269

#include<stdio.h>
int main(){
    int a,b,c;
    int d,e,f;
    scanf("%d %d %d",&a,&b,&c);
    d=a/100;
    e=(b%100)/10;
    f=c%10;
    printf("%d%d%d",d,e,f);
}

4. Input An integer n, calculate and output the general term of the sequence An: A1=1, A2=1 / (1+A1),..., An=1 / (1+An-1)

Standard input:

10

Standard output:

0.617977

#include<stdio.h>
int main(){
    double A=1.0;
    int i,n;
    scanf("%d",&n);
    for(i=1;i<n;i++){
        A=1/(1+A);
    }
    printf("%.7lf",A);
    return 0;
}

5. Input a long integer, calculate and output the product of its digits

Standard input:

123456

Standard output:

720

#include<stdio.h>
int main(){
    long n,result;
    scanf("%ld",&n);
    while(n){
        result *= n%10;
        n /= 10;
    }
    printf("%ld",result);
    return 0;
}

6.

7.

8. Input three integers and output them in order of small to large

Standard input:

23 22 27

Standard output:

22 23 27

#include<stdio.h>
int main(){
    int a,b,c,t;
    scanf("%d %d %d",&a,&b,&c);
    if(a>b){
        t=a;
        a=b;
        b=t;
    }
    if(b>c){
        t=c;
        c=b;
        b=t;
    }
    if(a>c){
        t=a;
        a=c;
        c=t;
    }
    printf("%d %d %d",a,b,c);
    return 0;
}

9. Define and output one-dimensional integer array (input n first, then input n integers), input an integer m, and output a new n-dimensional array. It is required that the elements starting from the subscript m (including m) are put in front of the array in the original order, and the previous m items are put behind the array in the original order.

Standard input:

10

1 2 3 4 5 6 7 8 9 10

7

Standard output:

8 9 10 1 2 3 4 5 6 7

#include<stdio.h>
int reverse(int num[],int start,int end){
    int temp;
    for(;start<end;start++,end--){
        temp=num[start];
        num[start]=num[end];
        num[end]=temp;
    }
}
int main(){
    int n,m,i;
    int num[100];
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d",&num[i]);
    }
    scanf("%d",&m);
    reverse(num,0,m-1);
    reverse(num,m,n-1);
    reverse(num,0,n-1);
    for(i=0;i<n;i++){
        printf("%d ",num[i]);
    }
    return 0;
}

10. Enter a 3-bit integer, invert its number and output

Standard input:

369

Standard output:

963

#include<stdio.h>
int main(){
    int n,a,b,c;
    scanf("%d",&n);
    a=n%10;
    b=(n/10)%10;
    c=n/100;
    printf("%d%d%d",a,b,c);
    return 0;
}

11. Input an integer n, find the value of Sn=1-1/2+1/3-1/4+...+(-1)^(n+1)/n and output

Standard input:

3

Standard output:

0.833333

#include<stdio.h>
int main(){
    int sign=1;
    int i,n;
    double Sn=1;
    scanf("%d",&n);
    for(i=2;i<=n;i++){
        sign*=-1;
        Sn+=1.0/n*sign;
    }
    printf(".6lf",Sn);
    return 0;
}

12. Input a string from the keyboard, convert all its uppercase letters to lowercase, lowercase English letters to uppercase, and output the result (other characters remain unchanged)

Standard input:

12Ad56b%

Standard output:

12aD56B%

#include<stdio.h>
int main(){
    int ch;
    while(ch=getchar()!='\n'){
        if(ch>='A'&&ch<='Z'){
            ch+=32;
        }else if(ch>='a'&&ch<='z'){
            ch-=32;
        }
        printf("%c",ch);
    }
    return 0;
}

13.

14.

15. Input an integer n and output the sum of the first n terms of Fibonacci series

Standard input:

6

Standard output:

20

#include<stdio.h>
int main(){
    int i,n,f1=1,f2=1,f,sum=2;
    scanf("%d",&n);
    if(n==1)
        sum=1;
    else if(n==2)
        sum=2;
    else
        for(i=3;i<=n;i++){
            f=f1+f2;
            f1=f2;
            f2=f;
            sum+=f;
        }
    printf("%d",sum);
    return 0;
}

16. Input the first term a, common ratio q, number of terms n of a sequence, and output the sum of the first n terms.

Standard input:

1 2 10

Standard output:

1023

#include<stdio.h>
#include<math.h>
int main(){
    double a,q,Sn=0;
    int n,i;
    scanf("%lf %lf %d",&a,&q,&n);
    for(i=1;i<=n;i++){
        Sn+=a*pow(q,i-1);
    }
    printf("%.0lf",Sn);
    return 0;
}

17. In the form of a linked list, two strings are connected and output.

Standard input:

123456AB

qwerty789

Standard output:

123456ABqwerty789

#include<stdio.h>
typedef struct node{
    char data;
    struct node *next;
}LinkList;
int main(){
    LinkList *head,*node,*end;
    head=(LinkList*)malloc(sizeof(LinkList));
    end=head;
    while(ch=getchar()!='\n'){
        node=(LinkList*)malloc(sizeof(LinkList));
        node->data=ch;
        end->next=node;
        end=node;
    }
    while(ch=getchar()!=\n){
        node=(LinkList*)malloc(sizeof(LinkList));
        node->data=ch;
        end->next=node;
        end=node;
    }
    end->next=NULL;
    while(head->next!=NULL){
        head=head->next;
        printf("%c",head->data);
    }
    return 0;
}

18.

120 original articles published, 36 praised, 120000 visitors+
Private letter follow