当前位置:网站首页>Niuke.com - Huawei Question Bank (100~108)
Niuke.com - Huawei Question Bank (100~108)
2022-07-30 19:21:00 【wrdoct】
华为题库
100.等差数列
#include <bits/stdc++.h>
using namespace std;
#define a1 2
#define d 3
int main(){
int n = 0;
while(cin >> n){
int res = 0;
int an = a1 + (n - 1) * d;
res = (a1 + an) * n / 2;
cout << res << endl;
}
return 0;
}
101.输入整型数组和排序标识,对其元素按照升序或降序进行排序
#include <bits/stdc++.h>
using namespace std;
struct cmp_0{
bool operator()(const int& a, const int& b){
return a < b;
}
};
bool cmp_1(const int& a, const int& b){
return a > b;
}
int main(){
int n = 0;
while(cin >> n){
vector<int> nums(n,0);
for(int i = 0; i < n; i++){
int num = 0;
cin >> num;
nums[i] = num;
}
int m = 0;
cin >> m;
if(m == 0){
//升序
sort(nums.begin(), nums.end(), cmp_0());
for(int i = 0; i < n; i++){
cout << nums[i] << " ";
}
cout << endl;
}
else if(m == 1){
//降序
sort(nums.begin(), nums.end(), cmp_1);
for(int i = 0; i < n; i++){
cout << nums[i] << " ";
}
cout << endl;
}
}
}
102.字符统计
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(const pair<char, int> & a, const pair<char, int> & b) {
if(a.second == b.second){
return a.first < b.first;
}
return a.second > b.second;
}
struct cmp1{
bool operator()(const pair<char, int> & a, const pair<char, int> & b){
if(a.second == b.second){
return a.first < b.first;
}
return a.second > b.second;
}
};
int main(){
string s = "";
while(getline(cin, s)){
map<char, int> m;
for(char ch : s){
m[ch]++;
}
vector<pair<char, int>> v(m.begin(), m.end()); //
//sort(v.begin(), v.end(), cmp;
//stable_sort(v.begin(), v.end(), cmp1());
//因为map已经依照键排序了,所以用稳定排序当次数一样时,会保持原来的先后关系,即ASCII码的升序
stable_sort(v.begin(), v.end(), [](const pair<char,int>& a,const pair<char,int>& b){
//用Lambda来写配合算法使用的小函数是非常方便的
return a.second>b.second;
});
for(auto iter : v){
cout << iter.first;
}
cout << endl;
}
return 0;
}
103.Redraiment的走法
#include <bits/stdc++.h>
using namespace std;
int main(){
int n = 0;
while(cin >> n){
vector<int> nums(n, 0);
for(int i = 0; i < n; i++){
int num = 0;
cin >> num;
nums[i] = num;
}
vector<int> dp(n, 1); //dp[i]表示以nums[i]is the length of the ending strictly ascending subsequence(初始化为1)
int res = 1;
for(int i = 0; i < nums.size(); i++){
for(int j = 0; j < i; j++){
//遍历前面的
if(nums[j] < nums[i]){
//ensure strict ascent
dp[i] = max(dp[i], dp[j] + 1); //
}
}
res = max(res, dp[i]); //维护最大值
}
cout << res << endl;
}
return 0;
}
105.记负均正II
#include<bits/stdc++.h>
using namespace std;
int main(){
int num = 0;
double sum = 0, ave = 0;
int posNum = 0;
int negNum = 0;
while(cin >> num){
if(num < 0){
negNum++;
}
else{
posNum++;
sum += num;
}
if(posNum != 0) ave = sum / posNum;
}
cout << negNum << endl;
printf("%.1lf\n", ave);
}
106.字符逆序
#include <bits/stdc++.h>
using namespace std;
void process(string str, string& res){
reverse(str.begin(), str.end());
res = str;
}
int main(){
string str = "";
while(getline(cin, str)){
string res = "";
process(str, res);
cout << res << endl;
}
return 0;
}
107.求解立方根
#include <bits/stdc++.h>
using namespace std;
double cal(double x){
//正负数都有
double l = min(-1.0, x);
double r = max(1.0, x);
double y;
while(abs(r - l) > 0.01){
//立方根的精度值
y = (l + r) / 2;
if(y*y*y > x){
//
r = y;
}
else{
l = y;
}
}
return y;
}
int main(){
double num = 0.0;
while(cin >> num){
//cout << setprecision(1) << fixed << cal(num) << endl; //控制小数位输出
printf("%.1lf", cal(num));
}
return 0;
}
108.求最小公倍数
#include<bits/stdc++.h>
using namespace std;
int main(){
int A = 0, B = 0;
while(cin >> A >> B){
int tmp = A * B;
for(int i = min(A, B); i <= tmp; i++){
if(i % A == 0 && i % B == 0){
cout << i << endl;
break;
}
}
}
}
边栏推荐
- Swiper rotates pictures and plays background music
- 云数据库和本地数据库有什么区别?
- Delay queue optimization (2)
- 牛客网——华为题库(100~108)
- Correct pose of Vulkan open feature
- Talking about Contrastive Learning (Contrastive Learning) the first bullet
- The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
- 试写C语言三子棋
- 跨进程启动后台服务
- MindSpore:mindspore有没有类似tf.GradientTape()用来求解梯度的?
猜你喜欢

牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)

Swiper rotates pictures and plays background music

MindSpore:ImageFolderDataset数据读取问题

OneFlow source code analysis: Op, Kernel and interpreter

SimpleOSS第三方库libcurl与引擎libcurl错误解决方法

延时队列优化 (2)

Golang logging library zerolog use record

Recommendation | People who are kind to you, don't repay them by inviting them to eat

VS Code connects to SQL Server

Delay queue optimization (2)
随机推荐
Object和Map的区别
MindSpore:【Resolve node failed】解析节点失败的问题
The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
WEBSOCKETPP使用简介+demo
What is the value of biomedical papers? How to translate the papers into Chinese and English?
深入浅出边缘云 | 3. 资源配置
Chapter 14 Type Information
MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
Range.CopyFromRecordset 方法 (Excel)
vxe-table实现复选框鼠标拖动选中
【剑指 Offer】剑指 Offer 22. 链表中倒数第k个节点
MindSpore:【resnet_thor模型】尝试运行resnet_thor时报Could not convert to
【MindSpore】多卡训练保存权重问题
牛客刷题系列之进阶版(组队竞赛,排序子序列,倒置字符串, 删除公共字符,修理牧场)
Win11如何更改默认下载路径?Win11更改默认下载路径的方法
中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
部分分类网络性能对比
7.30模拟赛总结
阿里云武林头条活动分享
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)