阿里笔试28

其中第二道思路一般都会想到dfs,但想起来C++STL算法,直接使用next_permutation搞定,string转int直接用stringstream搞定,偷奸取巧了,附上代码,欢迎交流
#include<iostream>
#include <vector>
#include<string>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
void print(string str) {
cout << str << endl;
}
int main()
{
stringstream ss;
int n, m;
cin >> n >> m;
vector<string> vec;
string str;
while (n) {
int temp = n % 10;
str.push_back(temp+'0');
n = n / 10;
}
while (next_permutation(str.begin(), str.end())) {
vec.push_back(str);
}
for_each(vec.begin(), vec.end(), print);
int res(0);
for (int i(0); i < vec.size(); i++) {
if (vec[i][0] == '0') continue;
else {
int num;
ss << vec[i];
ss >> num;
if (num%m==0){
res++;
}
}
}
cout << res << endl;

return 0;
}



#阿里巴巴#
全部评论
一样的思路,python凉了😭
点赞
送花
回复 分享
发布于 2020-08-28 20:32
老哥加个路人通道吧😂
点赞
送花
回复 分享
发布于 2020-08-28 20:33
秋招专场
校招火热招聘中
官网直投
为什么我超时了...
点赞
送花
回复 分享
发布于 2020-08-28 20:35
这暴力能过?
点赞
送花
回复 分享
发布于 2020-08-28 20:50
Python只有40。。。早知道也用内置迭代器了 估计比手写快很多
点赞
送花
回复 分享
发布于 2020-08-28 21:02
求问 两道题的题目是什么呀
点赞
送花
回复 分享
发布于 2020-08-28 21:58
看懂你的解法啦,但是 n 稍微大点就容易超时吧
点赞
送花
回复 分享
发布于 2020-08-29 14:32

相关推荐

1 4 评论
分享
牛客网
牛客企业服务