九度OJ 1132:与7无关的数 (数字特性)

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:1619

解决:1037

题目描述:

一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7,
则称其为与7相关的数.现求所有小于等于n(n<100)的与7无关的正整数的平方和。

输入:

案例可能有多组。对于每个测试案例输入为一行,正整数n,(n<100)

输出:

对于每个测试案例输出一行,输出小于等于n的与7无关的正整数的平方和。

样例输入:
21
样例输出:
2336
来源:
2008年北京大学软件所计算机研究生机试真题

思路:

基础题,涉及数位分解以及整除。


代码:

#include <stdio.h>
 
int main(void)
{
    int n, i;
    int sum;
 
    while (scanf("%d", &n) != EOF)
    {
        sum = 0;
        for(i=1; i<=n; i++)
        {
            if (i%7 == 0 || i%10 == 7 || i/10 == 7)
                continue;
            sum += i*i;
        }
        printf("%d
", sum);
    }
 
    return 0;
}
/**************************************************************
    Problem: 1132
    User: liangrx06
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:912 kb
****************************************************************/


编程算法爱好者。
原文地址:https://www.cnblogs.com/liangrx06/p/5083899.html