C++语言字典库
#include<bits/stdc++.h>
using namespace std;
1.取出位数(待解决)
int main(){
int k,n;
cin>>n;
while(k>0){
n=k%10;
k/=10;
}
cout<<;
return 0;
}
2.素数判断函数
(sushu(i))//调用函数
bool sushu(int num)
{
if(num==1) return false;
for(int i=2;i*i<=num;i++)
{
if(num%i==0) return false;
}
return true;
}
//或者
int sushu(int x){
int j;
if(x==2) return 1;
j=2;
while(j<=sqrt(x)&&x%j!=0) j++;
if(x%y==0)
return 0;
else
return 1;
}
//或者
int sushu(int a){
int y=2;
while(y<=floor(sqrt(a))&&(a%y!=0))
{
y++;
}
return (y>floor(sqrt(a)) ? 1 : 2);//是素数返回1,不是返回2
}
3.三目运算符
//例如:
x=(a>b?c:d)
//相当于 判断若a>b成立,则x=c;否则x=d.
if(a>b){
x=c;
}
else{
x=d;
}
4.取两个数的最大值或最小值
#include<math.h> //需加头文件:
max(a,b);
min(c,d);
5. 判断闰年
#include<iomanip>
using namespace std;
int main(){
int a;
cin>>a;
if((a%4==0)&&((a%100!=0)||(a%400==0))){
cout<<"是闰年";
}
else {
cout<<"不是闰年";
}
return 0;
}
6.求最大公约数
int yue(int x,int y){
int r=x%y;
while(r!=0){
x=y;
y=r;
r=x%y;
}
return y;
}
7.求最小公倍数(在6的基础上)
int bei(){
return x*y/yue(x,y);//主程序中使用时用“bei()”的形式
}
8.数制转换
程序举例:
#include<cstdlib>
#include<iostream>
using namespace std;
void TurnData(int n,int a);
char ch[6]={'A','B','C','D','E','F'};
int main(){
int n;
cin>>n;
TurnData(n,2);
TurnData(n,8);
TurnData(n,16);//前边是要转换的数,后边是转换成的数制
return 0;
}
void TurnData(int n,int a){
int x[17],i,j,k=0;
cout<<n<<"turn into"<<a<<":"<<endl;
if(n<0) cout<<"-";
j=abs(n);
do{
k++;
i=j%a;
j/=a;
x[k]=i;
}while(j!=0);
for(int h=k;h>=1;--h)
if(x[h]<10) cout<<x[h];
else cout<<ch[x[h]-10];
cout<<endl;
}
9.分数约分
10.用sort函数实现数组排序
(升序为从小到大 )
用头文件
#include<algorithm>
数组:
1.sort(A)升序排列数组内数据;
2.sort(A,'descend') 降序排列数组内数据;
3.[s,indes]=sort(A) s为升序之后的答案,indes为原来数据的索引;
矩阵:
4.sort(A)对列进行升序排列;
5.sort(A,2)对行进行升序排列;
6.sort(A,1,'descend')对列进行降序排序;
7.sort(A,2,'descend')对行进行降序排序;
8.sortrows(A,1)表示对第一行进行升序排序,其他列根据第一列元素进行相应变化;
9.sortrows(A,-1)表示对第一行进行降序排序,其他列根据第一列元素进行相应变化;
soft函数简介
在 c++ 算法库里有一个强大的排序函数 sort ,使用方便,速度快,基本上能解决我们在平时遇到的大多数排序问题,使用 sort 函数需要包含算法库: #include <algorithm>
sort 排序
定义原型:
void sort (RandomAccessIterator first, RandomAccessIterator last);
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
对序列 [first,last) 区间的元素默认按升序,如有比较函数,按比较函数规则排序
sort 不是稳定排序(相同的元素在排序后相对位置会发生变化)
sort(begin, end, cmp)
begin :为指向待待排序数组的第一个元素的指针
end :为指向待排序数组的最后一个元素的下一个位置的指针
cmp :参数为排序的准则,默认以非降序排序
举例说明:
#include <algorithm>//算法库
int main() {
int a[5] = {1, 3, 4, 2, 5};
std::sort(a+0, a + 5);
for (int i = 0; i < 5; i++) printf("%d ",a[i]);
return 0;
}
自定义cmp参数,降序排序
#include <cstdio>
#include <algorithm>//算法库
bool cmp(int x, int y) {
return x > y;//这样是降序排序,改成return x<y;呢?
}
int main() {
int a[5] = {1, 3, 4, 2, 5};
sort(a+0, a + 5, cmp);
for (int i = 0; i < 5; i++) printf("%d ",a[i]);
return 0;
}
对基本数据类型的数组进行排序,我们也可以使用标准库函数进行排序,例如,上述排序的代
码可以修改为:
sort(a, a + 5, less<int>());
stable_sort 排序
#include <algorithm>//算法库
int main() {
int a[5] = {1, 3, 4, 2, 5};
std::sort(a+0, a + 5);
for (int i = 0; i < 5; i++) printf("%d ",a[i]);
return 0;
}
#include <cstdio>
#include <algorithm>//算法库
bool cmp(int x, int y) {
return x > y;//这样是降序排序,改成return x<y;呢?
}
int main() {
int a[5] = {1, 3, 4, 2, 5};
sort(a+0, a + 5, cmp);
for (int i = 0; i < 5; i++) printf("%d ",a[i]);
return 0;
}
对序列 [first,last) 区间的元素默认按升序,如有比较函数,按比较函数规则排序
stable_sort 是稳定排序(相同的元素在排序后相对位置不变)
partial_sort 排序
定义原型:
void partial_sort (RandomAccessIterator first, RandomAccessIterator
middle,RandomAccessIterator last);
重排元素,使得范围 [first, middle) 含有范围 [first, last) 中已排序的 middle - first
个最小元素。
范围 [middle, last) 中剩余的元素顺序未指定。
不稳定排序
默认用 operator< 比较元素。否则用给定的二元比较函数 comp 比较元素。
实现原理
1. 对原始容器内区间为 [first, middle) 的元素执行 make_heap() 操作构造一个最大堆
2. 拿 [middle, last) 中的每个元素和 first 进行比较, first 内的元素为堆内的最大值
3. 如果小于该最大值,则互换元素位置,并对 [first, middle) 内的元素进行调整,使其保持最大
堆序
4. 比较完之后在对 [first, middle) 内的元素做一次对排序 sort_heap() 操作,使其按增序排列。
5. 时间复杂度约 (last-first)log(middle-first) 次应用 cmp 。
结构体排序
1. sort 函数内部排序是用小于号进行比较的,所以我们只需对该类型结构体的小于号进行重载即可
2. 对于结构体排序,还可以用重载结构体或类的比较运算符的方法,上述代码可以修改为:
struct Stu{
int yw;
int sx;
bool operator <(const Stu& A) const {
if (yw == A.yw) return sx > A.sx;
else return yw < A.yw;
}
};
Stu a[1000];
sort(a+0,a+100);//对数组下标从0~99之间元素按照cmp排序规则排序
11.cumsum函数
1. cumsum(A,1)表示行的累加(第一行不变,第二行的元素+第一行的元素,第三行的元素+第二行的元素+第一行的元素……);
2.cumsum(A,2) 表示列的累加(第一列不变,第二列的元素+第一列的元素,第三列的元素+第二列的元素+第一列的元素……);
12.待更新
待插入