C++字符串(srtring)反转

对称平方数

题目描述

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

分析:

1.首先使用函数to_stringint转化为string;
2.然后将字符串反转(reverse)在<algorithm>中;判断是否相等便知道是不是对称的字符串。

#include <iostream>
#include <string>
#include <algorithm>

bool is_sym(int n){
    string s = to_string(n);
    string ss = s;
    reverse(s.begin(), s.end());
    if(s == ss) return true;
    else return false;
}

using namespace std;

int main(){
    for(int i = 1; i < 256; i++){
        if(is_sym(i * i)){
            cout << i << endl;
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zhuobo/p/10505239.html