Nanjing Normal University re-examination training program five

41. give a percentile score, a required output level 'A', 'B', 'C', 'D', 'E'. 90 points or more as 'A', 80 ~ 89 are divided into 'B', 70 ~ 79 are divided into 'C', 60 ~ 69 are divided into 'D', 60 points or less 'E'.

#include <stdio.h>

int main()
{
    float score;
    scanf("%f",&score);
    while(score<0||score>100)
        scanf("%f",&score);
    switch((int)(score/10))
    {
    case 10:
    case 9:
        printf("A");
        break;
    case 8:
        printf("B");
        break;
    case 7:
        printf("C");
        break;
    case 6:
        printf("D");
        break;
    case 5:
    case 4:
    case 3:
    case 2:
    case 1:
    case 0:
        printf("E");
        break;
    }
    printf("\n");
    return 0;
}

operation result:

42. The array has a sequence had already been booked, a number of inputs required by the law of the original insert it sorted array.

#include <stdio.h>

int main()
{
    int a[10]= {1,2,3,5,6,7,8,9,10},num,i;
    scanf("%d",&num);
    a[9]=num;
    if(a[9]<a[8])
    {
        for(i=8; i>=0; i--)
        {
            if(a[i]>num)
                a[i+1]=a[i];
            else
                break;
        }
        a[i+1]=num;
    }
    for(i=0; i<10; i++)
        printf("%d ",a[i]);
    printf("\n");
    return 0;
}

operation result:

43. Triangle (line 10) outputs the following

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

...

#include <stdio.h>
#define N 10
int main()
{
    int i,j,a[N][N];
    for(i=0; i<N; i++)
    {
        a[i][0]=1;   //第一列为1
        for(j=1; j<=i; j++)
        {
            if(i>j)    //其它值由计算得出
                a[i][j]=a[i-1][j]+a[i-1][j-1];
            else
                a[i][j]=1;  //每行最后一位为1
        }
    }
    for(i=0; i<N; i++)    //打印杨辉三角
    {
        for(j=0; j<=i; j++)
            printf("%d ",a[i][j]);
        printf("\n");
    }

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

operation result:

44. The two-dimensional array to find the saddle point, that is the position of elements on the behavior of the largest, as in the smallest, may not have a saddle point.

#include <stdio.h>
#define N 3
int main()
{
    int i,j,k,a[N][N],max,maxj,flag;
    for(i=0; i<N; i++)
        for(j=0; j<N; j++)
            scanf("%d",&a[i][j]);
    for(i=0; i<N; i++)
    {
        max=a[i][0];     //找本行最大
        maxj=0;
        for(j=0; j<N; j++)
        {
            if(a[i][j]>max)
            {
                max=a[i][j];
                maxj=j;
            }
        }
        flag=1;      //判断是否为本列最小
        for(k=0; k<N; k++)
            if(a[k][maxj]<max)
            {
                flag=0;
                continue;
            }
        if(flag)
        {
            printf("a[%d][%d]=%d\n",i,maxj,a[i][maxj]);
            break;
        }
    }
    if(!flag)
        printf("It is not exist!");
    printf("\n");
    return 0;
}

operation result:

45. A binary search (the number of ascending into the array 15, a check number)

#include <stdio.h>

int main()
{
    int a[15]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},i,j,mid,num;
    scanf("%d",&num);
    i=0;
    j=14;
    while(i<=j)
    {
        mid=(i+j)/2;
        if(num==a[mid])
        {
            printf("在第%d个位置",mid+1);
            break;
        }
        else if(num<a[mid])
            j=mid+1;
        else
            i=mid+1;
    }
    if(i>j)
        printf("not exist!");
    printf("\n");
    return 0;
}

operation result:

46. ​​The following output pattern

****

 ****

  ****

   ****

#include <stdio.h>

int main()
{
    for(int i=0; i<5; i++)
    {
        for(int j=0; j<i; j++)
            printf(" ");
        printf("****\n");
    }
    printf("\n");
    return 0;
}

operation result:

47. There is an article, there are three lines of text, 80 characters per line, respectively, the statistics of the number of required capital letters, lowercase letters, numbers, spaces and other characters in them.

#include <stdio.h>

int main()
{
    char word[3][100];
    int i,j,a,b,c,d,e;
    a=b=c=d=e=0;
    for(i=0; i<3; i++)
    {
        gets(word[i]);
        for(j=0; j<80&&word[i][j]!='\0'; j++)
        {
            if(word[i][j]>='A'&&word[i][j]<='Z')   //大写字母个数
                a++;
            else if(word[i][j]>='a'&&word[i][j]<='z')   //小写字母个数
                b++;
            else if(word[i][j]>='0'&&word[i][j]<='9')   //数字个数
                c++;
            else if(word[i][j]==' ')   //空格个数
                d++;
            else    //其他字符个数
                e++;
        }
    }
    printf("大写字母个数:%d\n小写字母个数:%d\n数字个数:%d\n空格个数:%d\n其他字符个数:%d",a,b,c,d,e);
    printf("\n");
    return 0;
}

operation result:

48. Translation password A-> Z a-> z

                 B->Y b->y

                 C->X c->x

Translated into the i-th (26-i + 1) th letter, the same non-letter characters, and outputs the original password.

#include <stdio.h>

int main()
{
    char a[10],b[10];
    int i;
    gets(a);
    for(i=0; a[i]!='\0'; i++)
    {
        if(a[i]>='A'&&a[i]<='Z')
            b[i]=155-a[i];   //'A'+'Z'=155
        else if(a[i]>='a'&&a[i]<='z')
            b[i]=219-a[i];  //'a'+'z'=219
        else
            b[i]=a[i];
    }
    b[i]=0;
    printf("密码为:\n");
    puts(a);
    printf("译后原文:\n");
    puts(b);
    printf("\n");
    return 0;
}

operation result:

49. The two strings together

#include <stdio.h>

int main()
{
    int i,j;
    char str[20],str1[20],str2[20];
    gets(str1);
    gets(str2);
    for(i=0; str1[i]!='\0'; i++)
        str[i]=str1[i];
    for(j=0; str2[j]!='\0'; j++)
        str[i+j]=str2[j];
    str[i+j+1]='\0';
    printf("%s",str);

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

operation result:

50. The two compared strings s1 and s2: If s1> s2, the output of a positive number; s1 = s2, outputs 0; s1 <s2, a negative output.

#include <stdio.h>

int main()
{
    int i=0,num;
    char s1[10],s2[10];
    gets(s1);
    gets(s2);
    while(s1[i]==s2[i]&&s1[i]!='\0')
        i++;
    if(s1[i]==s2[i])
        num=0;
    else
        num=s1[i]-s2[i];
    printf("%d",num);
    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/104969630