栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1261 Prime Land

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1261 Prime Land

#include<iostream>#include<sstream>#include<vector>#include<map>#include<algorithm>#include<deque>using namespace std;namespace{vector<int> P;int pow(int a, int b){int res = 1;while (b--)res *= a;return res;}void init(){P.push_back(2);for (int i = 3; i <= 32767; i++){bool prime = true;for (size_t t = 0; t < P.size() && prime; t++)if (i % P[t] == 0)prime = false;if (prime)P.push_back(i);}}bool is_prime(int num){return binary_search(P.begin(), P.end(), num);}void put(map<int, int> &M, int num){if (M.find(num) == M.end())M[num] = 1;elseM[num]++;}}int main(){init();string s;map<int, int> M;deque<pair<int, int> > Q;while (getline(cin, s), s != "0"){M.clear();Q.clear();istringstream is(s);int a, b, num = 1;while (is >> a >> b)num *= pow(a, b);num--;while (!is_prime(num)){for (size_t t = 0; t < P.size(); t++)if (num % P[t] == 0){put(M, P[t]);num /= P[t];break;}}put(M, num);for (map<int, int>::iterator it = M.begin(); it != M.end(); it++)Q.push_front(*it);cout << Q.front().first << " " << Q.front().second;for (size_t t = 1; t < Q.size(); t++)cout << " " << Q[t].first << " " << Q[t].second;cout << endl;}return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/374519.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号