//题目:输入整数N,输出从1开始至N的字符串排列
//例如:N=10;输出【1 10 2 3 4 5 6 7 8 9】
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
void OutputInteger(int N)
{
vector<string> V;
char str[3] = { '\0' };
for (int i = 1; i <=N; ++i)
{
sprintf(str,"%d", i);
V.push_back(str);
}
sort(V.begin(),V.end());
for(vector<string>::iterator it = V.begin(); it != V.end(); it++)
{
cout << *it<< " " << endl;
}
}
int main()
{
cout <<"please input Integer N:" << endl;
int N;
cin >> N;
OutputInteger(N);
system("pause");
return 0;
}
//题目2:使用数组arr[]={1,2,3},给定一个数N,求N由arr元素组成的方式
//比如:g(4)={ (1 1 1 1),
// (2 1 1),
// (1 2 1),
// (1 1 2),
// (2 2),
// (3 1),
// (1 3)}
#include<iostream>
#include<vector>
using namespace std;
void Function(int n,int arr[],vector<int> v)
{
if (n < 0)
return;
if (n == 0)
{
for (vector<int>::iteratorit = v.begin(); it != v.end(); it++)
cout <<*it << " ";
cout << endl;
}
for (int i = 0; i <3; i++)
{
v.push_back(arr[i]);
Function(n - arr[i],arr, v);
v.pop_back();
}
}
int main()
{
cout <<"please input n:" << endl;
int n;
cin >> n;
int arr[] = { 1,2,3 };
staticvector<int> v;//装载符合条件的组合
Function(n, arr, v);
system("pause");
return 0;
}
题目3.字符串的全排列
用C++写一个函数, 如 Foo(const char *str), 打印出 str 的全排列,
如 abc 的全排列: abc, acb, bca, dac,cab, cba
#include<iostream>
#include<string>
using namespace std;
void permutation(string& str,int n,int N)//n指示交换的位置,N指示交换结束的位置
{
if (n == N)
cout << str <<endl;
for (int i = n; i <=N; ++i)
{
swap(str[n-1],str[i-1]);
permutation(str, n +1, N);
}
}
int main()
{
static string str = "abcd";
int len_str = str.length();
permutation(str, 1,len_str);
system("pause");
return 0;
}