shopee笔试总结2.29
1.第一题回文字符串,没什么好说的
2.输出一个数组右边比他小的个数,比如
输入: [5, 4, 3, 2]
输出:[3, 2, 1, 0]
思路:字符串的题,好像没有限制。直接暴力匹配,注意:23要当做一个数字,还有输出格式要根据题目给的样例来输出。
#include <bits/stdc++.h> using namespace std; int main(){ vector<int> v; vector<int> ans; string str; getline(cin,str); for (int i = 1; i <str.size()-1 ; ++i) { if(str[i]==',' || str[i]==' ') continue; if(str[i] >= '0' && str[i] <= '9') { int start = i; while (str[i + 1] >= '0' && str[i + 1] <= '9') { i++; } int end = i; int sum = 0,u=0; for (int j = end; j >= start; --j) { int temp = (str[j]-'0')*pow(10,u); sum = sum +temp; u++; } v.push_back(sum); } } for (int j = 0; j < v.size(); ++j) { int count = 0; if(j==v.size()-1) { ans.push_back(count); break; } for (int i = j+1; i < v.size(); ++i) { if(v[i]<v[j]) count++; } ans.push_back(count); } cout<<'['; for (int k = 0; k < ans.size(); ++k) { if(k==ans.size()-1){ cout<<ans[k]; break; } cout<<ans[k]<<", "; } cout<<']'<<endl; return 0; }
3.和第二题我觉得是一样的题,换个输出格式就行,还有这次是匹配字符串,不是数字。
#include <bits/stdc++.h> using namespace std; int main(){ vector<string> v; vector<int> ans; string str; getline(cin,str); for (int i = 1; i <str.size()-1 ; ++i) { if (str[i] == '"' || str[i] == ' ' || str[i] == ',' || str[i] == '[' || str[i] == ']') continue; else{ int start = i; while (str[i] != '"' && str[i] != ' ' && str[i] != ',' && str[i] != '[' && str[i] != ']') { i++; } int end = i; v.push_back(str.substr(start,end-start)); } } cout<<'"'; for (int j = 0; j < v.size(); ++j) { if(j==v.size()-1){ cout<<v[j]; break; } cout<<v[j]<<"\\n"; } cout<<'"'<<endl; return 0; }
总结:没想到shopee全考字符串,比昨天的心动网络要简单很多。。。
#Shopee##笔试题目#