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