牛客华为机试108题练习(练习输入输出)

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
*/

结果:
在这里插入图片描述

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
108中有部分目重合,因此么有收录在压缩文件中。 华为机试 ├─001 字符串最后一个单词长度 │ └─Source ├─002 计算字符个数 │ └─Source ├─003 明明的随机数 │ └─Source ├─004 字符串分隔 │ └─Source ├─005 进制转换 │ └─Source ├─006 质数因子 │ └─Source ├─007 取近似值 │ └─Source ├─008 合并表记录 │ └─Source ├─009 提取不重复的整数 │ └─Source ├─010 字符个数统计 │ └─Source ├─011 数字颠倒 │ └─Source ├─012 字符串反转 │ └─Source ├─013 句子逆序 │ └─Source ├─014 字典序排序 │ └─Source ├─015 求int型正整数在内存中存储是1的个数 │ └─Source ├─016 购物单 │ ├─Debug │ ├─Source │ │ └─Debug │ ├─Source - 时间优先 │ │ └─Debug │ └─Source - 空间优先 │ ├─Debug │ └─Release ├─017 坐标移动 ├─018 识别IP地址分类统计 │ └─Source │ └─Debug ├─019 错误记录 ├─020 密码验证合格程序 ├─021 密码破解 ├─023 删除字符串中出现次数最少字符 │ └─Source │ └─Debug ├─024 合唱队 │ └─Source │ ├─Debug │ └─Release ├─025 数据分类处理 │ └─Source │ └─Debug ├─026 查找兄弟单词 │ └─Source │ └─Debug ├─027 素数伴侣 │ └─Source │ └─Debug ├─028 字符串合并处理 │ └─Source │ └─Debug ├─030 密码截取(查找最长回文字符串) ├─031 蛇形矩阵 │ └─Source │ └─Debug ├─033 判断IP是否属于同一子网 │ └─Source │ └─Debug ├─034 称砝码 │ └─Source │ └─Debug ├─035 学英语 │ └─Source │ └─Debug ├─036 迷宫问 │ └─Source │ └─Debug ├─037 数独问 │ └─Debug ├─038 名字漂亮度 │ └─Source │ └─Debug ├─039 字符串截取 │ └─Source │ └─Debug ├─040 单链表删除数据 │ └─Source │ └─Debug ├─041 多线程 │ └─Source │ ├─Backup │ ├─Debug │ │ └─041.tlog │ └─Release │ └─041.tlog ├─042 表达式计算 │ └─Source │ └─Debug ├─043 计算字符串距离 │ └─Source │ └─Debug ├─044 杨辉三角形变形 ├─046 挑7 ├─047 完全数 │ └─Debug ├─048 高精度加法 ├─049 输出n个数中最小的k个 │ └─Debug ├─050 找出字符串只出现一次的字符 │ └─Debug ├─051 组成一个偶数最接近的2个质数 │ └─Debug ├─052 M个苹果放入N个盘子 ├─053 查找整数二进制中1的个数 ├─054 DNA子串 ├─055 MP3光标位置 │ └─Source │ └─Debug ├─056 查找2个字符串最大相同子串 │ └─Debug ├─057 配置文件恢复 │ └─Source │ └─Debug ├─058 24点计算 │ └─Debug ├─059 成绩排序 ├─060 矩阵相乘 ├─061 矩阵乘法次数计算 ├─062 字符串通配符 │ └─Debug ├─066 命令行解析 │ └─Source │ └─Debug ├─067 最大相同子串长度 │ └─Debug ├─068 火车编号进站 │ └─Debug ├─072 数组合并 ├─074 埃及分数 │ └─Source │ └─Debug ├─076 密码截取 │ └─Source ├─077 求最大连续bit数 ├─078 密码强度 ├─079 扑克牌大小 │ └─Source │ └─Debug ├─081 合法IP ├─082 棋盘格子走法 ├─083 在字符串中找出连续最长数字串 ├─084 int数组分组,两组和相等 │ └─Source │ └─Debug ├─086 人民币转换 │ └─Source │ └─Debug ├─087 表示数字 ├─090 自动售货系统 │ └─Source │ └─Debug └─091 24点输出 └─Debug
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值