题解 | #自守数#

自守数

http://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = Integer.valueOf(scan.nextLine().trim());
        int ans = 0;
        for (int i = 0; i <= n; i++) {
            if (isValid(i)) {
                ans++;
            }
        }
        System.out.println(ans);
    }
    public static boolean isValid(int num) {
        String str1 = num * num + "";
        String str2 = String.valueOf(num);
        return str1.substring(str1.length() - str2.length()).equals(str2);
    }
}
全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞
送花
回复 分享
发布于 2022-04-20 17:04

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务