find remainder

The remainder when n consecutive positive integers are divided by 7, for example, the remainder of 9/7 is 2. . . .

enter:

The input data has multiple lines of n, and ends when n is 0

output:

For each row of test data, calculate the remainder, one row per output

Test Data

enter

1

2

0

output

2

1

analyze:

The number of type int has a range, and it is definitely not possible to directly use a particularly large number to find the remainder

Do it like hand division

You can also find rules.

Method ① The code is as follows

#include<stdio.h>
intmain()
{
    int n;
    while(1)
    {
        scanf("%d",&n);
        if(n==0)
        break;

        int k=n,res=0;
        while(k)
        {
            res = res * 10 + 9;
            res = res% 7;
            k--;
        }
        printf("%d\n",res);
    }
    return 0;
}

Method ② Pass the rule

The rules are as follows

 2, 1, 5, 3, 4, 0, 2, 1, 5, 3, 4, 0, 2, 1, 5

#include<stdio.h>
intmain()
{
    int n;
    int a[6]={2,1,5,3,4,0};
    while(scanf("%d",&n))
    {
        if(n==0)
            break;
        int k=n%6;
        if(k==0)
            printf("%d\n",a[5]);
        else
            printf("%d\n",a[k-1]);
    }
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324695147&siteId=291194637