概述
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
You are given an integer nn. You have to change the minimum number of digits in it in such a way that the resulting number does not have any leading zeroes and is divisible by 77.
If there are multiple ways to do it, print any of them. If the given number is already divisible by 77, leave it unchanged.
Input
The first line contains one integer tt (1≤t≤9901≤t≤990) — the number of test cases.
Then the test cases follow, each test case consists of one line containing one integer nn (10≤n≤99910≤n≤999).
Output
For each test case, print one integer without any leading zeroes — the result of your changes (i. e. the integer that is divisible by 77 and can be obtained by changing the minimum possible number of digits in nn).
If there are multiple ways to apply changes, print any resulting number. If the given number is already divisible by 77, just print it.
Example
input
Copy
3 42 23 377
output
Copy
42 28 777
Note
In the first test case of the example, 4242 is already divisible by 77, so there's no need to change it.
In the second test case of the example, there are multiple answers — 2828, 2121 or 6363.
In the third test case of the example, other possible answers are 357357, 371371 and 378378. Note that you cannot print 077077 or 7777.
解题说明:此题是一道数学题,按照题目意思尽可能少的改动数字中的某位数,让数字能被7整除,最容易想到的就是只改最低位。首先统计出余数是多少,比较余数和n的最低位,余数比n的最后一位小那就直接减去余数,否则就加7再减。
#include <stdio.h>
int main(void)
{
int t;
scanf("%d", &t);
while (t--)
{
int n, k;
scanf("%d", &n);
k = n % 7;
if (n % 10 - k<0)
{
n = n + 7 - k;
}
else
{
n = n - k;
}
printf("%dn", n);
}
return 0;
}
最后
以上就是机智自行车为你收集整理的A. Div. 7的全部内容,希望文章能够帮你解决A. Div. 7所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复