算法库-全排列,递归全排列 auti()字符转为整型 stringstream用法

简介: 算法库-全排列,递归全排列 auti()字符转为整型 stringstream用法

导航


1.全排列函数next_permutation(beg,end)

2.auti() //将字符串转化为整型

万能头文件 #include < bits/stdc++.h>

3.stringstream的int型转为string型用法

———————————————————————————————————


1.全排列:可以推举出所有可能性


头文件:#include < algorithm>


例1:(int型)


#include <iostream>
using namespace std;
#include <algorithm>
int main()
{
  int a[] = {1,3,2}; //如果数组中数不是升序的话
  sort(a,a+len);  //注意要排一下序
  int len = sizeof(a)/sizeof(a[0]); //计算出数组中数量 
  do{
  for(int i=0;i<len;i++)
  {
    cout<<a[i];
  }
  cout<<endl;
  }while(next_permutation(a,a+len)); //这个函数返回的是bool类型,没有更多排列返回false 
  return 0;
}


运行结果:



例2:(string型)


#include <iostream>
using namespace std;
#include <algorithm>
#include <string>
int main()
{
  string s;
  cin>>s;
  do{
  cout<<s<<endl;
  }while(next_permutation(s.begin(),s.end())); 
  return 0;
}


运行结果:



例3:(vector容器)


#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
int main()
{
  vector<int> v;
  for(int i=0;i<=2;i++)
  {
  v.push_back(i);
  }
  do{
  for(vector<int>::iterator it=v.begin();it!=v.end();it++) //迭代器遍历
  {
    cout<<*it;
  }
  cout<<endl;
  }while(next_permutation(v.begin(),v.end())); //放入迭代器
  return 0;
}


运行结果:



手动写一个递归全排列:



学习网址:https://www.bilibili.com/video/av65164273?from=search&seid=12828774151800623150


使用for循环:


#include <stdio.h>
int main()
{
  int a,b,c;
  for(a=1;a<=3;a++)
  for(b=1;b<=3;b++)
    for(c=1;c<=3;c++)
    if(a!=b&&a!=c&&b!=c)
      printf("%d%d%d\n",a,b,c);
  return 0;
}


运行结果:



———————————————————————————————————


2.atoi() //将字符串转化为整型


要保证atoi中是char*型


c语言:


#include <stdio.h>
#include <stdlib.h>  //要加头文件 
int main()
{
  char *s = "123";  //放到atoi中要char*型 
  int a = atoi(s);
  printf("%d",a);  //输出123
  return 0;
}


c++:


#include <iostream>
using namespace std;
#include <stdlib.h>
int main()
{
  string s = "123";
  int a = atoi(s.c_str()); //中间要转换为char*型
  cout<<a<<endl;
  return 0; 
}


———————————————————————————————————


3.stringstream的int型转为string型用法


#include <iostream>
#include <sstream>  //要添加头文件 
using namespace std;
int main()
{
  int a = 145;
  string b;
  stringstream ss;  //定义 
  ss<<a;//传入 
  ss>>b;//转成string型 
  if(b == "145")
  {
  cout<<"相同类型"<<endl; 
  } 
  else
  {
  cout<<"不同类型"<<endl;
  }
  return 0;
}


相关文章
|
17天前
|
算法 C++
算法笔记:递归(c++实现)
算法笔记:递归(c++实现)
|
9天前
|
存储 算法 程序员
数据结构与算法===递归
数据结构与算法===递归
|
18天前
|
存储 算法 数据可视化
算法金 | D3blocks,一个超酷的 Python 库
D3Blocks是一个基于d3.js的Python图形库,用于创建吸引人的数据可视化图表,如D3graph、Elasticgraph和Sankey图。拥有超过470个Star,其特点包括简易性、功能丰富、易用性、可定制性和及时更新。通过pip安装后,用户能轻松创建粒子图和其他图表。文中展示了实战应用,如能源数据集的网络图,通过调整节点和边的属性实现个性化展示。关注作者,享受智能乐趣。
48 8
算法金 | D3blocks,一个超酷的 Python 库
|
15天前
|
机器学习/深度学习 算法 C语言
详细介绍递归算法在 C 语言中的应用,包括递归的基本概念、特点、实现方法以及实际应用案例
【6月更文挑战第15天】递归算法在C语言中是强大力量的体现,通过函数调用自身解决复杂问题。递归涉及基本概念如自调用、终止条件及栈空间管理。在C中实现递归需定义递归函数,分解问题并设定停止条件。阶乘和斐波那契数列是经典应用示例,展示了递归的优雅与效率。然而,递归可能导致栈溢出,需注意优化。学习递归深化了对“分而治之”策略的理解。**
30 7
|
16天前
|
算法 前端开发 Java
探讨Java中递归构建树形结构的算法
探讨Java中递归构建树形结构的算法
9 1
|
20天前
|
存储 算法 数据挖掘
python5种算法模拟螺旋、分层填充、递归、迭代、分治实现螺旋矩阵ll【力扣题59】
python5种算法模拟螺旋、分层填充、递归、迭代、分治实现螺旋矩阵ll【力扣题59】
|
20天前
|
机器学习/深度学习 存储 算法
Python5种算法回溯+剪枝、字典序、递归交换、计数回溯、迭代法 实现全排列ll【力扣题47】
Python5种算法回溯+剪枝、字典序、递归交换、计数回溯、迭代法 实现全排列ll【力扣题47】
|
20天前
|
存储 机器学习/深度学习 算法
python 五种算法转置后翻转、层次旋转、递归分块、一次性旋转、环状替换 实现旋转图像【力扣题48】
python 五种算法转置后翻转、层次旋转、递归分块、一次性旋转、环状替换 实现旋转图像【力扣题48】
|
8天前
|
机器学习/深度学习 编译器 算法框架/工具
OpenCV算法库
numba是一个用于编译Python数组和数值计算函数的编译器,这个编译器能够大幅提高直接使用Python编写的函数的运算速度。
|
12天前
|
机器学习/深度学习 存储 算法
算法学习:递归
算法学习:递归
15 0