首页 > 其他 > 详细

A simple trick about <sstream>

时间:2020-05-01 18:16:44      阅读:67      评论:0      收藏:0      [点我收藏+]

If you want to transform integer into string,so that you can easily access every bit of the number,you can use the "stringstream" variable.

 

Q:you are asked to figure out all the number qualified.

1.five-digit and six-digit decimal palindromes.

2.the sum of each digit of the number is n.

Input(n):

52

Output:

899998
989989
998899

Complete code is given below:

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

int main(){
    int n,i,j;
    string s,t;
    stringstream temp;
    cin>>n;
    for(i=9999;i<=999999;i++){
        int sum=0;
        temp<<i;
        temp>>s;
        temp.clear();//remember to clear the variable if you want reuse it.
        t=s;
        reverse(t.begin(),t.end());
        for(j=0;s[j];j++)
            sum+=(s[j]-0);
        if(sum==n&&s==t&&j>=5){
            cout<<i<<endl;
        }
     }
    return 0;
}

But........it seems slower than the normal solutions.

....much slower.

Just take it as an example.Don‘t be serious.

 

A simple trick about <sstream>

原文:https://www.cnblogs.com/lym11248/p/12813878.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!