ACM模式机试

1、键盘输入常用函数

C++-基础语法之cin.getline() 与 cin.get() 的区别,以及getline()函数使用方法_sunyuxiu的博客-CSDN博客_cin.get()函数和cin.getline

cin、cin.get()、cin.getline()、getline()的区别_啊大1号的博客-CSDN博客_cin getline

getline()函数详解_C-K方程的博客-CSDN博客

#include<iostream>
#include<string>

using namespace std;

//cin>>
void test1(char* dst)
{
	cout << "cin>> test" << endl;
	cout << "please input your string" << endl;
	cin >> dst;
	cout << "your string is:" << endl;
	cout << dst;
}

//cin.get(char)/char=cin.get()
void test2(char* dst)
{
	cout << "cin.get(char)/char=cin.get() test" << endl;
	cout << "please input your string" << endl;
	//*dst=cin.get();
	cin.get(*dst);
	cout << "your string is:" << endl;
	cout << dst;
}

//cin.get(dst,size)
void test3(char* dst)
{
	cout << "cin.get(dst,size) test" << endl;
	cout << "please input your string" << endl;
	cin.get(dst,10);
	cout << "your string is:" << endl;
	cout << dst;
}

//cin.get(dst,size,'char')
void test4(char* dst)
{
	cout << "cin.get(dst,size,'char') test" << endl;
	cout << "please input your string" << endl;
	cin.get(dst, 10,'z');
	cout << "your string is:" << endl;
	cout << dst;
	cout << endl;
	//cin buffer
	cout << "cin buffer is:" << endl;
	memset(dst, '\0', sizeof(dst));
	cin >> dst;
	cout << dst << endl;
}

//cin.get()&cin.ignore()
void test5(char* dst)
{
	cout << "cin.get(dst,size,'char') test" << endl;
	cout << "please input your string" << endl;
	cin.get(dst, 10, 'z');
	cout << "your string is:" << endl;
	cout << dst;
	cout << endl;
	//clear cin buffer:ignore one char
	//cin.ignore();
	cin.ignore(1024, '\n');
	//cin buffer
	cout << "cin buffer is:" << endl;
	memset(dst, '\0', sizeof(dst));
	cin >> dst;
	cout << dst << endl;
}

//cin.getline()
void test6(char* dst)
{
	cout << "cin.getline(dst,size,'char') test" << endl;
	cout << "please input your string" << endl;
	//cin.getline(dst, 10, 'z');
	cin.getline(dst, 10);
	cout << "your string is:" << endl;
	cout << dst;
	cout << endl;
	//cin buffer
	cout << "cin buffer is:" << endl;
	memset(dst, '\0', sizeof(dst));
	cin >> dst;
	cout << dst << endl;
}

//<string>:getline()
void test7(string& dst_str)
{
	cout << "getline test" << endl;
	cout << "please input your string" << endl;
	//cin.getline(dst, 10, 'z');
	//getline(cin,dst_str);
	getline(cin, dst_str,'#');
	cout << "your string is:" << endl;
	cout << dst_str;
	cout << endl;
}


//difference between cin>> ,cin.get(),cin.getline()
int main()
{
	char dst[200];
	memset(dst,'\0',sizeof(dst));
	string dst_str;
	//test5(dst);
	test7(dst_str);
	return 0;
}

2、元素之间利用空格输入,建立矩阵

#include<iostream>
#include<vector>

using namespace std;

//空格输入矩阵

//difference between cin>> ,cin.get(),cin.getline()
int main()
{
	int m, n;
	cin >> m >> n;
	vector<vector<int>> vec2d(m, vector<int>(n, 0));
	for (int i = 0; i < m; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			cin>>vec2d[i][j];
		}
	}
	cout << "your vec2d is" << endl;
	for (int i = 0; i < m; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			cout << vec2d[i][j]<<' ';
		}
		cout << endl;
	}
	return 0;
}

3、元素之间利用' , '输入,建立矩阵

#include<iostream>
#include<vector>

using namespace std;

//逗号输入矩阵
//必须紧密相连
//difference between cin>> ,cin.get(),cin.getline()
int main()
{
	int m, n;
	cin >> m >> n;
	vector<vector<int>> vec2d(m, vector<int>(n, 0));
	for (int i = 0; i < m; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			cin>>vec2d[i][j];
			//clear ','
			cin.get();
		}
	}
	cout << "your vec2d is" << endl;
	for (int i = 0; i < m; ++i)
	{
		for (int j = 0; j < n-1; ++j)
		{
			cout << vec2d[i][j]<<' ';
		}
		cout << vec2d[i][n-1] <<endl;
	}
	return 0;
}

4、建立链表基础模板

#include<iostream>
#include<vector>

using namespace std;

//Definition for singly-linked list.
 struct ListNode {
     int val;
     ListNode *next;
     ListNode() : val(0), next(nullptr) {}
     ListNode(int x) : val(x), next(nullptr) {}
     ListNode(int x, ListNode *next) : val(x), next(next) {}
 };
 
 void PrintList(ListNode* head)
 {
     ListNode* cur = head;
     while (cur)
     {
         cout<<cur->val<<endl;
         cur = cur->next;
     }
 }


int main()
{
    int n;
    cin >> n;
    ListNode* head = nullptr;
    ListNode* cur = nullptr;

    for (int i = 0; i < n; ++i)
    {
        int val;
        cin >> val;
        ListNode* tmp = new ListNode(val);

        if (!head)
        {
            head = tmp;
            cur = head;
        }
        else
        {
            cur->next = tmp;
            cur = cur->next;
        }
    }

    PrintList(head);

	return 0;
}

5、手写2-32进制转10进制,和10进制转2-32进制

#include<iostream>
#include<string>
#include<stdio.h>
#include<algorithm>


using namespace std;

int Others2Dec(string s, int radix)
{
	int ans = 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] >= '0' && s[i] <= '9')
		{
			ans =ans*radix+ (s[i] - '0');
		}
		else
		{
			ans = ans * radix + (s[i] - 'a') + 10;
		}
	}
	return ans;
}

//
string Dec2Others(int n,int radix)
{
	string ans;
	//防止n=0
	do {
		int t = n % radix;
		if (t >= 0 && t <= 9) ans += t + '0';
		else ans += t - 10 + 'a';
			n /= radix;
	} while (n != 0);
	reverse(ans.begin(), ans.end());
	return ans;
}


int main()
{
	string s;
	cin >> s;
	int n1 = Others2Dec(s, 16);
	cout << n1 << endl;
	cout << Dec2Others(n1,16) << endl;
	char tmp[10] = {0};
	sprintf_s(tmp, "%4x", n1);
	cout << tmp << endl;
	return 0;
}

6、文件读写

#include<iostream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;

//从cin读取多行数据,写入file(覆盖或者新建)
//从file读取文件并按行输出

int main()
{
	//write
	string file("./myfile.txt");
	ofstream out;
	//in out app ate trunc
	out.open(file, ios::out);
	if (!out)
	{
		cerr << "err out" << endl;
		return -1;
	}
	int num;
	cin >> num;
	string tmp;
	for (int i = 0; i < num; i++)
	{
		cin >> tmp;
		out << tmp << endl;
	}
	out.close();
	//read
	ifstream in(file);
	vector<string> vec;
	if (!in)
	{
		cerr << "err int" << endl;
		return -1;
	}
	while (getline(in, tmp))
	{
		vec.push_back(tmp);
	}
	in.close();
	for (auto s : vec)
		cout << s << endl;
	return 0;
}

7、getline(istream&,string,char)分隔符最好显式标出,不然有可能出错!!!

#include<iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include<sstream>

using namespace std;

string GetName(string s)
{
    istringstream is(s);
    string name;
    while (getline(is, name, '\\')) {}
    int len = name.length();
    if (len <= 16)
        return name;
    else
        return name.substr(len-16);
}

int main() {

    string line;
    vector<string> record;
    unordered_map<string, int> mp;//<string,num>
    //
    while (getline(cin, line,'\n'))
    {
        istringstream is(line);
        string name, row;
        getline(is, name,' ');
        getline(is, row);
        name = GetName(name);
        string key = name + ' ' + row;
        if (mp.find(key) != mp.end())
        {
            mp[key]++;
        }
        else
        {
            record.push_back(key);
            mp[key]++;
        }
    }
    //
    int n = record.size();
    int start=(n-8>=0)?(n-8):0;
    for (int i = start;i<n; i++)
    {
        cout << record[i] << ' ' << mp[record[i]] << endl;
    }
    return 0;
}

8、istringstream不忽略流中的首个空格或者换行,需要ignore()

#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

int main() {

    int n;
    vector<string> vec;
    //注意n之后有空格
    istringstream is(" 15 123 456 786 453 46 7 5 3 665 453456 745 456 786 453 123\n");
    string line;
    is >> n;
    getline(is, line, '\n');
    istringstream is2(line);
    //is2.ignore()少不了,否则第一个分割的字符串是空串
    is2.ignore();
    for (int i = 0; i < n; i++)
    {
        string s;
        getline(is2, s, ' ');
        vec.push_back(s);
    }
    for (auto val : vec)
    {
        cout << val << endl;
    }
    return 0;
}

9、string的find函数(以及cin输入选好数据类型,int类型别用string处理,引来麻烦)

string的find( )函数✅_<Running Snail>的博客-CSDN博客_string的find函数

string find()函数、string::npos的含义、erase()函数_Mongo_girl的博客-CSDN博客

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s = "hello world!";
	string obj = "llo";
	string others = "abcd";
	cout << s.find('e') << endl;
	cout << s.find(obj) << endl;
	cout << s.find_first_of('l') << endl;
	cout << s.find_last_of('l') << endl;
	//不存在的位置
	cout << s.find(others) << endl;
	cout << s.npos << endl;
	return 0;
}

10、unique函数

unique 函数 详解_生于忧患,死于安乐2017的博客-CSDN博客_unique函数

#include<vector>
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
	vector<int> vec1 = { 7,8,9,2,3,4,4,4,3,2,1,0,10,11 };
	vector<int> vec2(vec1);
	//如果不排序
	vec1.erase(unique(vec1.begin(), vec1.end()), vec1.end());
	for (auto val : vec1)
	{
		cout << val << ' ';
	}
	cout << endl;
	//排序
	sort(vec2.begin(), vec2.end());
	vec2.erase(unique(vec2.begin(), vec2.end()), vec2.end());
	for (auto val : vec2)
	{
		cout << val << ' ';
	}
	cout << endl;
	return 0;
}

11、仿函数的书写(HJ26字符串排序)

字符串排序_牛客题霸_牛客网

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(pair<char, int> a, pair<char, int> b)
{
    char ca = a.first;
    char cb = b.first;
    if (ca >= 'a' && ca <= 'z') ca -= 'a' - 'A';
    if (cb >= 'a' && cb <= 'z') cb -= 'a' - 'A';
    if (ca == cb)
    {
        return (a.second < b.second);
    }
    else
    {
        return (ca < cb);
    }
}

int main() {
    string s;
    cin >> s;
    int len = s.length();
    vector<pair<char, int>> vec;
    //存放非字母下标
    vector<int> Idx;
    for (int i = 0; i < len; i++)
    {
        if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z'))
        {
            vec.push_back({s[i],i});
        }
        else
        {
            Idx.push_back(i);
        }
    }
    sort(vec.begin(),vec.end(),cmp);
    int m = 0, n = 0;
    for (int i = 0; i < len; i++)
    {
        if (m<Idx.size() && i == Idx[m])
        {
            cout << s[Idx[m++]];
        }
        else if(n<vec.size())
        {
            cout << vec[n++].first;
        }
    }
    cout << endl;
    return 0;
}

12、字典序排列问题

leetcode31

#include<vector>
#include<iostream>
#include<algorithm>

using namespace std;

/*自己手写的next_permutation*/
void nextPermutation(vector<int>& nums) {
    int n = nums.size();
    int i = n - 2;
    while (i >= 0 && nums[i] >= nums[i + 1])
    {
        i--;
    }
    int j = n - 1;
    if (i >= 0)
    {
        while (j > i && nums[j] <= nums[i])
        {
            j--;
        }
        if (j > i && nums[j] > nums[i])
        {
            swap(nums[i], nums[j]);
        }
    }
    reverse(nums.begin() + i + 1, nums.end());
}

void Print(vector<int>& vec)
{
    for (auto val : vec)
    {
        cout << val<<' ';
    }
    cout << endl;
}

int main()
{
	vector<int> vec = { 3,1,2 };
    int mode = 1;
    if (mode == 0)
    {
        sort(vec.begin(), vec.end());
        /*为了判断回到起点了*/
        vector<int> origin = vec;
        Print(vec);
        nextPermutation(vec);
        while (vec != origin)
        {
            Print(vec);
            nextPermutation(vec);
        }
    }
    else
    {
        sort(vec.begin(), vec.end());
        Print(vec);
        while (next_permutation(vec.begin(), vec.end()))
        {
            Print(vec);
        }
    }
	return 0;
}

13、琐碎的注意事项

(1)sort()等函数的迭代器必须写对,如头迭代器和尾迭代器来自不同结构,会造成数组越界。

如sort(s1.begin(),s2.end());

14、C/C++标准输入输出格式控制

C++输出格式控制大全_爱吃的板栗.的博客-CSDN博客_c++格式控制符

C/C++标准输入输出格式控制_Medlen的博客-CSDN博客_c++限制输入格式

C++输入输出格式控制_hesorchen的博客-CSDN博客_c++输入输出格式控制

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<bitset>

using namespace std;

void C_Test()
{
	
}

//成员函数   或者  流操纵算子也可以
void CPP_Test()
{
	//整数输入输出格式
	int val = 255;
	//cin >> hex >> val;
	cout << dec << val << endl;
	cout << oct << val << endl;
	cout << bitset<8>(val) << endl;
	//小数输入输出格式,设置一次输出格式,对后续都有效,除非更改或者取消
	double a = 3.141592653589793;
	//设置16位有效数字
	cout << setprecision(3) << a << endl;
	//设置小数点后保留16位
	cout << setprecision(3) << setiosflags(ios::fixed) << a << endl;
	//重置标志位
	cout << resetiosflags(ios::fixed);
	//查看
	cout << a << endl;
}

int main()
{
	/*
	测试:二~十六进制输入输出整数
	
	*/
	C_Test();
	CPP_Test();
	return 0;
}

15、bitset设置

bitset——定义及常用操作。_小酒窝.的博客-CSDN博客_bitset操作

c++bitset用法详解(超简单)——蒟蒻函数_彗星七号的博客-CSDN博客_c++bitset用法

#include<bitset>
#include<string>
#include<algorithm>
#include<iostream>

using namespace std;

int main()
{
	bitset<32> b(253);
	//bitset的基本操作
	cout << b.any() << endl;
	//int和bitset
	cout << b << endl;
	//string 和 bitset
	string s("11");
	bitset<4> b2(s);
	b2.flip(3);
	cout << b2 << endl;
	return 0;
}

16、sscanf和ssprintf函数

通过sprintf函数来实现的进制转换_weixin_34370347的博客-CSDN博客

sprintf()函数的用法_wangkeyen的博客-CSDN博客_sprintf

sscanf的用法_yanyanwenmeng的博客-CSDN博客_sscanf函数的用法

sscanf函数使用详解_faihung的博客-CSDN博客_sscanf

17、任意进制之间的两两互换

C++中的各种进制转换函数汇总_小·幸·运的博客-CSDN博客_c++进制转换

c/c++进制转换方法汇总(含全部代码)_lady_killer9的博客-CSDN博客_c++八进制转二进制

此外应当注意一下memset函数

C++学习——memset函数详解_流楚丶格念的博客-CSDN博客_c++中memset是什么意思
void *memset(void *s, int ch, size_t n);

任意进制互换需要通过10进制中间量

#include<iostream>
#include<cstdio>
#include<string>
#include<sstream>
#include<bitset>
#include<algorithm>

using namespace std;

//其他string转10进制int
int O2Dec(string s,int radix)
{
	int val = 0;
	for (auto c : s)
	{
		if (c >= '0' && c <= '9')
		{
			val=val*radix + c - '0';
		}
		else
		{
			val = val * radix + c - 'A'+10;
		}
	}
	return val;
}

//10进制int转其他string
string Dec2O(int val, int radix)
{
	string s;
	do
	{
		int tmp = val % radix;
		if (tmp >= 0 && tmp <= 9)
		{
			s += tmp + '0';
		}
		else
		{
			s += tmp - 10 + 'A';
		}
		val /= radix;
	} while (val != 0);
	reverse(s.begin(),s.end());
	return s;
}

//任意进制互换


int main()
{
	string s("FD");
	int val;
	//1.利用sstream转化
	stringstream ss;
	ss << hex << s;
	ss >> val;
	cout << val << endl;
	//2.自己手写的10进制转其他
	string s1 = Dec2O(val, 16);
	cout << s1 << endl;
	//3.自己手写的其他转10进制
	int val1 = O2Dec(s1,16);
	cout << val1 << endl;
	//4.自己手写的任意进制互转 16->2
	string h("FD");
	string b = Dec2O(O2Dec(h,16),2);
	cout << b << endl;
	//5.利用bitset来转换
	bitset<32> bs(b);
	int val3 = bs.to_ulong();
	cout << val3 << endl;
	return 0;
}

18、一些常用的数学函数(以及头文件)

C++/C++11中头文件cmath的使用_fengbingchun的博客-CSDN博客_cmath头文件包含什么

C/C++笔试必须熟悉掌握的头文件系列(二)——math.h/cmath_无鞋童鞋的博客-CSDN博客_cmath头文件包含什么

#include<iostream>
#include<cmath>

using namespace std;

int main()
{
	double a = 4;
	cout << pow(a, 2) << endl;
	cout << sqrt(a) << endl;
	cout << fabs(a) << endl;
	cout << abs(a) << endl;
	cout << log10(a) << endl;
	cout << log(a) << endl;
	return 0;
}

19、C++ upper_bound、lower_bound、binary_search函数

C++中lower_bound函数和upper_bound函数_AEP_WYK的博客-CSDN博客

C++ upper_bound()函数_freshhell的博客-CSDN博客_c++ upper_bound

upper_bound与lower_bound函数_qq_37357873的博客-CSDN博客_upper_bound

lower_bound(起始地址,结束地址,要查找的数值) 返回的是数值第一个大于等于出现的位置。

upper_bound(起始地址,结束地址,要查找的数值) 返回的是 第一个大于待查找数值 出现的位置。

binary_search(起始地址,结束地址,要查找的数值) 返回的是是否存在这么一个数,是一个bool值。
 

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

vector<int> v = {
	89,3,22,12,34,55,33,21,23,67,44,34,77,56,8
};

void Show(vector<int>& v)
{
	int n = v.size();
	for (int i = 0; i < n - 1; i++)
	{
		cout << v[i] << ' ';
	}
	cout << v[n - 1] << endl;

}

bool cmp(int val, int ele)
{
	return (val<ele);
}




int main()
{
	/*----------------upper_bound------------------*/
	//原始
	Show(v);
	//排序后
	sort(v.begin(), v.end());
	Show(v);
	//upper基本用法:寻找非递减数组中第一个大于val的元素,底层是采用二分法实现
	cout << *upper_bound(v.begin(),v.end(),55) << endl;
	/*----------------lower_bound------------------*/
	//lower基本用法:寻找非递减数组中第一个大于等于val的元素
	cout << *lower_bound(v.begin(), v.end(), 55) << endl;
	/*----------------binary_search------------------*/
	//二分法搜索是否存在val
	cout << binary_search(v.begin(), v.end(), 55) << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值