Nanjing Normal University re-examination program training four

31. a ball falling from 100m, half of the original height of each bounce back, and asked the 10th experienced a total number of meters, how many meters rebound.

#include <stdio.h>

int main()
{
    double h=100,sum=100;   //sum初值置为首高度
    for(int i=1; i<=10; i++)
    {
        h*=0.5;   //下次高度为本次的一半
        if(i==1)   //sum初值已加过
            continue;
        sum=sum+2*h; //每次经历两次
    }
    printf("第10次共经历%.2lf米,反弹%.2lf米\n",sum,h);
    return 0;
}

operation result:

32. It is exactly equal to a number of factors (divisible) of the sum, such as: 6 = 1 + 2 + 3. Find all finished within a few 1000

#include <stdio.h>

int main()
{
    int i,j,sum,a[10],k;
    for(i=2; i<1000; i++)
    {
        a[10]= {0};
        k=0;
        sum=0;
        for(j=1; j<i; j++)
        {
            if(i%j==0)
            {
                sum+=j;
                a[k++]=j;
            }

        }
        if(sum==i)
        {
            printf("%d=",i);
            for(j=0; j<k-2; j++)
                printf("%d+",a[j]);
            printf("%d\n",a[k-2]);
        }
    }
    return 0;
}

operation result:

33. The sequence of a fraction 2 / 1,3 / 2,5 / 3,8 / 5,13 / 8,21 / 13, 20 and find the front ....

#include <stdio.h>

int main()
{
    float n=1,m=2,t,s,sum=0;//m为分母,n为分子
    for(int i=1; i<=20; i++)
    {
        s=m/n;
        sum+=s;  //求和
        t=m;   //往后确定两部分的值
        m=m+n;
        n=t;
    }
    printf("sum=%f\n",sum);
    return 0;
}

operation result:

34. Monkeys eat peach problems. Monkey off his first day of a number of peach, half eaten immediately, but also eat a; the next morning and eat the rest of the half, then eat one; later, too. By day 10 only one. I asked the first day how many peaches were picked?

#include <stdio.h>

int main()
{
    int i,sum=1;   //rest为第10天剩余个数
    for(i=9; i>0; i--) //i为第9~1天
        sum=(sum+1)*2;
    printf("sum=%d\n",sum);
    return 0;
}

operation result:

35. The iterative method x = sqrt (a). Iterative formula of square Xn + 1 = 1/2 * (Xn + a / Xn)

#include <stdio.h>
#include <math.h>
int main()
{
    float x1=1,x2,a;
    scanf("%f",&a);
    x2=a;
    while(fabs(x1-x2)>=1e-5)
    {
        x1=x2;
        x2=(x1+a/x1)/2;
    }
    printf("%.2f,%.2f\n",x1,x2);

    return 0;
}

operation result:

36. seeking dichotomy between roots following equation (-10,10) is: 2 * x³-4 * x² + 3 * x-6 = 0

#include <stdio.h>
#include <math.h>
int main()
{
    double a=-10,b=10,c,x,x1,x2,x3;
    for(int i=0;; i++)
    {
        c=(a+b)/2;
        x1=2*pow(a,3)-4*pow(a,2)+3*a-6;
        x2=2*pow(b,3)-4*pow(b,2)+3*b-6;
        x3=2*pow(c,3)-4*pow(c,2)+3*c-6;
        if(x3==0)
            x=c;
        else if(x1*x3<0)
            b=c;
        else if(x2*x3<0)
            a=c;
        if(fabs(a-b)<1e-8)
        {
            x=a;
            break;
        }
    }
    printf("Root=%lf\n",fabs(x));
    return 0;
}

operation result:

37. Output

   *

  ***

 *****

*******

 *****

  ***

   *

#include <stdio.h>

int main()
{
    int i,j,k;
    for(i=0; i<=3; i++)
    {
        for(j=0; j<3-i; j++)
            printf(" ");
        for(k=0; k<2*i+1; k++)
            printf("*");
        printf("\n");
    }
    for(i=0; i<3; i++)
    {
        for(j=0; j<i+1; j++)
            printf(" ");
        for(k=0; k<5-2*i; k++)
            printf("*");
        printf("\n");
    }
    return 0;
}

operation result:

38. competition. A team of a, b, c three, Team B is x, y, z three. that he does with a ratio of x, c, said he does not x, z ratio. Programming three teams racing to find the list.

#include <stdio.h>

int main()
{
    char i,j,k;   //i,j,k分别对应为a,b,c的对手
    for(i='x'; i<='z'; i++)
        for(j='x'; j<='z'; j++)
            if(j!=i)   //a,b,c不能同为一个人的对手
                for(k='x'; k<='z'; k++)
                    if(k!=i&&k!=j)  //a,b,c不能同为一个人的对手
                    {
                        if(i!='x'&&k!='x'&&k!='z')   //a不与x比,c不与x,z比
                            printf("a--%c\nb--%c\nc--%c\n",i,j,k);
                    }
    return 0;
}

operation result:

39. The two-dimensional array of rows and columns are interchanged, to deposit another two-dimensional array.

#include <stdio.h>

int main()
{
    int a[3][3]= {{1,2,3},{4,5,6},{7,8,9}},b[3][3];
    printf("原矩阵为:\n");
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            printf("%d ",a[i][j]);
            b[j][i]=a[i][j];
        }
        printf("\n");
    }

    printf("行列互换之后:\n");
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
            printf("%d ",b[i][j]);
        printf("\n");
    }
    return 0;
}

operation result:

40. seeking a 3 * 3 matrix integer and the diagonal elements.

#include <stdio.h>

int main()
{
    int a[3][3]= {{1,2,3},{4,5,6},{7,8,9}},sum=0;
    for(int i=0; i<3; i++)
        sum+=a[i][i];
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            printf("%d ",a[i][j]);
            if(j==2)
                printf("\n");
        }
    }
    printf("对角线元素之和为:%d\n",sum);
    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/104966124