南师大复试编程训练五

41.给一个百分制成绩,要求输出等级‘A’,‘B’,‘C’,‘D’,‘E’。90分以上为‘A’,80~89分为‘B’,70~79分为‘C’,60~69分为‘D’,60分以下为‘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;
}

运行结果:

42.已有一个已排好序的数组,输入一个数要求按原来排序的规律将它插入数组中。

#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;
}

运行结果:

43.输出以下杨辉三角形(10行)

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;
}

运行结果:

44.找出二维数组中的鞍点,即该位置上的元素在该行为最大,在该列为最小,也可能没有鞍点。

#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;
}

运行结果:

45.折半查找(15个数由小到大放到数组中,查一个数)

#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;
}

运行结果:

46.输出以下图案

****

 ****

  ****

   ****

#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;
}

运行结果:

47.有一篇文章,共有3行文字,每行80个字符,要求分别统计出其中英文大写字母,小写字母,数字,空格及其他字符的个数。

#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;
}

运行结果:

48.译密码 A->Z a->z

                 B->Y b->y

                 C->X c->x

第i个译成(26-i+1)个字母,非字母字符不变,输出密码和原文。

#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;
}

运行结果:

49.将两个字符串连接起来

#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;
}

运行结果:

50.将两个字符串s1和s2相比较:若s1>s2,输出一个正数;s1=s2,输出0;s1<s2,输出一个负数。

#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;
}

运行结果:

发布了462 篇原创文章 · 获赞 55 · 访问量 32万+

猜你喜欢

转载自blog.csdn.net/LY_624/article/details/104969630