最大公约数和最小公倍数的求解:
#include<bits/stdc++.h>
#include<bitset>
using namespace std;
int gcd(int x,int y)
{
int temp;
if(x<y)
{
temp=x;
x=y;
y=temp;
}
int r=x%y;
while(r!=0)
{
x=y;
y=r;
r=x%y;
}
return y;
}
int lcm(int x,int y)
{
return x*y/gcd(x,y);
}
int main()
{
int a,b;
cin>>a>>b;
cout<<"最大公约数:"<<endl ;
cout<<gcd(a,b);
cout<<endl<<"最小公倍数:"<<endl;
cout<<lcm(a,b);
return 0;
}
stl中的next_permutation全排列函数
函数原型
bool next_permutation(iterator start,iterator end)//当不存在当前序列之后的序列时返回false否则返回true
调用方法:
next_permutation(a,a+n)//n为数组长度
含义:
从数组a中的数据当前的排列状态向后排列(从大到小)
注:在全排列过程中,该函数实时改变数组中元素的顺序(可以用于输出);当全排列过程结束后数组的顺序还会还原。
全排列函数代码测试:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num[10]={1,2,3,4};
cout<<"全排列时的数组:"<<endl;
do{
for(int i=0;i<4;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
}while(next_permutation(num,num+4));
cout<<"全排列结束后的数组:"<<endl;
for(int i=0;i<4;i++)
{
cout<<num[i]<<" ";
}
return 0;
}
next_permutation(a,a+n)//n可以为小于数组长度的任意大小,根据需求可以设置数组中前几个元素全排列,后面的元素不动。
最近总结的要点(emmm忘得差不多才总结的一些易错点,小白一个,勿喷):
1.double精度比float高,一般遇到保留小数选择double较好。
2.sort(数组首地址,数组尾地址,(compare|空))//第三个参数为空表示从小到大。 bool compare(int a,int b) { return a>b; }
3.int *a=new int[元素个数]
4.
c++万能头文件:#include<bits/stdc++.h>
常用头文件:#include<cstdio>
#include<iostream>
#include<algorithm>
5.c++中可以定义string s,求字符串长度用s.length(),s+t起到将两个字符串连接在一起的作用。
6.若只需要数值的大小而不分正负号,unsigned int往往比int形整数表示的范围更广相当于两倍。
7.用if分类时情况要尽量全面。
8.当可能结果有限时,可以采用直接输出的方式进行。
9.<<左移运算符不会对原数产生影响,必须和在表达式里一起写。
10.开辟比较大的数组最好在主函数外开辟,这样不会产生溢出。
11.当遇到题目需要保留小数位数的时候,记得使用printf。
12.十进制转二进制使用bitset<长度> b(x);string s=b.to_string();比较快
13.vector thing[maxn];
vectorvec[10]与vectorvec(10)的区别
前者是定义了10个vector变量,后者是定义了一个长度为10的vector变量。
具体例子参见该文章