没啥难度,还是难以判断测试用例的问题,也就是k是否大于10,否则就会采用字符串进行判断;
#include<iostream> #include<vector> #include<queue> #include<string> using namespace std; int n, k; struct node { int index = -1; int num = -1; }; queue<node>que; vector<node>vec; bool charge(int m) { if (m%k == 0) return true; string sm = to_string(m); string sk = to_string(k); if (sm.size() < sk.size()) return false; if (sm.find(sk,sm.size()-sk.size()) != string::npos) return true; else return false; } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { node n; n.index = i; n.num = i; if (charge(i)) { vec.push_back(n); } else { que.push(n); } } int cnt = n + 1; while (!que.empty()) { //对队列内进行循环计数; node n = que.front(); que.pop(); n.num = cnt++; if (charge(n.num)) { vec.push_back(n); } else { que.push(n); } } for (auto ele : vec) { cout << ele.index << " " << ele.num << endl; } }
这里注意一下获取个位数字的问题;
如果获取个位数字,可以直接%10或者num-num/10*10;
自己当时脑子抽了没想到;
原文地址:https://www.cnblogs.com/songlinxuan/p/12681243.html
时间: 2024-10-30 05:49:32