codevs 1341 与3和5无关的数

题目描述 Description

有一正整数a,如果它能被x整除,或者它的十进制表示法中某位上的数字为x,则称a与x相关.现求所有小于等于n的与3或5无关的正整数的平方和.

输入描述 Input Description

只有一行,一个正整数n(0<n<300)

输出描述 Output Description

只有一行,小于等于n的与3和5无关的正整数的平方和

样例输入 Sample Input

8

样例输出 Sample Output

134

代码:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>

using namespace std;
int square(int n) {
    if(n % 3 == 0 || n % 5 == 0) return 0;
    int d = n * n;
    while(n) {
        if(n % 10 == 3 || n % 10 == 5) return 0;
        n /= 10;
    }
    return d;
}
int main() {
    int n,ans = 0;
    scanf("%d",&n);
    for(int i = 1;i <= n;i ++) {
        ans += square(i);
    }
    printf("%d",ans);
}
原文地址:https://www.cnblogs.com/8023spz/p/10826788.html