牛客网 - 反转整数

问题描述:

打印所有不超过n(n<256)的,其平方具有对称性质的数。如11*11=121。

知识点:

  1. int 转字符串方法:不是用 char* 而是 用 to_string() 就可以
    关于其他 int 转 string 和 string 转 int 的方法还有很多,请看:
    https://www.cnblogs.com/smile233/p/8379802.html
    https://blog.csdn.net/lxj434368832/article/details/78874108

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	int count;
	string s;
	for (int i = 1; i<256; i++)
	{
		count = 1;
		s = to_string(i*i);
		int len = s.length();
		for (int a = 0; a<len / 2; a++)
		{
			if (s[a] != s[len - 1 - a])
				count = 0;
		}
		if (!count)
			continue;
		cout << i << endl;

	}
	return 0;
}

  1. 注意反转整数的写法:
#include <bits/stdc++.h>
 
int rev(int n){      //反转整数 妙不可言!
    int new_n=0;
    while(n){
        new_n = new_n*10+n%10;
        n/=10;
    }
    return new_n;
}
 
int main(){
    int i;
    for(i=1;i<256;i++)
        if(i*i==rev(i*i))
            printf("%d\n",i);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值