阿里笔试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;
}



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

相关推荐

牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
1
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务