Guess the number: For a positive integer between 1 and 100, give the remainder of the number divided by 3, 4, and 7, and guess what the number is.

This is a question on the school's ACM platform. I encountered this problem before and didn’t know how to design the algorithm, so I found a similar article on csdn. The code is very short, but the algorithm is a bit difficult and requires some mathematical skills, which is not easy to understand. The algorithm will be explained in less space.
The following is the code I wrote. The idea is very simple. It is to use the for loop to judge one by one, and output if the condition is met. At the same time, use the flag to record once. If all the numbers are checked one by one and flag=0, it means that no eligible number can be found.

Question:
For a positive integer between 1 and 100, give the remainder of dividing the number by 3, 4, and 7, and guess what this number is. Enter the three remainders of dividing the number by 3, 4, and 7 on one line. If the answer is unique, output the integer guessed by the computer on one line. If the answer is not unique, output each integer that meets this condition on one line, separated by a semicolon (there is also a semicolon after the last integer). If there is no qualified integer, output "no solution".
Sample input
1 0 5
Sample output
40;
code implementation:

#include<stdio.h>
int main()
{
    
    
    int t, f, s, i = 1,sum = 0;
    int flag = 0;
    scanf("%d%d%d", &t, &f, &s);
    for (i; i <= 100; i++)
    {
    
    
        if (i % 3 == t && i % 4 == f && i % 7 == s)
        {
    
    
            printf("%d;", i);
            flag = 1;
        }
        else
        {
    
    
            flag = 0;
        }
        sum += flag;
    }
    if (sum == 0)
    {
    
    
        printf("no solution");
    }

    return 0;
}

For the first time writing a blog, if there are some mistakes, please understand.

Guess you like

Origin blog.csdn.net/qq_46541463/article/details/105398914