当前位置:网站首页>牛客网——华为题库(31~40)
牛客网——华为题库(31~40)
2022-06-22 10:03:00 【wrdoct】
华为题库
31.单词倒排
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string str = "";
getline(cin, str);
str += " ";
vector<string> vec;
string tmp = "";
for(char c : str){
if(!isalpha(c)){
if(!tmp.empty()){
vec.push_back(tmp);
tmp.clear();
}
}
else{
tmp += c;
}
}
reverse(vec.begin(), vec.end());
string res = "";
for(int i = 0; i < vec.size(); i++){
//cout << vec[i]<<endl;
res += vec[i];
res += " ";
}
res.pop_back();
cout << res << endl;
return 0;
}
32.密码截取
#include <bits/stdc++.h>
using namespace std;
int getLength(string str, int l, int r){
while(l >= 0 && r < str.size() && str[l] == str[r]){
l--;
r++;
}
return r - l - 1;
}
//最大回文子串
void process(string str, int& res){
//dp[i][j]:字符串s在[i, j]范围内最长的回文子串的长度为dp[i][j]。
int n = str.size();
vector<vector<bool>> dp(n, vector<bool>(n, false));
for(int i = 0; i < n; i++){
dp[i][i] = true;//一个字符的回文子序列长度就是1。
}
for(int i = n - 1; i >= 0; i--){
for(int j = i; j < n; j++){
//
if(str[i] == str[j]){
if(j - i <= 2){
dp[i][j] = true; //
}
else{
dp[i][j] = dp[i + 1][j - 1];
}
}
//取最长回文子串
if(dp[i][j] && j - i + 1 > res){
//s = str.substr(i, j - i + 1);
res = j - i + 1;
}
}
}
/*//中心扩展法 int n = str.size(); for(int i = 0; i < n; i++){ //ABA int l1 = getLength(str, i, i); //ABBA int l2 = getLength(str, i, i + 1); res = max(res, l1 > l2 ? l1 : l2); }*/
}
int main(){
string str = "";
cin >> str;
int res = 0;
process(str, res);
cout << res << endl;
return 0;
}
33.整数与IP地址间的转换
#include <bits/stdc++.h>
using namespace std;
void process(string strIP, long decIP, long& _decIP, string& _strIP){
stringstream iss(strIP);
string sTmp = "";
vector<long> nums; //long
while(getline(iss, sTmp, '.')){
nums.push_back(atol(sTmp.c_str()));
}
_decIP = nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3]; //位运算组装
_strIP += to_string((decIP >> 24) & 0xff); //取第一个八位二进制转换成字符 & 0xff
_strIP += ".";
_strIP += to_string((decIP >> 16) & 0xff);
_strIP += ".";
_strIP += to_string((decIP >> 8) & 0xff);
_strIP += ".";
_strIP += to_string(decIP & 0xff);
}
int main(){
string strIP = "";
long decIP = 0;
cin >> strIP;
cin >> decIP;
long _decIP = 0;
string _strIP = "";
process(strIP, decIP, _decIP, _strIP);
cout << _decIP << endl;
cout << _strIP <<endl;
return 0;
}
34.图片整理
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
struct cmp{
bool operator()(char& a, char& b){
return a < b; //前面小后面大 升序
}
};
int main(){
string str = "";
getline(cin, str);
sort(str.begin(), str.end(), cmp());
cout << str << endl;
return 0;
}
35.蛇形矩阵
#include <bits/stdc++.h>
using namespace std;
void process(int N){
/*if(N == 0){ cout << "" << endl; }*/
//第一行最后一个元素 = (N)*(N + 1) / 2;
//第一行第j列元素 = (j) * (j + 1) / 2;
//第i行第j列元素 = 第i - 1行j + 1列 - 1;
//最后一行只有一个元素 = 第一行最后一个元素 - N - 1;
int num = 1;
vector<vector<int>> matrix(N, vector<int>(N, 0));
for(int i = 0; i < N; i++){
for(int j = 0; j <= i; j++){
matrix[i - j][j] = num++; //
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N - i; j++){
//
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main(){
int N = 0;
cin >> N;
process(N);
return 0;
}
36.字符串加密
#include <bits/stdc++.h>
using namespace std;
//encoder
void encoder(string key, string str, string& res){
//如果单词中包含有重复的字母,只保留第1个,将所得结果作为新字母表开头
vector<char> s; //s[i]对应的便是26个字母中第i个字母的加密结果
for(int i = 0; i < key.size(); i++){
key[i] = tolower(key[i]); //全部转小写
if(find(s.begin(), s.end(), key[i]) == s.end()) //如果是第一次加入
s.push_back(key[i]);
}
for(char c = 'a'; c <= 'z'; c++){
if(find(s.begin(), s.end(), c) == s.end()) //如果是第一次加入
s.push_back(c);
}
/*for(char ch : s){ cout << ch; }*/
for(int i = 0; i < str.size(); i++){
if(isupper(str[i])){
res += s[str[i] - 'A'] - 32; //需要在转出来的小写字母基础上减32
}
else{
res += s[str[i] - 'a']; //
}
}
}
int main(){
string key = "";
cin >> key;
string str = "";
cin >> str;
string res = "";
encoder(key, str, res);
cout << res << endl;
return 0;
}
37.统计每个月兔子的总数
#include <bits/stdc++.h>
using namespace std;
int process(int n){
return n < 3 ? 1 : process(n - 1) + process(n - 2);
}
int main(){
int n = 0;
while(cin >> n){
int res = process(n);
cout << res << endl;
}
return 0;
}
38.求小球落地5次后所经历的路程和第5次反弹的高度
#include <bits/stdc++.h>
using namespace std;
void process(int num, double& distance, double& high){
double tmp = (double)num;
for(int i = 0; i < 5; i++){
distance += tmp * 2;
tmp = tmp / 2;
}
distance = distance - (double)num;
high = tmp;
}
int main(){
int num = 0;
cin >> num;
double distance = 0.0;
double high = 0.0;
process(num, distance, high);
printf("%lf\n", distance);
printf("%lf\n", high);
return 0;
}
39.判断两个IP是否属于同一子网
#include <bits/stdc++.h>
using namespace std;
string str2bin(int num){
if(num == 0) return "00000000";
//cout << "num = " << num << endl;
string binRes = "";
string help = "0123456789ABCDEF";
while(num){
binRes += help[num % 2]; //二进制
num /= 2;
}
reverse(binRes.begin(), binRes.end());
while(binRes.size() < 8){
binRes.insert(binRes.begin(), '0');
}
//cout << "binRes = " << binRes << endl;
return binRes;
}
bool noLegalYanMa(string zwYanMa){
string res = "";
stringstream iss(zwYanMa);
string tmp = "";
bool isNeg = false;
while(getline(iss, tmp, '.')){
if(tmp[0] == '-'){
isNeg = true;
tmp = tmp.substr(1, tmp.size() - 1);
}
//cout << "tmp = " << tmp << endl;
int size = tmp.size();
int num = 0;
for(int i = 0; i < size; i++){
num = num * 10 + (tmp[i] - '0');
}
if(isNeg) num = -num;
//cout << "num==" << num << endl;
if(num < 0 || num > 255) return true;
res += str2bin(num);
}
//cout << "res = " << res << endl;
bool notZero = true;
for(int j = 0; j < res.size(); j++){
if(res[j] == '0') {
notZero = false;
}
else{
if(!notZero){
return true;
}
continue;
}
}
return false;
}
string str2binStr(string s){
string res = "";
stringstream iss(s);
string tmp = "";
while(getline(iss, tmp, '.')){
//cout << "tmp = " << tmp << endl;
int size = tmp.size();
int num = 0;
for(int i = 0; i < size; i++){
num = num * 10 + (tmp[i] - '0');
}
//cout << "num==" << num << endl;
res += str2bin(num);
}
return res;
}
bool noLegalIP(string IP){
string res = "";
stringstream iss(IP);
string tmp = "";
bool isNeg = false;
while(getline(iss, tmp, '.')){
if(tmp[0] == '-'){
isNeg = true;
tmp = tmp.substr(1, tmp.size() - 1);
}
//cout << "tmp = " << tmp << endl;
int size = tmp.size();
int num = 0;
for(int i = 0; i < size; i++){
num = num * 10 + (tmp[i] - '0');
}
if(isNeg) num = -num;
//cout << "num==" << num << endl;
if(num < 0 || num > 255) return true;
}
return false;
}
string anWeiSum(string zwYanMa, string IP){
string s1 = str2binStr(zwYanMa);
string s2 = str2binStr(IP);
cout << s1 << endl;
cout << s2 << endl;
string res = "";
int i = s1.size() - 1;
int j = s2.size() - 1;
int carry = 0; //进位
while(i >= 0 || j >= 0){
int digitA = i >= 0 ? (s1[i--] - '0') : 0; //i--
int digitB = j >= 0 ? (s2[j--] - '0') : 0; //j--
int sum = digitA + digitB + carry;
carry = sum >= 2 ? 1 : 0;
sum = sum >= 2 ? (sum - 2) : sum;
res += to_string(sum);
}
if(carry == 1) res += "1";
reverse(res.begin(), res.end());
cout << res << endl;
return res;
}
string anWeiYu(string zwYanMa, string IP){
string s1 = str2binStr(zwYanMa);
string s2 = str2binStr(IP);
//cout << s1 << endl;
//cout << s2 << endl;
string res;
for(int i = 0; i < 32; i++){
if(s1[i] != s2[i]){
res += "0";
}
else if(s1[i] == '0' && s2[i] == '0'){
res += "0";
}
else{
//s1[i] == '1' && s2[i] == '1'
res += "1";
}
}
//cout << res << endl;
return res;
}
bool isSameZiWang(string zwYanMa, string firstIP, string secondIP){
if(anWeiYu(zwYanMa, firstIP) == anWeiYu(zwYanMa, secondIP)){
return true;
}
return false;
}
void process(string zwYanMa, string firstIP, string secondIP, int& res){
if(noLegalYanMa(zwYanMa) || noLegalIP(firstIP) || noLegalIP(secondIP)){
res = 1;
return;
}
if(isSameZiWang(zwYanMa, firstIP, secondIP)){
res = 0;
}
else{
res = 2;
}
return;
}
int main(){
string zwYanMa = "";
string firstIP = "";
string secondIP = "";
getline(cin, zwYanMa);
getline(cin, firstIP);
getline(cin, secondIP);
int res = 0;
process(zwYanMa, firstIP, secondIP, res);
//anWeiYu(zwYanMa, firstIP);
cout << res << endl;
return 0;
}
40.统计字符
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "";
while (getline(cin, str))
{
int alphaNum = 0;
int spaceNum = 0;
int digitNum = 0;
int punctNum = 0;
for(char c : str){
if(isalpha(c)) alphaNum++;
else if(isspace(c)) spaceNum++;
else if(isdigit(c)) digitNum++;
else if(ispunct(c)) punctNum++;
}
cout << alphaNum << endl << spaceNum << endl << digitNum << endl << punctNum << endl;
}
return 0;
}
边栏推荐
- 三个月让软件项目成功“翻身”!
- Target detection / segmentation training using pytorch mask RCNN
- day260:只出现一次的数字 III
- [学习笔记] 回滚莫队
- 使用 Matplotlib 这么久,竟不知道数据可以动起来
- Xidian AI ranked higher than Qingbei in terms of AI major, and Nantah ranked first in China in terms of Software Science in 2022
- 使用pytorch mask-rcnn进行目标检测/分割训练
- [hdu] P6964 I love counting
- 信号完整性(SI)电源完整性(PI)学习笔记(二十四)差分对与差分阻抗(四)
- 传iPhone 14将全系涨价;TikTok美国用户数据转移到甲骨文,字节无法访问;SeaTunnel 2.1.2发布|极客头条...
猜你喜欢

Software project management 8.3 Agile project quality activities

信号完整性(SI)电源完整性(PI)学习笔记(二十四)差分对与差分阻抗(四)

Three months to successfully "turn over" the software project!

蓝牙、wifi、zigbee和lora、NB-lot,通话信号,网络信号4G
![[structure training camp - module 3]](/img/89/2972c9ee29af5b14731cf2e28784c9.jpg)
[structure training camp - module 3]

秋招秘籍A
![[LineCTF2022]BB](/img/9d/c5b0ce1e603d40efc5245ba074aa77.png)
[LineCTF2022]BB

HMS Core新闻行业解决方案:让技术加上人文的温度

Cobalt Strike 从入门到入狱(三)
![[学习笔记] 回滚莫队](/img/19/d374dd172b9609a3f57de50791b19e.png)
[学习笔记] 回滚莫队
随机推荐
【backtrader源码解析51】observers中七个文件源代码简单解读(枯燥,仅供参考)
Signal integrity (SI) power integrity (PI) learning notes (XXIV) differential pair and differential impedance (IV)
秋招秘籍C
科研奖励,创业奖励评定
Target detection / segmentation training using pytorch mask RCNN
一句话木马上传常见的几种方法
Zuckerberg's latest VR prototype is coming. It is necessary to confuse virtual reality with reality
Learning serialization and deserialization from unserialize3
It is said that the price of the iPhone 14 will rise; TikTok US user data is transferred to Oracle, and bytes cannot be accessed; Seatunnel 2.1.2 releases geek headlines
MySQL skip scan range small function to solve big problems?
C语言编写一个双向链表
Cobalt Strike 从入门到入狱(三)
不要再傻傻分不清 hash、 chunkhash 和 contenthash 啦
Tiktok practice ~ one click registration and login process of mobile phone number (verification code)
【科普】一文弄懂监督式学习、非监督式学习以及强化式学习
PAT甲级 - 1010 Radix(思维+二分)
office2016+visio2016
Former amd chip architect roast said that the cancellation of K12 processor project was because amd counseled!
[hdu] P2087 剪花布条
PAT甲级 - 1015 Reversible Primes (进制转换&素数判断)