北京大学 与7无关的数(java)

题目描述
一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7, 则称其为与7相关的数.现求所有小于等于n(n<100)的与7无关的正整数的平方和。
输入描述:
案例可能有多组。对于每个测试案例输入为一行,正整数n,(n<100)
输出描述:
对于每个测试案例输出一行,输出小于等于n的与7无关的正整数的平方和。
示例1
输入
复制
21
输出
复制
2336
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.* ;
public class Main
{
	static char[] pre;
	static char[] in;
	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String str;
			while((str=br.readLine()) != null) {
				int n = Integer.parseInt(str);
				boolean[] num = new boolean[n+1];
				for(int i = 7; i <= n; i += 7) {
					num[i] = true;
				}
				for(int i = 17; i <= n; i += 10) {
					num[i] = true;
				}
				for(int i = 70; i <= n && i < 80; i++) {
					num[i] = true;
				}
				int count = 0;
				for(int i = 1; i <= n; i++) {
					if(!num[i]) count += i*i;
				}
				System.out.println(count);
			}				 
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
}



发布了252 篇原创文章 · 获赞 24 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43306331/article/details/104266049