华为2019秋招笔试软件题题解
一共三道编程题,第一题100分,第二题200分,第三题300分。时间为2h。
第一题:挑选便宜的厂商,厂商1:大于等于3件打3折,实付大于50免运费10元 。厂商2:满十元就减两元,实付大于99免运费6元,输入件数和单价,计算哪家厂商更便宜(四舍五入,精确到分)。
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
double price;
while(cin>>n>>price)
{
double s1, s2;
if (n >= 3)
s1 = (n)*price*0.7+10;
else
s1 = n*price+10;
s1 = s1 > 50 ? s1 - 10 : s1;
s2 = n*price - n*price / 10 * 2+6;
s2 = s2 > 99 ? s2 - 6 : s2;
s1 = (int)(s1 * 100 + 0.5) / 100.0;
s2 = (int)(s2 * 100 + 0.5) / 100.0;
cout << s1 <<"fd" <<s2 << endl;
if (s1 > s2)
cout << 2 << endl;
else if (s1 < s2)
cout << 1 << endl;
else if (s1 == s2)
cout << 0 << endl;
}
return 0;
}
第二题:给定一个包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度。(需要注意对于非法字符的处理,如"(a)",返回0,“(())a()”,返回4)
#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
while (getline(cin, str))
{ stack<int> s0;
int max = 0, last = -1;
for (int i = 0; i < str.size(); i++)
{
if (str[i] != '('&&str[i] != ')')
{
while (!s0.empty()) s0.pop();
}
else if (str[i] == '(')
s0.push(i);
else
{
if (s0.empty()) last = i;
else
{
s0.pop();
if (s0.empty())
max = max >= i - last ? max : i - last;
else max = max >= i - s0.top() ? max : i - s0.top();
}
}
}
cout << max << endl;
}
return 0;
}
第三题:输入DNA序列,返回最长的重复DNA序列片段(此题c++时间要求不超过5s,我直接遍历了)
#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
while (cin >> str)
{
int maxlen = 0, start;
int curlen = 0, p, q;
for (int i = 0; i < str.size(); i++)
{
for (int j = i + 1; j < str.size() - i; j++)
{
p = i; q = j;
curlen = 0;
while (str[p] == str[q] && q<str.size())
{
curlen++; p++; q++;
}
if (curlen>maxlen)
{
maxlen = curlen;
start = i;
}
}
}
for (int i = start; i < start + maxlen; i++)
cout << str[i];
cout << " " << maxlen << endl;
}
return 0;
}
#秋招##题解##华为#
CVTE公司福利 671人发布