108题不是很难,但是可以很好的练习输入输出。
c++的几个内置函数
islower(char c) 是否为小写字母
isupper(char c) 是否为大写字母
isdigit(char c) 是否为数字
isalpha(char c) 是否为字母
isalnum(char c) 是否为字母或者数字
toupper(char c) 字母小转大
tolower(char c) 字母大转小
整数转字符串:
to_string(整数);
字符串 p 转整数 a :
int a=atoi(p.c_str());
或者:a=stol(p);
进制转换:
十进制n 转十六进制 cout<<hex<<n<<endl;
转八进制 cout<<oct<<n<<endl;
装十进制 cout<<dec<<n<<endl;
十进制转二进制:(位运算)
#include <iostream>
using namespace std;
void printbinary( int val)
{
//表示转换成二进制输出格式的位数0-7 有8位
for (int i = 7; i >= 0; i--)
{
if (val & (1 << i))//1左移7位和原数进行&运算,1左移6位和原数进行&运算...
cout << "1";
else
cout << "0";
}
}
int main()
{
printbinary(10);
system("pause");
return 0;
}
结果:
cin输入与getline输入 区别
具体见:http://c.biancheng.net/view/1345.html
虽然可以使用 cin 和 >> 运算符来输入字符串,但它可能会导致一些需要注意的问题。
当 cin 读取数据时,它会传递并忽略任何前导白色空格字符(空格、制表符或换行符)。
一旦它接触到第一个非空格字符即开始阅读,当它读取到下一个空白字符时,
它将停止读取。
getline 只是输入字符串
为了解决这个问题,可以使用一个叫做 getline 的 C++ 函数。
此函数可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中。
计算加法进位次数
int main()
{
int a, b, ans, c;
while (cin >> a >> b)
{
ans = 0; c = 0;
for (int i = 1; i <= 9; i++)//循环多少次,没关系
{
c = (a % 10 + b % 10 + c) > 9 ? 1 : 0;
ans += c;
a = a / 10; b = b / 10;
}
cout << ans << endl;
}
system("pause");
return 0;
}
1.输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
输入描述:
输入一行字符串,可以有空格
输出描述:
统计其中英文字符,空格字符,数字字符,其他字符的个数
输入
1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][
输出
26
3
10
12
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
//while(cin>>str){
while(getline(cin, str)){
int english_num=0, kong_num=0, digit_num=0, other_num=0;
for(auto i:str){
// if(i>='a' && i<='z'){
if(isalpha(i)){
english_num++;
}
else if(i==' '){
kong_num++;
}
else if(isdigit(i)){
digit_num++;
}
else{
other_num++;
}
}
// if(english_num>0) cout<<english_num<<endl;
//if(kong_num>0) cout<<kong_num<<endl;
//if(digit_num>0) cout<<digit_num<<endl;
cout<<english_num<<endl<<kong_num<<endl<<digit_num<<endl<<other_num<<endl;
}
return 0;
}
2.迷宫问题
题目描述
定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。入口点为[0,0],既第一空格是可以走的路。
输入描述:
输入两个整数,分别表示二位数组的行数,列数。再输入相应的数组,其中的1表示墙壁,0表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。
输出描述:
左上角到右下角的最短路径,格式如样例所示。
示例1
输入
复制
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
输出
复制
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(4,4)
思路:DFS回溯
#include<iostream>
#include<vector>
using namespace std;
int N, M; //分别代表行和列
vector<vector<int>> maze;//迷宫矩阵
vector<vector<int>> path_temp;//存储当前路径,第一维表示位置
vector<vector<int>> path_best;//存储最佳路径
void MazeTrack(int i, int j)
{
maze[i][j] = 1;//表示当前节点已走,不可再走
path_temp.push_back({ i, j });//将当前节点加入到路径中
if (i == N - 1 && j == M - 1) //判断是否到达终点
if (path_best.empty() || path_temp.size() < path_best.size())
path_best = path_temp;
if (i - 1 >= 0 && maze[i - 1][j] == 0)//探索向上走是否可行
MazeTrack(i - 1, j);
if (i + 1 < N && maze[i + 1][j] == 0)//探索向下走是否可行
MazeTrack(i + 1, j);
if (j - 1 >= 0 && maze[i][j - 1] == 0)//探索向左走是否可行
MazeTrack(i, j - 1);
if (j + 1 < M && maze[i][j + 1] == 0)//探索向右走是否可行
MazeTrack(i, j + 1);
//当程序执行到这里的时候,说明某个点的上下左右4个方向都不能走(或走过了),那么就可以执行下面的恢复现场了。
maze[i][j] = 0; //恢复现场,设为未走
path_temp.pop_back(); //顺便把路径里的那个点pop掉
}
int main()
{
while (cin >> N >> M)
{
maze = vector<vector<int>>(N, vector<int>(M, 0));
path_temp.clear();
path_best.clear();
for (auto &i : maze)
for (auto &j : i)
cin >> j; //从开始到这儿是手动输入一个二维矩阵
MazeTrack(0, 0);//回溯寻找迷宫最短通路
for (auto i : path_best)//按输出格式输出
cout << '(' << i[0] << ',' << i[1] << ')' << endl;//输出通路
}
return 0;
}
3.字符统计,table_sort 稳定排序,pair<char,int>
输入描述:
输入一串字符。
输出描述:
对字符中的
各个英文字符(大小写分开统计),数字,空格进行统计,并按照统计个数由多到少输出,如果统计的个数相同,则按照ASII码由小到大排序输出 。如果有其他字符,则对这些字符不用进行统计。
示例1
输入
aadddccddc
输出
dca
思路:
第一步: 使用map<char, int> 统计每个字符的个数, map默认按字典排序
第二步: 将map中的数据拷贝到vector<pair<char, int> > 中
第三步: 使用stable_sort()排序, 注意需要自己传入谓词, 以便按照字符数量大小排序
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
bool isSorter(const pair<char, int> &s1, const pair<char, int> &s2) {
return s1.second > s2.second;
}
int main() {
string str;
while (cin >> str) {
map<char, int> mp;
for (auto i : str) {
mp[i]++;
}
vector<pair<char, int>> tmp(mp.begin(), mp.end());
stable_sort(tmp.begin(), tmp.end(), isSorter);
for (auto p : tmp) {
cout << p.first;
}
cout << endl;//没有这个通不过,因为也是为了输入多个测试案例,输完一组,换行才能输入第二组数据
}
return 0;
}
有 cout << endl; 光标在这:
没有 cout << endl; 光标在这,那肯定不过。
4.两数之和
代码
#include<iostream>
#include<vector>
#include<string>
#include<map>
using namespace std;
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> a;//提供一对一的hash
vector<int> res;//用来承载结果
for (int i = 0; i < nums.size(); i++)
{ //先找再存,这样有先后关系,在已经存的元素中去找可以凑成2sum对的元素,防止同一个数被使用两次
if (a.count(target - nums[i]))
{
res.push_back(nums[a[target - nums[i]]]);
res.push_back(nums[i]);
break;
}
a[nums[i]] = i;//反过来放入map中,用来获取结果下标
}
return res;
}
int main() {
vector<int> nums;
int i;
int target;
while (cin >> i) {
nums.push_back(i);
if (cin.get() == '\n') break;
}
cin >> target;
vector<int> res= twoSum(nums, target);
//输出格式不知道和题目要求的一样不
cout << "["<<res[0]<<","<<res[1] <<"]"<< endl;
system("pause");
return 0;
}
5.360公司某题
#include<iostream>
#include<string>
using namespace std;
int main() {
string str;
cin >> str;
string str1 = "";
//for (auto &i : str) {
char i = toupper(str[0]);
str1 = str1 + i;
//范围for循环从第二位开始
for(auto beg =str.begin()+1,end=str.end();beg!=end;++beg){
auto &i = *beg;
if (i == 'n') {
cout << str1 << endl;
str1 = "";
i = toupper(i);
}
str1 = str1 + i;
}
cout << str1 << endl;
system("pause");
return 0;
}
输出:
华为笔试第一题
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main() {
vector<string> res;
string tmp;
int n;
string gt;
while (1) {
cin >> tmp;
if (tmp.size() > 1) {
res.push_back(tmp);
}
else if (tmp.size() == 1) {
n = stol(tmp);
cin >> gt;
break;
}
}
cout << "----------" << endl;
vector<string> tmpp;//当前字符串特征值
for (auto &i : res) {
string s="";
for (auto &j : i) {
if (j-'0' < n) {
s += j;
}
}
tmpp.push_back(s);
}
for (auto i : tmpp) {
cout << i << endl;
}
cout << "----------" << endl;
string rt="";//给定字符串特征值
for (auto &k : gt) {
if (k-'0' <n) rt += k;
}
cout << rt << endl;
cout << "--------------" << endl;
for (int i = 0; i < tmpp.size();i++) {
if (tmpp[i].find(rt)!=-1) {//找不到会返回-1;
cout << res[i]<< endl;
}
}
system("pause");
return 0;
}
/*
135682318
23457
14282123
14231728
3
1724153
*/
结果: