C++笔试题 字符串的比较,全排列,类似 和分解的问题
来源:互联网 发布:js splice 删除 编辑:程序博客网 时间:2024/05/10 18:37
//题目:输入整数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;
}
- C++笔试题 字符串的比较,全排列,类似 和分解的问题
- CVTE笔试题---字符串的全排列
- 笔试面试之字符串的全排列
- 字符串的全排列问题
- 字符串的全排列问题
- 字符串的全排列问题
- 字符串的全排列问题
- 字符串的全排列问题
- 笔试题:字符串全排列
- 字符串的全排列和全组合
- 字符串的全排列和组合问题整理
- 字符串的全排列和所有组合问题
- 字符串的全排列问题的解决方法
- 字符串的全排列 C/C++
- 一个全排列的笔试题
- 全排列和全组合的问题
- 字符串、数组的全排列问题
- 字符串的全排列
- Django 设置cookies与获取cookies.
- CountDownLatch的介绍和使用
- [Leetcode] Remove Nth Node From End of List
- UML——类图(二)
- MockHttpServletRequest examples
- C++笔试题 字符串的比较,全排列,类似 和分解的问题
- 10.23 django1.10, TEMPLATES设置
- VVDocumenter-Xcode 函数注释
- 涵盖各种编程语言的深度学习库整理大全!
- Word 表格换页自动“续表”方法
- Java 并发专题 :闭锁 CountDownLatch 之一家人一起吃个饭
- Maven实现项目编译时不同命令加载不同属性值功能
- Android内存泄漏分析及调试
- Java关键字static、final使用小结