第五周数组

2024-02-02 20:22:19 浏览数 (1)

分糖果问题

代码语言:javascript复制
#include <stdio.h>
#include <math.h>

int main() {
    int a[5] = {10, 14, 8, 22, 16};
    int tmp[5];
    int i;
    int count = 0;

    while (!(a[0] == a[1] && a[0] == a[2] && a[0] == a[3] && a[0] == a[4])) {
        for (i = 0; i < 5; i  )
            if (a[i] % 2 == 1)
                a[i] = a[i]   1;

        for (i = 0; i < 5; i  )
            tmp[i] = a[i] / 2;

        for (i = 1; i < 5; i  )
            a[i] = tmp[i]   tmp[i - 1]; //后四个数字更新

        a[0] = tmp[0]   tmp[4]; //第一个更新
        count  ;
    }
    printf("分%d次n", count);
    for (i = 0; i < 5; i  )
        printf("%dn", a[i]);

    return 0;
}

摘水果冠军问题

代码语言:javascript复制
#include <stdio.h>
#include <string.h>

int main() {
    int a[3][4] = {{2011, 57, 68, 40}, {2012, 60, 83, 72}, {2013, 40, 56, 69}};
    int sum[3] = {0, 0, 0}; //保存每个人的水果重量
    int i, j;
    int max, index;
    for (i = 0; i < 3; i  ) {
        for (j = 1; j < 4; j  )
            sum[i] = sum[i]   a[i][j];
    }
    max = sum[0];
    index = 0;
    for (i = 1; i < 3; i  ) {
        if (sum[i] > max) {
            max = sum[i];
            index = i;
        }
    }

    printf("冠军是:%d", a[index][0]);


    return 0;
}

一共打了多少个单词

代码语言:javascript复制
#include <stdio.h>
#include <string.h>

int main() {
    char a[500];
    gets(a);
    int count = 0;
    if (a[0] != ' ')
        count  ;
    for (int i = 1; i < strlen(a); i  ) {
        if (a[i] != ' ' && a[i - 1] == ' ')
            count  ;
    }
    printf("一共的单词有:%d个", count);

    return 0;
}
代码语言:javascript复制
#include<stdio.h>

int main(){
    int n;
    float a[6666],b[6666];
    float sum,x;
    scanf("%d",&n);
    a[1]=1,b[1]=2;
    sum=2;
    for (int i=2;i<=n;i  )
    {
        b[i]=b[i-1] a[i-1];
        a[i]=b[i-1];
        x=b[i]/a[i];
        sum=sum x;
    
    }
    printf("%.2f",sum);
    return 0;
}
代码语言:javascript复制
int main(){
    int n,m,sum,ans;
    scanf("%d",&n);
    scanf("%d",&m);
    sum=n;
    ans=n;
    printf("%d ",n);
    for(int i=1;i<m;i  )
    {
        sum=sum*10 n;
        printf("  %d ",sum);
        ans =sum;
    }
    printf("= %d",ans);
    return 0;
}

0 人点赞