ACM算法基础工具

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int cmp(int a,int b)//从大到小
{
    return a>b;
}

void print(int *temp,int n)//打印数组
{
    for(int i=0;i<=n-1;i++)
    {
        cout<<temp[i]<<" ";
    }
    cout<<endl;
}
int main()
{
    int a[10]={9,9,7,7,6,5,4,4,3,2};
    int b[11]={9,9,7,7,6,5,4,4,3,2,100};

    int *s=max_element(a,a+10);//最大元素
    cout<<*s<<endl;//9

    s=min_element(a,a+10);//最小元素
    cout<<*s<<endl;//2

    int N=unique(a,a+10)-a;//去重,返回去重后数组的尾地址
    cout<<N<<endl;//2
    print(a,N);

    sort(b,b+10);
    print(b,10);

    sort(b,b+10,cmp);
    print(b,10);

    ///以下两个为初始化函数
    memset(b,0,sizeof(b)); //按位初始化,只能初始化为0 1 -1
    print(b,11);

    int x;
    cin>>x;
    fill(b,b+10,x);//按一定区间[b,b+10)初始化为x
    print(b,11);

    return 0;
}

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

int main()
{
    char str[5]="abcd";
    int len=strlen(str);
    cout<<str<<endl;
    while(next_permutation(str,str+len))
    {
        //从这个区间向后查找还有字典序吗?没有的话返回false
        cout<<str<<endl;
    }

    char str1[5]="dcba";
    len=strlen(str1);
    cout<<str1<<endl;
    while(prev_permutation(str1,str1+len))
    {
        //从这个区间向前查找还有字典序吗?没有的话返回false
        cout<<str1<<endl;
    }

    return 0;
}

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;

int main()
{
    int data;
    data=atoi("123");//a char  字符串转换成整型 同理 atol atoll atof strtoi stroll strtof
    cout<<data<<endl;

    char buf[512];
    sprintf(buf,"%o",data);//转换八进制,等价于buf=itoa(data);
    cout<<buf<<endl;
    //int float double long long  string

    int i=1750;
    char buffer[33];
    itoa(i,buffer,10);//i转换成十进制保存到字符串中
    printf("decimal:%s\n",buffer);
    itoa(i,buffer,16);//i转换成十六进制保存到字符串中
    printf("hexadecimal:%s\n",buffer);
    itoa(i,buffer,2);//i转换成二进制保存到字符串中
    printf("binary:%s\n",buffer);
    return 0;
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值