网易互娱 笔试题分享 3AC+一道40%(其余超时)
第一题
#include<set> #include<stdio.h> #include<iostream> #include<bits/stdc++.h> using namespace std; int trans(int n) //10转2 ,返回1的个数 { int result=0,temp=n,i=1,count=0; while(temp) { if(temp%2==1)count++; result+=i*(temp%2); temp=temp/2; i=i*10; } return count; } void tongJi(int N) { int a[102],results,temp; set<int> lable; for(int i=0;i<N;i++) { cin>>a[i]; temp=trans(a[i]); lable.insert(temp); } results=lable.size(); cout<<results<<endl; } int main() { int T,N; cin>>T;//输入样例个数 for(int i=0;i<T;i++) { cin>>N;//输入每个样例中的个数 tongJi(N); } return 0; }第二题
#include <iostream> #include <vector> #include <algorithm> #include <set> #include <math.h> #include <string> #include <queue> #include <stdlib.h> #include <stdio.h> using namespace std; int min(int a, int b, int c) { int r; if(a >= b) r = b; else r = a; if(r >= c) r = c; return r; } int main() { int T, m, t, m1, t1, m2, t2; cin >> T; while(T--) { int sum = 0; cin >> m >> t >> m1 >> t1 >> m2 >> t2; int x = t1, y = t2; int flag = 0; while(t!=0) { if(flag == 0) { int a = min(t1, t2, t); sum += a * (m1-m2); if(sum < 0) sum = 0; else if(sum > m) sum = m; t1 -= a; t2 -= a; t -= a; if(t1 == 0 && t2!=0) { flag = 2; t1 += x; } else if(t2 == 0 && t1 != 0) { flag = 1; t2 += y; } else if(t1==0&&t2==0) { flag = 3; t1 += x; t2 += y; } } else if(flag == 1) { int a = min(t1, t2, t); sum += a * (m1); if(sum < 0) sum = 0; else if(sum > m) sum = m; t1 -= a; t2 -= a; t -= a; if(t1 == 0 && t2!=0) { flag = 3; t1 += x; } else if(t2 == 0 && t1 != 0) { flag = 0; t2 += y; } else if(t1==0&&t2==0) { flag = 2; t1 += x; t2 += y; } } else if(flag == 2) { int a = min(t1, t2, t); sum += a * (-m2); if(sum < 0) sum = 0; else if(sum > m) sum = m; t1 -= a; t2 -= a; t -= a; if(t1 == 0 && t2!=0) { flag = 0; t1 += x; } else if(t2 == 0 && t1 != 0) { flag = 3; t2 += y; } else if(t1==0&&t2==0) { flag = 1; t1 += x; t2 += y; } } else if(flag == 3) { int a = min(t1, t2, t); t1 -= a; t2 -= a; t -= a; if(t1 == 0 && t2!=0) { flag = 1; t1 += x; } else if(t2 == 0 && t1 != 0) { flag = 2; t2 += y; } else if(t1==0&&t2==0) { flag = 0; t1 += x; t2 += y; } } } cout << sum << endl; } return 0; }
第三题
#include<bits/stdc++.h> using namespace std; int count(string str) { int left = 0, right = 0, change = 0, res = 1; for (int i = 0; i < str.length(); i++) { if (str[i] != 'N') { if (change < 2) { change++; right++; } else { while (left <= right && str[left] == 'N') left++; left++; right++; } } else right++; res = max(res, right - left ); } return res; } int main() { int T; cin >> T; while(T--) { string str; cin >> str; for(int i = 0; i < str.length(); ++i) { if(str[i] < 'A' || str[i] > 'Z') return 0; } cout << count(str) << endl; }return 0; }
第四题 只通过了40%,其余超时,求大神分享代码呀~~~
#include <iostream> #include <stdlib.h> #include <stdio.h> using namespace std; int a[200001]; void ansQun(int h,int n)//h是这次洪水的高度,n是发电站的个数 { int temp,flag=0,count=0; for(int i=0;i<n;i++) { if(h>=a[i]) { flag=0; } else //没被淹没的 { if(flag==0)//还没有记录,要计算之 { flag=1;count++; } } } cout<<count<<endl; } int main() { int n, q, h; cin >> n;//输入发电基站的个数 for(int i=0;i<n;i++) cin >> a[i];//每个电站的海拔 cin >> q;//洪水个数 for(int i=0;i<q;i++) { cin>>h; ansQun(h,n); } return 0; }