OpenJudge exercises and answers (C ++) -- question 4045: numbers irrelevant to 3 and 5
Question:
-
Total time limit:
-
1000 ms
-
Memory limit:
-
65536kB
-
Description
-
A positive integer. If it can be divisible by x, or the number in a single digit in its decimal representation is x, it is called the number related to x. calculate the sum of the squares of all positive integers less than or equal to n (n <300) that are irrelevant to x.
-
Input
-
Input is a row, positive integer n, (n <300)
-
Output
-
The sum of squares of positive integers 3 and 5 that are less than or equal to n
-
Sample input:
-
8
-
Sample output:
-
134
Solution:
#include<iostream>using namespace std;int main(){int n;cin>>n;int s=0;if (n<10){for(int i=1;i<=n;i++){if(i%3!=0&&i%5!=0&&i!=3&&i!=5){s+=i*i;}}}else if(n<100){for(int i=1;i<=n;i++){if(i%5!=0&&i%3!=0&&i/10!=3&&i/10!=5&&i%10!=3&&i%10!=5){s+=i*i;}}}else{for(int i=1;i<=n;i++){if(i%5!=0&&i%3!=0){if(i/100!=5&&i/100!=3&&i%10!=3&&i%10!=5&&i%100/10!=3&&i%100/10!=5){s+=i*i;}}}}cout<<s; return="" pre=""><p></p> </s;></iostream>