美团笔试编程题交流
灯泡问题:AC
#include<iostream> #include<vector> using namespace std; int main() { long n; while(cin >> n) { int ai; bool flag; int cnt = 0; vector<int> lamp; for(int i =0; i <n; ++i){ cin >> ai; lamp.push_back(ai); } flag = lamp[0]; for(int i =1; i <n; ++i){ if(lamp[i] == flag){ continue; }else{ ++cnt; flag = lamp[i]; } } if((lamp[0] ==1) && (cnt & 1) || (lamp[0] ==0) && !(cnt & 1)) cout << "Bob" <<endl; else cout << "Alice" <<endl; } return 0; }
第二题:被7整除,没有ac,想不到思路,暴力+优化结果,70% 超时。
#include<iostream> #include<vector> using namespace std; long long combine2Num(long long n1, long long n2){ int bit = 0; long long t = n2; while(n2/10){ ++bit; n2 /= 10; n1 *= 10; } n1 *= 10; return n1 + t; } int main() { int n; while(cin >> n) { int ret = 0; int ai; int nn = 0; vector<long long> a; bool is7n[n]; for(int i =0; i <n; ++i){ cin >> ai; a.push_back(ai); } for(int i = 0; i < n;++i){ if(a[i] % 7 == 0){ is7n[i] = true; ++nn; } else is7n[i] = false; } ret = nn*(nn-1); for(int i = 0 ;i < n;++i){ if(is7n[i]){ continue; } for(int j = i+1; j < n; ++j){ if(is7n[j]){ continue; } if( (combine2Num(a[i],a[j]) % 7) == 0){ ++ret; } if( (combine2Num(a[j],a[i]) % 7) == 0){ ++ret; } } } cout <<ret<<endl; } return 0; } 求各位大佬的思路啊~~~#美团#