Experiment 2-4-1 Count the number that the sum of each number is 5 (20 points)

This question requires the realization of two functions: one function judges whether the sum of the digits of a given positive integer is equal to 5; the other function counts how many integers meet the above requirements in a given interval, and calculates the sum of these integers.

Function interface definition:
int is( int number );
void count_sum( int a, int b ); The
function is judges whether the sum of the digits of number is equal to 5, if yes, it returns 1, otherwise it returns 0.

The function count_sum uses the function is to count how many integers in a given interval [a, b] meet the above requirements (that is, let is return 1), and calculate the sum of these integers. Finally follow the format

count = the number of integers that meet the condition, sum = the sum of these integers and
output. The question is guaranteed to be 0<a≤b≤10000.

Sample referee test procedure:

#include <stdio.h>

int is( int number );
void count_sum( int a, int b );

int main()
{
    
    
    int a, b;

    scanf("%d %d", &a, &b);
    if (is(a)) printf("%d is counted.\n", a);
    if (is(b)) printf("%d is counted.\n", b);
    count_sum(a, b);

    return 0;
}

/* 你的代码将被嵌在这里 */

Input sample:
104 999
Output sample:
104 is counted.
count = 15, sum = 3720
title set complete set portal

int is(int number)
{
    
    
    int sum = number % 10;
    do
    {
    
    
        number /= 10;
        sum += number % 10;
    }while (number / 10 > 0);
    if (sum == 5)
        return 1;
    else
        return 0;
}
void count_sum(int a, int b)
{
    
    
    int count, sum;
    count = sum = 0;
    for (int i = a; i <= b; i++)
        if (is(i))
        {
    
    
            count++;
            sum += i;
        }
            
    printf("count = %d, sum = %d", count, sum);
}

Guess you like

Origin blog.csdn.net/fjdep/article/details/112846444