当前位置:网站首页>Niuke.com Huawei question bank (1~10)
Niuke.com Huawei question bank (1~10)
2022-06-10 01:32:00 【wrdoct】
Huawei question bank
1. The length of the last word in the string
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]){
char inPut[5000];
cin.get(inPut, 5000);
int strLen = 0;
while(inPut[strLen] != '\0'){
strLen++; // String length
}
int num = 0;int i = 0;
while(i < strLen){
if(inPut[i] != ' '){
i++;
num++;
}
if(inPut[i] == ' '){
i++;
num = 0;
}
}
cout << num <<endl;
}
2. Count the number of occurrences of a character
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]){
/*char inPut[1000]; cin.get(inPut, 1000); cin.ignore(); char inStr; cin>>inStr; int strLen = 0; while(inPut[strLen] != '\0'){ strLen++; } int num = 0; for(int j = 0; j < strLen; j++){ if(inStr >= '0' && inStr <= '9'){ if(inPut[j] == inStr) num++; } else{ if(inPut[j] == inStr || (inPut[j]^32) == inStr) num++; } }*/
string s;
getline(cin,s);
char inStr;
cin>>inStr;
int num = 0;
for(int j = 0; j < s.size(); j++){
if(inStr >= '0' && inStr <= '9'){
if(s[j] == inStr) num++;
}
else{
if(s[j] == inStr || (s[j]^32) == inStr) num++;
}
}
cout << num << endl;
return 0;
}
3. A clear random number
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
int main(){
int N = 0;
cin>>N;
priority_queue<int> pq;
for(int i = 0; i < N; i++){
int num = 0;
cin>>num;
pq.push(num);
}
stack<int> st;
st.push(pq.top());
pq.pop();
while(!pq.empty()){
if(st.top() != pq.top()){
st.push(pq.top());
}
pq.pop();
}
vector<int> res;
while(!st.empty()){
res.push_back(st.top());
st.pop();
}
for(int i = 0; i < res.size(); i++){
cout << res[i] << endl;
}
return 0;
}
4. String delimitation
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void process(vector<string>& res, string& s){
while(s.size() % 8 != 0){
s += "0";
}
for(int i = 0; i < s.size() - 7; i += 8){
string tmp = s.substr(i, 8);
//cout<<tmp<<endl;
res.push_back(tmp);
}
}
int main(){
string str = "";
vector<string> res;
while(cin>>str){
process(res, str);
}
for(int i = 0; i < res.size(); i++){
cout<<res[i]<<endl;
}
return 0;
}
5. Hexadecimal conversion
#include <unordered_map>
#include <algorithm>
#include <math.h>
using namespace std;
unordered_map<char, int> m = {
{
'0', 0},
{
'1', 1},
{
'2', 2},
{
'3', 3},
{
'4', 4},
{
'5', 5},
{
'6', 6},
{
'7', 7},
{
'8', 8},
{
'9', 9},
{
'A', 10},
{
'B', 11},
{
'C', 12},
{
'D', 13},
{
'E', 14},
{
'F', 15}
};
void process(string s, int& res){
reverse(s.begin(), s.end());
//cout<<s<<endl;
for(int i = 0; i < s.size(); i++){
res += m[s[i]] * pow(16, i);
//cout<<res<<endl;
}
}
int main(){
string str = "";
int res = 0;
while(cin>>str){
process(str, res);
}
cout<<to_string(res)<<endl;
return 0;
}
6. Prime factor
#include <iostream>
#include <vector>
using namespace std;
int main(){
long num = 0; //
cin >> num;
vector<int> res;
for(int i = 2; i * i <= num; ++i){
//2 Is the smallest prime factor You only need to verify half of it 2 Is it a qualitative factor
//cout<<num<<endl;
while(num % i == 0){
res.push_back(i);
num /= i;
}
}
// If m=1, be while The cycle is just decomposed by prime numbers , If it is greater than 1, Explain that it has not been completely decomposed ,m That's the last prime number
// meanwhile , This sentence can also deal with the special case where the input is a prime number
if(num >= 2) res.push_back(num);
for(int i = 0; i < res.size(); i++){
cout << res[i] << " ";
}
cout << endl;
return 0;
}
7. Take an approximation
#include <iostream>
using namespace std;
int main() {
double num;
while(cin >> num)
{
cout << static_cast<int>(num + 0.5) << endl; //
}
}
8. Consolidated statement records
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
/*struct cmp{ bool operator()(pair<int, int>& m, pair<int, int>& n) { return m.first < n.first; } };*/
int main(){
int num = 0;
cin >> num;
map<int, int> m; //map The interior itself is based on key Is stored in order of size
//vector<pair<int, int>> vec;
for(int i = 0; i < num; i++){
int key = 0, value = 0;
while(cin>>key>>value){
if(!m[key]){
m[key] = value;
}
else m[key] += value;// When it doesn't exist , Accumulate when present
//vec.push_back(make_pair(key, value));
}
}
//sort(vec.begin(), vec.end(), cmp());
//map The interior itself is based on key Is stored in order of size
for(map<int, int>::iterator/*auto*/ iter = m.begin(); iter != m.end(); iter++){
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}
9. Extract non duplicate integers
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_set>
using namespace std;
int main(){
int num = 0;
cin >> num;
string str = to_string(num);
reverse(str.begin(), str.end());
unordered_set<char> tmp;
int res = 0;
for(int i = 0; i < str.size(); i++){
if(tmp.count(str[i]) == 0){
res = res * 10 + (str[i] - '0');
}
tmp.insert(str[i]);
}
cout << res << endl;
return 0;
}
10. Character count
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main(){
string str;
cin>>str;
unordered_set<char> s;
for(char c : str){
s.insert(c);
}
cout << s.size() << endl;
return 0;
}
边栏推荐
- 电脑系统怎么修改图片格式
- 【LeetCode】128. 最长连续序列
- 比你想象中更强大的 reduce 以及对敲码的思考
- Curriculum Learning and Graph Neural Networks (or Graph Structure Learning)
- 【ACL 2022】Hallucinated but Factual! Inspecting the Factuality of Hallucinations
- How to implement complex SQL such as distributed database sub query and join?
- How the computer system modifies the picture format
- 騰訊Libco協程開源庫 源碼分析(一)---- 下載Libco 編譯安裝 嘗試運行示例代碼
- 【ACL 2022】Hallucinated but Factual! Inspecting the Factuality of Hallucinations
- Teaching Broad Reasoning Skills via Decomposition-Guided Contexts
猜你喜欢

【LeetCode】接雨水

【FPGA】day15-串口协议uart回环工程

You're not still using xshell, are you? This open source terminal tool is yyds!

Teaching Broad Reasoning Skills via Decomposition-Guided Contexts

Why do programmers run away after two years?

Thingsboard tutorial (19): overview of rule nodes

Source code analysis of Tencent libco collaboration open source library (I) -- download libco compilation and installation and try to run the sample code

为什么芯片设计也需要「匠人精神」?

Win11怎么直接退回进入桌面?

【ACL 2022】Hallucinated but Factual! Inspecting the Factuality of Hallucinations
随机推荐
Unity技术 - 2D项目经验
正则表达式不含某字符串
What are the good techniques for domestic spot silver: simple usage of common indicators
JVM records a CPU surge
Custom action logging annotation
[從零開始學習FPGA編程-16]:快速入門篇 - 操作步驟2-4- Verilog HDL語言描述語言基本語法(軟件程序員和硬件工程師都能看懂)
[proteus simulation] 51 single chip microcomputer +lcd1602+ external memory calculator
Win11右键怎么直接打开所有选项?
【Proteus仿真】51单片机+LCD1602+外置存储器计算器
d具体化函数参数
Locust:微服务性能测试利器
What if win11 returns win10 without a return option?
实验三 字符类型及其操作(新)
网络中的拥塞控制与数据传输
电脑系统怎么修改图片格式
牛客网——华为题库(1~10)
The gradient decline triggered a "heated debate" among AI bulls. Netizen: everyone's answer is worth seeing
Application of discrete time integration in Simulink simulation
Why do programmers run away after two years?
比你想象中更强大的 reduce 以及对敲码的思考