python判断自守数_自守数

26#include #include using namespace std;//转换成字符串 查找,利用现成的库函数 =-=int main(){long n;while(cin>>n){int ans = 2;// 0, 1也是for(long i = 3; i<=n; i++){long n2 = i*i;string s1 = to_string(i);string s...
摘要由CSDN通过智能技术生成

26

#include

#include

using namespace std;

//转换成字符串 查找,利用现成的库函数 =-=

int main()

{

long n;

while(cin>>n){

int ans = 2;// 0, 1也是

for(long i = 3; i<=n; i++){

long n2 = i*i;

string s1 = to_string(i);

string s2 = to_string(n2);

int pos = s2.size()- s1.size();

if(s2.find(s1,pos) != -1)

ans++;

}

cout<

}

return 0;

}

编辑于 2016-07-06 15:45:52

回复(17)

14

#include

using namespace std;

int main() {

int n;

while (cin >> n) {

int base = 10, count = 0;

for (int i = 0; i <= n; i++) {

if (i == base) base *= 10;

if (i*i%base == i)

count++;

}

cout << count << endl;

}

return 0;

}

发表于 2017-08-03 22:55:53

回复(4)

20

python 常规的解法: while True:

try:

a, res = int(input()), 0

for i in range(0, a + 1):

if str(i ** 2).endswith(str(i)):

res += 1

print(res)

except:

break

使用函数式编程: print len(filter(lambda x: str(x ** 2).endswith(str(x)), range(input())))

上面这是python 2.7的写法,在python3中,这样写会报错,要先将filter转为list: print(len(list(filter(lambda c:str(c**2).endswith(str(c)),range(int(input()))))))

编辑于 2018-09-29 18:14:37

回复(11)

8

import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

while (sc.hasNextInt()) {

int n = sc.nextInt();

int cnt = 0;

for(int i=0; i<=n; i++) {

long end = i*i;

if(String.valueOf(end).endsWith(String.valueOf(i))) {

cnt ++;

}

}

System.out.println(cnt);

}

}

}

发表于 2018-10-09 19:33:27

回复(1)

7

#include

#include

using namespace std;

int main(){

int n;

while(cin>>n){

int cnt=0;

string pow;

string str;

for(int val=0;val<=n;++val){

pow=to_string(val*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值