9.8晚上网易互娱笔试(c++)
第一题:
#include <iostream>
#include <string>
#include <queue>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--) {
string str;
cin >> str;
queue<char> sss;
int len = str.length();
for (int i = 0; i < len;) {
sss.push(str[i++]);
while (str[i] == (str[i - 1] + 1)) {
sss.push(str[i++]);
}
if (sss.size() < 4) {
while (!sss.empty()) {
cout << sss.front();
sss.pop();
}
} else {
cout << sss.front()<<'-';
while (sss.size() > 1)
sss.pop();
cout << sss.front();
sss.pop();
}
}
cout << endl;
}
system("pause");
return 0;
} 第二题:
#include <iostream>
#include <string>
using namespace std;
int char_to_num(char x) {
if ((x >= '0') && (x <= '9'))
return x - '0';
else return x - 'A' + 10;
}
int main()
{
int n;
cin >> n;
while (n--) {
int x, y;
cin >> x >> y;
string str;
cin >> str;
int len = str.length();
int first, second;
for (int i = 1; i < len; i++) {
first = 0;
second = 0;
for (int j = 0; j < i; j++) {
first *= x;
first += char_to_num(str[j]);
}
for (int j = i; j < len; j++) {
second *= y;
second += char_to_num(str[j]);
}
if (first == second)
cout << first << endl;
}
}
system("pause");
return 0;
}
第三题:
#include <iostream>
#include <vector>
#include <string>
#include <set>
using namespace std;
int max(int a,int b){
return a>b?a:b;
};
int min(int a,int b){
return a<b?a:b;
};
int main(){
int T;
while(cin>>T){
vector<int>result_v;
while(T) {
int N,M,L,S,temp,result=0;
cin>>N>>M>>L>>S;
string temps;
vector<pair<int,int>> timu = vector<pair<int,int>>(N);
vector<vector<string>> shuxin = vector<vector<string>>(N);
for(int i =0;i<N;i++){
cin>>temp;
timu[i].first=temp;
cin>>temp;
timu[i].second=temp;
for(int j=0;j<temp;j++){
cin>>temps;
shuxin[i].push_back(temps);
}
}
set<string>settemp;
int sum=0,Min,Max;
result=0;
for(int i = 0; i <N*N; ++i){
bool flag=true; settemp.clear();
sum=0,Min=L,Max=0;
int tempi=i; int count=0;
for (int j =0;j<N;j++){
int index; index=tempi%2;
tempi/=2;
if(index) count++;
}
if(count!=M)
continue;
tempi=i;
for (int j =0;j<N;j++) {
int qianyigesize=settemp.size();
int houyigesize;
int index;
index=tempi%2;
tempi/=2;
if (index){
Max=max(Max,timu[j].first);
Min=min(Min,timu[j].first);
for (int k = 0; k < shuxin[j].size(); k++) {
settemp.insert(shuxin[j][k]);
}
houyigesize=settemp.size();
if(qianyigesize+shuxin[j].size()!=houyigesize){
flag= false; break;
}
sum+=timu[j].first;
if (sum>L) {
flag= false;
break;
}
}
}
if(flag){
if(Max-Min>=S) {
result++;
}
}
}
result_v.push_back(result);
T--;
}
for(int i:result_v) {
cout<<i<<endl;
}
}
return 0;
} (第三题credit to 评论二楼 @null~~· 感谢~~)
第四题:
#include <iostream>
using namespace std;
struct node {
int x;
int y;
}num[1000];
int main()
{
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
int cnt = 0;
for (int i = 0; i < n; i++) {
cin >> num[i].x >> num[i].y;
}
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
if (num[i].y == num[j].x) {
num[j].x = num[i].x;
cnt++;
break;
}
if (num[i].x == num[j].y) {
num[j].y = num[i].y;
cnt++;
break;
}
}
}
if (cnt < n - 1)cout << "no" << endl;
else cout << "yes" << endl;
}
system("pause");
return 0;
}
#网易互娱##笔试题目##题解#
查看17道真题和解析