Nanjing Normal University re-examination training program three

With the original 21. The fourth letter after the letter instead of the original letter. "China" -> "Glmre"

With given initial approach

#include <stdio.h>

int main()
{
    char c1='C',c2='h',c3='i',c4='n',c5='a';
    c1+=4;
    c2+=4;
    c3+=4;
    c4+=4;
    c5+=4;
    printf("密码是:%c%c%c%c%c",c1,c2,c3,c4,c5);

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

operation result:

22. A segment output percentile scores by grade test scores

#include <stdio.h>

int main()
{
    char grade;
    scanf("%c",&grade);
    switch(grade)
    {
    case 'A':
        printf("90~100\n");
        break;
    case 'B':
        printf("80~89\n");
        break;
    case 'C':
        printf("70~79\n");
        break;
    case 'D':
        printf("60~69\n");
        break;
    default:
        printf("60(不包括)以下\n");
    }

    return 0;
}

operation result:

23. A process with the menu command switch

#include <stdio.h>

void action1(int a,int b)
{
    printf("a-b=%d\n",a-b);
}
void action2(int a,int b)
{
    printf("a+b=%d\n",a+b);
}
int main()
{
    int a=15,b=23;
    char ch;
    ch=getchar();
    switch(ch)
    {
    case 'A':
        action1(a,b);
        break;
    case 'B':
        action2(a,b);
        break;
    }
    return 0;
}

operation result:

Note: If the switch's case statements do not break out of, the results are as follows:

24. Given a positive integer of not more than 5 digits: number of digits which is seeking 1; 2 outputs each digit; 3 each digit output in the reverse order, the original 123, an output 321.

#include <stdio.h>

int main()
{
    int num;
    scanf("%d",&num);
    int place;   //求输入数字的位数
    if(num>9999)
        place=5;
    else if(num>999&&num<10000)
        place=4;
    else if(num>99&&num<1000)
        place=3;
    else if(num>9&&num<100)
        place=2;
    else
        place=1;
    printf("%d是%d位数\n",num,place);
    int a,b,c,d,e;//得到每位数
    a=num/10000;
    b=num/1000%10;
    c=num/100%10;
    d=num%100/10;
    e=num%10;
    printf("逆序输出各位数:");
    switch(place)  //逆序输出各位数
    {
    case 5:
        printf("%d %d %d %d %d\n",e,d,c,b,a);
        break;
    case 4:
        printf("%d %d %d %d\n",e,d,c,b);
        break;
    case 3:
        printf("%d %d %d\n",e,d,c);
        break;
    case 2:
        printf("%d %d\n",e,d);
        break;
    case 1:
        printf("%d\n",e);
        break;
    }

    return 0;
}

operation result:

25. The greatest common divisor of n and m seeking the least common multiple

#include <stdio.h>

int main()
{
    int m,n,t,maxy,minb;   //maxy为最大公约数,minb为最小公倍数
    scanf("%d %d",&m,&n);
    if(m<n)   //将m与n的小者放在n中
    {
        t=n;
        n=m;
        m=t;
    }
    for(int i=1; i<=n; i++) //求最大公约数
        if(n%i==0&&m%i==0)
            maxy=i;
    minb=m*n/maxy;   //求最小公倍数
    printf("最大公约数为%d,最小公倍数为%d\n",maxy,minb);
    return 0;
}

operation result:

26. Enter a line of characters, respectively, the statistics which the letters of the alphabet, spaces, numbers and other characters of the number of

#include <stdio.h>

int main()
{
    char ch;
    int a=0,b=0,c=0,d=0;
    while((ch=getchar())!='\n')
    {
        if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
            a++;
        else if(ch>='0'&&ch<='9')
            b++;
        else if(ch==' ')
            c++;
        else
            d++;
    }
    printf("英文字母:%d个\n数字:%d个\n空格:%d个\n其他:%d个\n",a,b,c,d);
    return 0;
}

operation result:

27. seeking Sn = a + aa + aaa + aaaa + ... (n th a) value. For example: 2 + 22 + 222 + 2222 + ... (5 2)

#include <stdio.h>

int main()
{
    int a,n,sum=0,i=1,temp=0;  //temp为每个加上的元素
    scanf("%d %d",&a,&n);
    while(i<=n)
    {
        temp=temp*10+a;  //下一个元素为上一个元素的*10+a
        sum+=temp;
        i++;
    }
    printf("%d\n",sum);
    return 0;
}

operation result:

28. seek sum (n!), I.e. 1! +2! + ... + n!

#include <stdio.h>

int main()
{
    int n,t=1,sum=0;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        t=t*i;
        sum+=t;
    }
    printf("Result:%d\n",sum);
    return 0;
}

operation result:

29. 求 am (k) + I (k²) + I (1 / k)

#include <stdio.h>

int main()
{
    int k1,k2,k3;
    double sum=0.0;
    for(k1=1; k1<=100; k1++)
        sum+=k1;
    //printf("%.2lf\n",sum);
    for(k2=1; k2<=50; k2++)
        sum+=k2*k2;
    //printf("%.2lf\n",sum);
    for(k3=1; k3<=10; k3++)
        sum+=1.0/k3;
    printf("%.2lf\n",sum);
    return 0;
}

operation result:

30. The output of all the daffodils number, the digits of the cube and is equal to the number itself. Such as: 1 = 153 + 5 + 5 * 5 * 3 * 3 * 3

#include <stdio.h>
#include <math.h>
int main()
{
    int num,a,b,c;
    for(num=100; num<1000; num++)
    {
        a=num%10;
        b=num/10%10;
        c=num/100;
        if(num==pow(a,3)+pow(b,3)+pow(c,3))
            printf("%d\n",num);
    }
    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/104954468