Nanjing Normal University re-examination training program two

11. bubble sort method

#include <stdio.h>

int main()
{
    int i,j,temp;
    int a[10]= {1,3,5,7,2,4,6,8,9,10};
    for(i=0; i<9; i++)
        for(j=0; j<i; j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    for(i=0; i<10; i++)
        printf("%d ",a[i]);
    printf("\n");
    return 0;
}

operation result:

12.3 * 4 matrix, where the value of programming to obtain the maximum value of that element, and the line number and column number in it

#include <stdio.h>

int main()
{
    int i,j,max,maxi=0,maxj=0;
    int a[3][4]= {{1,2,3},{6,5,4},{9,7,8}};
    max=a[0][0];
    for(i=0; i<3; i++)
        for(j=0; j<4; j++)
        {
            if(a[i][j]>max)
            {
                max=a[i][j];
                maxi=i;
                maxj=j;
            }
        }
    printf("max=%d,maxi=%d,maxj=%d\n",max,maxi,maxj);
    return 0;
}

operation result:

13. Enter a line of characters, statistics on the number of words separated by spaces between words

#include <stdio.h>

int main()
{
    char string[81];
    int i,num=0,word=0;   //一开始word=0,相当于加上第一个词
    char c;
    gets(string);
    for(i=0; (c=string[i])!='\0'; i++)
    {
        if(c==' ')    //遇到空格时,相当于一个单词的开始
            word=0;
        else if(word==0)  //空格的下一个字符,即本单词的第一个字符,进行num++
        {
            word=1;
            num++;
        }
    }
    printf("%d words",num);   //num为单词个数

    printf("\n");
    return 0;
}

operation result:

14. There are three strings, which identify the maximum of claim

#include <stdio.h>
#include <string.h>
int main()
{
    char str[3][10],temp[10];
    for(int i=0; i<3; i++)
        gets(str[i]);
    if(strcmp(str[0],str[1])>0)
        strcpy(temp,str[0]);
    else
        strcpy(temp,str[1]);
    if(strcmp(str[1],temp)>0)
        strcpy(temp,str[1]);
    printf("the largest string is:%s\n",temp);

    return 0;
}

operation result:

15. Enter 4 integer, to find the largest number

#include <stdio.h>

int max1(int a,int b)
{
    if(a>b)
        return a;
    else
        return b;
}
int main()
{
    int a,b,c,d,m;
    scanf("%d %d %d %d",&a,&b,&c,&d);
    m=max1(a,b);
    m=max1(m,c);
    m=max1(m,d);
    printf("max=%d\n",m);
    return 0;
}

operation result:

16.5 students, compared to the fourth of the five big 2-year-old, fourth than the third big 2-year-old, ..., the first 10 years. Q fifth much. Recursive.

#include <stdio.h>

int f(int n)
{
    if(n==1)
        return 10;
    else
        return f(n-1)+2;
}
int main()
{
    int n;
    scanf("%d",&n);
    printf("第%d个学生:%d岁\n",n,f(n));

    printf("\n");
    return 0;
}

operation result:

17. seeking recursive n!

#include <stdio.h>

int main()
{
    int i,n,sum=1;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
        sum=sum*i;
    printf("%d!=%d\n",n,sum);
    return 0;
}
#include <stdio.h>

int f(int n)
{
    if(n==0||n==1)
        return 1;
    else
        return f(n-1)*n;
}
int main()
{
    int n;
    scanf("%d",&n);
    printf("%d!=%d\n",n,f(n));

    printf("\n");
    return 0;
}

operation result:

18. Tower of Hanoi problem

#include <stdio.h>

void hanoi(int n,char one,char two,char three)
{
    if(n==1)
        printf("%c-->%c\n",one,three);
    else
    {
        hanoi(n-1,one,three,two);
        printf("%c-->%c\n",one,three);
        hanoi(n-1,two,one,three);
    }
}
int main()
{
    int m;
    scanf("%d",&m);
    hanoi(m,'A','B','C');

    printf("\n");
    return 0;
}

operation result:

19. Selection Sort

#include <stdio.h>

int main()
{
    int a[10]= {2,3,4,7,6,5,1,8,10,9};
    int i,j,k,temp;
    for(i=0; i<9; i++)
    {
        k=i;
        for(j=i+1; j<10; j++)
        {
            if(a[j]<a[k])
                k=j;
        }
        if(k!=i)
        {
            temp=a[i];
            a[i]=a[k];
            a[k]=temp;
        }
    }
    for(i=0; i<10; i++)
        printf("%d ",a[i]);
    printf("\n");
    return 0;
}

operation result:

20. There is a string, there are a number of characters, a character input now, the program requires the character string deleted.

#include <stdio.h>

void delete_string(char str[],char ch)
{
    int i,j;
    for(i=0,j=0; str[i]!='\0'; i++)
    {
        if(str[i]!=ch)
            str[j++]=str[i];
    }
    str[j]='\0';
}
int main()
{
    char str[20],ch;
    gets(str);
    ch=getchar();
    delete_string(str,ch);
    printf("%s",str);

    printf("\n");
    return 0;
}

operation result:

Published 462 original articles · won praise 55 · views 320 000 +

Guess you like

Origin blog.csdn.net/LY_624/article/details/104953402