GESP Level 2 September 2024 – Sum of Digits

Learn programming with Lao Ma while “leveling up and fighting monsters”!

This aims to provide comprehensive learning materials for children to prepare for the level examination together.

Add the assistant on WeChat and reply with 【GESP Level 2 2024.09_Sum of Digits】 to obtain the source code for this problem.

GESP Level 2 2024.09_Sum of Digits

[Submit]

https://www.luogu.com.cn/problem/B4036

[Problem Description]

Little Yang has positive integers, and he considers a positive integer to be a beautiful number if and only if the sum of its digits is a multiple of .

Little Yang wants you to write a program to determine which of the positive integers are beautiful numbers.

[Input Description]

The first line contains a positive integer , representing the number of positive integers.

Then lines follow, each containing a positive integer.

[Output Description]

For each positive integer, if it is a beautiful number, output <span>Yes</span>, otherwise output <span>No</span>.

[Sample Input 1]

3
7
52
103

[Sample Output 1]

Yes
Yes
No

[Hint]

The sum of the digits of 7 is , which is a multiple of . The sum of the digits of 52 is , which is a multiple of . The sum of the digits of 103 is , which is not a multiple of .

For all data, it is guaranteed that there are ,.

Reference Answer:

/*
 * Sum of Digits
 * https://www.luogu.com.cn/problem/B4036
*/
#include<iostream>

using namespace std;

int main() 
{
    int n;
    cin >> n;
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        int tot = 0;
        while (x)
        {
            tot += (x % 10);
            x /= 10;
        }
        if (tot % 7 == 0)
        {
            cout << "Yes\n";
        }
        else
        {
            cout << "No\n";
        }
    }
    return 0;
}

Youth Programming Competition Exchange

The “Youth Programming Competition Exchange Group” has been established (suitable for youth aged 6 to 18). Add the assistant on WeChat to invite everyone to join the study group. After joining, everyone can participate in regularly organized 21-day problem-solving challenges, level examination assessments, guidance for Ministry of Education whitelist competitions, and youth programming team competitions.

GESP Level 2 September 2024 - Sum of Digits

Leave a Comment