4.19爱奇艺C++开发笔试题编程题代码
第一题 洗牌
思路就 是模拟洗牌 https://paste.ubuntu.com/p/xRxpjGWh4k/
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define mp make_pair vector<int> res,tleft,tright; map<int,int> vis; int main() { int a,n,t; for(int i=0;i<13;i++) { cin>>a; res.push_back(a); } cin>>n; while(n--) { cin>>t; tleft.clear(); tright.clear(); int j=0; for(int i=0;i<t;i++) { tleft.push_back(res[j++]); } for(int i=0;i<13-t;i++) { tright.push_back(res[j++]); } //for(int i=0;i<tleft.size();i++) cout<<tleft[i]<<" "; //cout<<endl; //for(int i=0;i<tright.size();i++) cout<<tright[i]<<" "; //cout<<endl; int cleft =tleft.size()-1; int cright=tright.size()-1; res.clear(); while(cleft!=-1 || cright !=-1) { //cout<<cleft<<" "<<cright<<endl; if(cleft!=-1) { //cout<<tleft[cleft]<<" "; res.push_back(tleft[cleft--]); } if(cright!=-1) { //cout<<tright[cright]<<" "; res.push_back(tright[cright--]); } //system("pause"); } reverse(res.begin(),res.end()); } for(int i=0;i<res.size();i++) { if(i==0) cout<<res[i]; else cout<<" "<<res[i]; } cout<<endl; return 0; }
第二题 三个数字 两个操作分别是 +1 +2 让三个数最后相等的最少次数
思路就是 贪心 , 三个数 a b c ,如果 c>b>=a 就 a+1 b+1 ,如果 c=b>a ,就a+2 https://paste.ubuntu.com/p/5XZvTbxMfq/
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define mp make_pair int a[3]; int main() { while(cin>>a[0]>>a[1]>>a[2]) { sort(a,a+3); int res=0; while( a[0]!=a[2]) { if(a[2]==a[1]) a[0]+=2; else { a[0]++; a[1]++; } sort(a,a+3); //cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl; system("pause"); res++; } cout<<res<<endl; } return 0; }
第三题 选3个糖 糖有TKS 甜苦酸 三种,不能KS不能在一起,还给你m个限制,限制了第a和第b个糖不能放一起 ,问有多少种选择,
正解不会 ,乱搞了一个, 分别 统计 T K S 的个数 记为 cnt_T ,cnt_K cnt_S 然后 如果cnt_T <=1 输出 0 否则 输出 min( cnt_T /2, cnt_K+cnt_s) https://paste.ubuntu.com/p/NXjSqPYmxy/
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define mp make_pair int vis[505][505]; char str[505]; int main() { int n,m; while(cin>>n>>m) { int cnt0=0,cnt1=0,cnt2=0; for(int i=0;i<n;i++) { cin>>str[i]; if(str[i]=='T') cnt0++; else if(str[i]=='K') cnt1++; else cnt2++; } memset(vis,0,sizeof(vis)); int a,b; for(int i=0;i<m;i++) { cin>>a>>b; vis[a][b]=vis[b][a] =1; } if(cnt0<=1) puts("0"); else { int res = cnt0/2; cout<<min(res,cnt1+cnt2)<<endl; } } return 0; }
#实习#