2018.8.13/华为在线开发与测试笔试
//华为在线笔试--大小写字母互换问题
100%
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
while((ch=getchar())!= '\n')
{
if(isupper(ch)) ch+=32;
else if(islower(ch)) ch-=32;
putchar(ch);
}
}
40%
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int main()
{
char str[256];
scanf("%s",str);
for(int i=0;i<strlen(str);i++)
{
if(str[i]>='a' && str[i]<='z')
str[i] -=32;
else if(str[i]>='A' && str[i]<='Z')
str[i] += 32;
cout<<str[i];
}
return 0;
}
20%
int main()
{
string str;
cin >> str;
for (auto c:str)
{
if (islower(c))
cout << toupper(c);
else if (isupper(c))
cout << tolower(c);
else cout << c;
}
return 0;
}
//华为在线笔试--背包问题
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
vector<string> split(string str, char tag){
vector<string> res;
string tmp = "";
for (auto c : str){
if (c != tag)tmp += c;
else{
res.push_back(tmp);
tmp = "";
}
}
if (tmp != "")res.push_back(tmp);
return res;
}
int main()
{
int n = 5, c;
vector<int> w(n+1, 0);
vector<int> v(n+1, 0);
string tmp;
cin >> tmp;
vector<string> s = split(tmp, ',');
for (int i = 1; i <= n; i++){
v[i] = stoi(s[i-1]);
}
cin >> tmp;
s = split(tmp, ',');
for (int i = 1; i <= n; i++){
w[i] = stoi(s[i-1]);
}
cin >> c;
vector<vector<int> > m(n+1,vector<int>(c+1,0));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= c; j++)
{
if (j >= w[i])
m[i][j] = max(m[i - 1][j], m[i - 1][j - w[i]] + v[i]);
else
m[i][j] = m[i - 1][j];
}
}
cout << m.back().back() << endl;
return 0;
}
//华为在线笔试--typedef问题
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<map>
using namespace std;
vector<string> split(string str){
vector<string> res;
string tmp = "";
for (auto c : str){
if (c != ' ' && c != ';')tmp += c;
else{
res.push_back(tmp);
tmp = "";
}
}
if (tmp != "")res.push_back(tmp);
return res;
}
int main()
{
string str,tofind;
getline(cin,str);
cin >> tofind;
vector<string> s = split(str);
map<string, string> m;
stack<string> ready;
for (auto i : s){
if (i == "typedef"){
string tmpkey, tmpvalue = "";
if (!ready.empty()){
tmpkey = ready.top();
ready.pop();
}
if (!ready.empty()){
tmpvalue = ready.top();
ready.pop();
}
while (!ready.empty()){
tmpvalue = ready.top() + " " + tmpvalue;
ready.pop();
}
if (!tmpkey.empty())m[tmpkey] = tmpvalue;
}
else ready.push(i);
}
string tmpkey, tmpvalue = "";
if (!ready.empty()){
tmpkey = ready.top();
ready.pop();
}
if (!ready.empty()){
tmpvalue = ready.top();
ready.pop();
}
while (!ready.empty()){
tmpvalue = ready.top() + " " + tmpvalue;
ready.pop();
}
if (!tmpkey.empty())m[tmpkey] = tmpvalue;
if (m.find(tofind) == m.end()){
cout << "none" << endl;
return 0;
}
else{
tofind = m[tofind];
}
bool turn = true;
while (turn)
{
auto it = m.begin();
while (it != m.end())
{
string::size_type n;
n = tofind.find((*it).first);
if (n == std::string::npos){
turn = false;
break;
}
else{
string t = tofind.substr(0, n) + (*it).second + tofind.substr(n + (*it).first.size());
tofind = t;
}
}
}
for (auto c : tofind){
if (c == '*')cout << " ";
cout << c;
}
cout << endl;
return 0;
}
#华为##测试工程师##笔试题目##题解#
100%
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch;
while((ch=getchar())!= '\n')
{
if(isupper(ch)) ch+=32;
else if(islower(ch)) ch-=32;
putchar(ch);
}
}
40%
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int main()
{
char str[256];
scanf("%s",str);
for(int i=0;i<strlen(str);i++)
{
if(str[i]>='a' && str[i]<='z')
str[i] -=32;
else if(str[i]>='A' && str[i]<='Z')
str[i] += 32;
cout<<str[i];
}
return 0;
}
20%
int main()
{
string str;
cin >> str;
for (auto c:str)
{
if (islower(c))
cout << toupper(c);
else if (isupper(c))
cout << tolower(c);
else cout << c;
}
return 0;
}
//华为在线笔试--背包问题
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
vector<string> split(string str, char tag){
vector<string> res;
string tmp = "";
for (auto c : str){
if (c != tag)tmp += c;
else{
res.push_back(tmp);
tmp = "";
}
}
if (tmp != "")res.push_back(tmp);
return res;
}
int main()
{
int n = 5, c;
vector<int> w(n+1, 0);
vector<int> v(n+1, 0);
string tmp;
cin >> tmp;
vector<string> s = split(tmp, ',');
for (int i = 1; i <= n; i++){
v[i] = stoi(s[i-1]);
}
cin >> tmp;
s = split(tmp, ',');
for (int i = 1; i <= n; i++){
w[i] = stoi(s[i-1]);
}
cin >> c;
vector<vector<int> > m(n+1,vector<int>(c+1,0));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= c; j++)
{
if (j >= w[i])
m[i][j] = max(m[i - 1][j], m[i - 1][j - w[i]] + v[i]);
else
m[i][j] = m[i - 1][j];
}
}
cout << m.back().back() << endl;
return 0;
}
//华为在线笔试--typedef问题
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<map>
using namespace std;
vector<string> split(string str){
vector<string> res;
string tmp = "";
for (auto c : str){
if (c != ' ' && c != ';')tmp += c;
else{
res.push_back(tmp);
tmp = "";
}
}
if (tmp != "")res.push_back(tmp);
return res;
}
int main()
{
string str,tofind;
getline(cin,str);
cin >> tofind;
vector<string> s = split(str);
map<string, string> m;
stack<string> ready;
for (auto i : s){
if (i == "typedef"){
string tmpkey, tmpvalue = "";
if (!ready.empty()){
tmpkey = ready.top();
ready.pop();
}
if (!ready.empty()){
tmpvalue = ready.top();
ready.pop();
}
while (!ready.empty()){
tmpvalue = ready.top() + " " + tmpvalue;
ready.pop();
}
if (!tmpkey.empty())m[tmpkey] = tmpvalue;
}
else ready.push(i);
}
string tmpkey, tmpvalue = "";
if (!ready.empty()){
tmpkey = ready.top();
ready.pop();
}
if (!ready.empty()){
tmpvalue = ready.top();
ready.pop();
}
while (!ready.empty()){
tmpvalue = ready.top() + " " + tmpvalue;
ready.pop();
}
if (!tmpkey.empty())m[tmpkey] = tmpvalue;
if (m.find(tofind) == m.end()){
cout << "none" << endl;
return 0;
}
else{
tofind = m[tofind];
}
bool turn = true;
while (turn)
{
auto it = m.begin();
while (it != m.end())
{
string::size_type n;
n = tofind.find((*it).first);
if (n == std::string::npos){
turn = false;
break;
}
else{
string t = tofind.substr(0, n) + (*it).second + tofind.substr(n + (*it).first.size());
tofind = t;
}
}
}
for (auto c : tofind){
if (c == '*')cout << " ";
cout << c;
}
cout << endl;
return 0;
}
#华为##测试工程师##笔试题目##题解#