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