求1+11+111+1111+11…11(n个1)的和除以7的余数是多少。
先求最大的n个1的乘积,再求所有数的和,最后求除以7的余数,代码如下,结果已验证,望采纳:
#include <stdio.h>
int main()
{
int n = 0,ten_num = 1,product = 1;
int gather,result,a;
printf("Enter a number:\n");
scanf("%d",&n);
a = n-1;
while(a > 0){
ten_num *= 10;
product += ten_num;
a--;
}
gather = product;
while(product > 0){
product = product/10;
gather += product;
}
result = gather%7;
printf("n:%d,gather:%d,result:%d\n",n,gather,result);
return 0;
}
结果:
Enter a number:
2
n:2,gather:12,result:5
Enter a number:
5
n:5,gather:12345,result:4
供参考:
#include <stdio.h>
int main()
{
int i, k, s, n;
scanf("%d", &n);
for (i = 0, k = 0, s = 0; i < n; i++) {
k = k * 10 + 1;
s += k;
}
printf("%d", s % 7);
return 0;
}