现在的位置: 首页 > 综合 > 正文

Pascal程序练习-与7无关的数

2013年05月14日 ⁄ 综合 ⁄ 共 511字 ⁄ 字号 评论关闭

与7无关的数
时间限制: 1000ms内存限制: 65536kB
描述
一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7,则称其为与7相关的数.现求所有小于等于n(n<100)的与7无关的正整数的平方和.
输入
输入为一行,正整数n,(n<100)
输出
输出小于等于n的与7无关的正整数的平方和
样例输入
21
样例输出
2336
参考代码

program pig3864;
{Numbers have relationship with seven 2011-10-8 20:00PM Eric Zhou}
var i,n,sum:Longint;
function hasRelation(n:Longint):Boolean;
var i:Longint;
begin
	if n mod 7 = 0 then
		exit(true);
	while n > 0 do
	begin
		i := n mod 10;
		if i = 7 then exit(true);
		n := n div 10;
	end;
	hasRelation := false;
end;
begin
	readln(n);
	sum := 0;
	for i := 1 to n do
		if not hasRelation(i) then
			sum := sum + i * i;
	writeln(sum);
end.

抱歉!评论已关闭.