如何在结果中找到最大数量

时间:2017-05-30 13:11:55

标签: c

我得到5个整数,必须不小于10且不大于100.然后,如果我们将它们除以10,我必须找到它们的除法余数。然后,我必须找到我发现的余数的总和。 (最困难的部分)最后五个剩余部分中的哪一个是最大的。

在这里,我给你写了我写的代码,但我不知道如何进一步。也许用“for”,但我不知道究竟是怎么回事。

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int a, b, c, d, e, a10,b10,c10,d10,e10, sum, max;
  printf("give 5 integers \n");
  scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
  a10 = a % 10;
  b10 = b % 10;
  c10 = c % 10;
  d10 = d % 10;
  e10 = e % 10;
  printf("division remainder is: %d %d %d %d %d\n", 
  a10,b10,c10,d10,e10);
  sum = a10 + b10 + c10 + d10 + e10;
  printf("the sum of the remains is: %d\n", sum);

  system("pause");
}

2 个答案:

答案 0 :(得分:1)

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>

int *rands_range(int n, int low, int high);
void print(FILE *fp, int n, int *nums);
int *map(int n, int *nums, int (*f)(int));
int fold(int n, int *nums, int init, int (*f)(int, int));
int add(int x, int y);
int rem10(int x);
int greater(int x, int y);

int main(void){
    srand(time(NULL));
    int n = 5;

    printf("give 5 integers\n");
    int *integers = rands_range(n, 10, 100);
    print(stdout, n, integers);
    putchar('\n');

    printf("division remainder is: ");
    int *rems = map(n, integers, rem10);
    print(stdout, n, rems);
    putchar('\n');

    int sum = fold(n, rems, 0, add);
    printf("the sum of the remains is: %d\n", sum);
    putchar('\n');

    int max = fold(n, rems, *rems, greater);
    printf("the max of the remains is:  %d\n", max);
    putchar('\n');

    free(integers);
    free(rems);
    system("pause");
}

int *rands_range(int n, int low, int high){
    assert(n > 0 && RAND_MAX >= high - low && high >= low);

    int *values = malloc(n * sizeof(*values));
    if(values){
        for(int i = 0; i < n; ++i){
            values[i] = low + rand() % (high-low+1);
        }
    }
    return values;
}
void print(FILE *fp, int n, int *nums){
    for(int i = 0; i < n; ++i){
        if(i)
            fprintf(fp, ", ");
        fprintf(fp, "%d", nums[i]);
    }
    fprintf(fp, "\n");
}
int remainder_int(int x, int y){
    return x % y;
}
int add(int x, int y){
    return x + y;
}
int rem10(int x){
    return remainder_int(x, 10);
}
int greater(int x, int y){
    return x > y ? x : y;
}
int *map(int n, int *nums, int (*f)(int)){
    int *result = malloc(n * sizeof(*result));
    if(result){
        for(int i = 0; i < n; ++i){
            result[i] = f(nums[i]);
        }
    }
    return result;
}
int fold(int n, int *nums, int init, int (*f)(int, int)){
    int acc = init;
    for(int i = 0; i < n; ++i){
        acc = f(acc, nums[i]);
    }
    return acc;
}

具有可变参数的函数:

#include <stdio.h>
#include <stdarg.h>
#include <limits.h>

int max(int n, ...){
    int ret = INT_MIN;
    va_list ap;

    va_start(ap, n);
    while(n--){
        int wk = va_arg(ap, int);
        if(ret < wk)
            ret = wk;
    }
    va_end(ap);

    return ret;
}

int sum(int n, ...){
    int sum = 0;
    va_list ap;

    va_start(ap, n);
    while(n--){
        sum += va_arg(ap, int);
    }
    va_end(ap);

    return sum;
}

int main(void){
    int a = 6, b = 3, c = 9, d = 2, e = 8;
    printf("sum:%d\n", sum(5, a, b, c, d, e));
    printf("max:%d\n", max(5, a, b, c, d, e));
}

答案 1 :(得分:0)

以下是使用'for'和int数组的代码。要使用'for',您必须使用数组。

#include<stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    int max;
    int sum=0;
    int aryNum[5];
    int aryRem[5];

    printf("give 5 integers \n");
    for(i=0;i<5;i++)
    {
        scanf("%d",&aryNum[i]);
    }

    printf("division remainder is: ");
    for(i=0;i<5;i++)
    {
        aryRem[i]=aryNum[i]%10;
        printf("%d ",aryRem[i]);
    }
    printf("\n");

    max=aryRem[0];
    sum = max;
    for(i=1;i<5;i++)
    {
        if(max<aryRem[i])
        {
            max=aryRem[i];
        }
        sum+=aryRem[i];
    }
    printf("the sum of the remains is: %d\n", sum);
    printf("maximum remains is: %d\n", max);
    system("pause");
    return 0;
}

如果您不想使用'for'和int数组,那么您可以使用以下代码。但这不可取:

#include <stdio.h>
#include <stdlib.h>

int greater(int a, int b)
{
    return (a>b)? a:b;
}

int main()
{
  int a, b, c, d, e, a10,b10,c10,d10,e10, sum, max;
  printf("give 5 integers \n");
  scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
  a10 = a % 10;
  b10 = b % 10;
  c10 = c % 10;
  d10 = d % 10;
  e10 = e % 10;
  printf("division remainder is: %d %d %d %d %d\n",
  a10,b10,c10,d10,e10);
  sum = a10 + b10 + c10 + d10 + e10;
  printf("the sum of the remains is: %d\n", sum);

  /*New added code*/
  max=greater(a10,b10);
  max=greater(max,c10);
  max=greater(max,d10);
  max=greater(max,e10);
  printf("maximum remains is: %d\n", max);

  system("pause");
  return 0;
}