华为上机题汇总(十七)
注:编译环境为Visual Studio 2012,答案仅供参考。
目录
- 华为上机题汇总十七
- 目录
- 第八十一题
- 第八十二题
- 第八十三题
- 第八十四题
- 第八十五题
第八十一题
81.对一个二维的整数数组,对指定列进行升序排序,返回排列后的数组
例如:
3
2
2 6
3 3
4 1
1
2 1
3 3
4 6
#include <iostream>
using namespace std;
void mySort(int **a, int cols, int rows, int row){
for (int i = 0; i < cols - 1; i++)
{
for (int j = i + 1; j < cols; j++)
{
if (a[i][row] > a[j][row])
{
int tmp = a[i][row];
a[i][row] = a[j][row];
a[j][row] = tmp;
}
}
}
}
int main()
{
int cols, rows;
cin >> cols >> rows;
int **a = new int *[cols];
for (int i = 0; i < cols; i++)
{
a[i] = new int [rows];
}
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j ++)
{
cin >> a[i][j];
}
}
int row;
cin >> row;
mySort(a,cols,rows,row);
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j ++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < cols; i++)
{
delete []a[i];
}
delete []a;
}
第八十二题
82.最大连续子序列乘积
给定一个整数序列(可能有正数,0和负数),求它的一个最大连续子序列乘积。比如给定数组a={3, -4, -5, 6, -2},则最大连续子序列乘积为360,即3*(-4)*(-5)*6=360。
#include <iostream>
using namespace std;
int max(int a, int b, int c){
int tmp = a < b ? b : a;
return tmp < c ? c : tmp;
}
int min(int a, int b, int c){
int tmp = a > b ? b : a;
return tmp > c ? c : tmp;
}
int maxMul(int *a, int n){
int *Max = new int [n];
int *Min = new int [n];
int maxValue = a[0];
Max[0] = a[0];
Min[0] = a[0];
for (int i = 1; i < n; i++)
{
Max[i] = max(Max[i-1]*a[i],Min[i-1]*a[i],a[i]);
Min[i] = min(Max[i-1]*a[i],Min[i-1]*a[i],a[i]);
if (maxValue < Max[i])
{
maxValue = Max[i];
}
}
delete []Max;
delete []Min;
return maxValue;
}
int main()
{
int a[] = {3, -4, -5, 6, -2}, n = 5;
cout << maxMul(a,n) << endl;
}
第八十三题
83.对整形数据组按照和指定整数的差值大小进行排序,
按照差值升序排列返回。
输入:
num = {1,2,3,4,5,10,17,18,19} value = 5
返回:
{5,4,3,2,1,10,17,18,19}
#include <iostream>
using namespace std;
bool isBigger(int a, int b, int index){
int tmp1 = a > index ? a - index : index - a;
int tmp2 = b > index ? b - index : index - b;
return tmp1 > tmp2;
}
void mySort(int *a, int n, int index){
for (int i = 0; i < n-1; i++)
{
for (int j = i+1; j < n; j++)
{
if (isBigger(a[i],a[j],index))
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
}
int main()
{
int a[] = {1,2,3,4,5,10,17,18,19}, n = 9;
int index = 5;
mySort(a,n,index);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
第八十四题
84.取出整型数据中出现次数最多的元素,并按照升序排列返回。
输入:
num = {1,1,3,4,4,4,9,9,9,10} len = 10
返回:
{4,9}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printMost(vector<int> &input, vector<int> &output){
int max = 0;
sort(input.begin(),input.end());
auto begin = input.begin();
while (begin != input.end())
{
auto ahead = begin + 1;
while (ahead != input.end() && *ahead == *begin)
{
ahead++;
}
int i = ahead - begin;
if (i > max)
{
max = i;
if (!output.empty())
{
output.clear();
}
output.push_back(*begin);
}
else if (i == max)
{
output.push_back(*begin);
}
begin = ahead;
}
for (unsigned i = 0; i < output.size(); i++)
{
cout << output[i] << " ";
}
cout << endl;
}
int main()
{
int a[] = {1,1,3,4,4,4,9,9,9,10}, len = 10;
vector<int> input(a,a+len), output;
printMost(input,output);
}
第八十五题
85.小球落地
假设一个球从任意高度自由落下,每次落地后反跳回原高度的一半; 再落下, 求它在第5次落地时,共经历多少米?第5次反弹多高?
输入起始高度,int型
分别输出第5次落地时,共经过多少米第5次反弹多高
【去掉小数末尾无效的0】
样例输入:1
#include <iostream>
using namespace std;
void compute(int n, double &sum, double &fifthHeight){
double height = (double)n;
for (int i = 0; i < 5; i++)
{
sum += height + height / 2;
height /= 2;
}
fifthHeight = height;
sum -= fifthHeight;
}
int main()
{
int n;
cin >> n;
double sum = 0, fifthHeight;
compute(n,sum,fifthHeight);
cout << sum << " " << fifthHeight << endl;
}
时间: 2024-10-11 22:28:38