[高效算法设计]Calculator conundrum Uva 11549

来源:互联网 发布:猎豹浏览器有mac版吗 编辑:程序博客网 时间:2024/05/17 09:08

使用 sstream 方便输入输出, 

用于判断某个元素是否被访问过,  在数组开不下的时候可以使用 set集合

#include <iostream>#include <sstream>#include <set>using namespace std;int next(int n, int k){stringstream ss;ss << (long long)k*k;string s = ss.str();if (s.size() > n) s = s.substr(0, n);int ans;stringstream ss2(s);return ans;}int main(){int T;cin >> T;while (T--){int n, k;cin >> n >> k;set<int> s;int ans = k;while (!s.count((k))){s.insert(k);if (k > ans) ans = k;k = next(n, k);}cout << ans << endl;}return 0;}


0 0
原创粉丝点击