网易2018校招在线笔试题解
被3整除 打表找规律,发现n%3==1不可,n为第几个这样的数
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <bits/stdc++.h> using namespace std ;int main () { int l,r,ans=0 ; cin >>l>>r; for (int i=l;i<=r;i++) { if (i%3 !=1 ) ans++; } cout <<ans<<endl ; }
安置路灯 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <bits/stdc++.h> using namespace std ;int main () { int n,len; char ch; cin >>n; while (n--) { int ans=0 ; cin >>len; for (int i=0 ;i<len;i++) { cin >>ch; if (ch=='.' ) { ans++; if (++i<len) cin >>ch; if (++i<len) cin >>ch; } } cout <<ans<<endl ; } }
迷路的牛牛 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <bits/stdc++.h> using namespace std ;int main () { char ch; int n,init=0 ; cin >>n; for (int i=0 ; i<n; i++) { cin >>ch; if (ch=='L' ) init--; else if (ch=='R' ) init++; } init%=4 ; if (init<0 ) init+=4 ; if (init==0 ) cout <<"N" <<endl ; else if (init==1 ) cout <<"E" <<endl ; else if (init==2 ) cout <<"S" <<endl ; else cout <<"W" <<endl ; return 0 ; }
数对 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> #define ll long long using namespace std ;int main () { ll n,k,ans=0 ; cin >>n>>k; if (k==0 ) ans=n*n; else for (ll i=k+1 ;i<=n;i++) ans+=(n/i)*(i-k)+max(0L L,n%i+1 -k); cout <<ans<<endl ; return 0 ; }
牛牛找工作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #include <cstdio> #include <vector> #include <algorithm> #include <unordered_map> using namespace std ;const int maxn = 100000 +1 ;int n, m;unordered_map <int , int > lev_pay;vector <int > jobs;vector <int > pays;void read_in () { scanf ("%d%d" , &n, &m); int lev, pay; for (int i = 0 ; i < n; i++) { scanf ("%d%d" , &lev, &pay); if (!lev_pay.count(lev)) { lev_pay[lev] = pay; jobs.push_back(lev); } else { if (lev_pay[lev] < pay) lev_pay[lev] = pay; } } sort(jobs.begin(), jobs.end()); pays.push_back(lev_pay[jobs[0 ]]); for (int i = 1 ; i < jobs.size(); i++) { lev_pay[jobs[i]] > pays[i-1 ] ? pays.push_back(lev_pay[jobs[i]]) : pays.push_back(pays[i-1 ]); } } void solve () { vector <int > res; int tmp; for (int i = 0 ; i < m; i++) { scanf ("%d" , &tmp); auto it = upper_bound(jobs.begin(), jobs.end(), tmp); if (it == jobs.begin()) { res.push_back(0 ); } else { it --; int loc = int (it - jobs.begin()); res.push_back(pays[loc]); } } for (auto & i : res) printf ("%d\n" , i); } int main () { read_in(); solve(); return 0 ; }
牛牛的闹钟 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <iostream> #include <vector> #include <algorithm> using namespace std ;int ti[105 ];int main () { int n,uptime,uphour,upminit; cin >>n; for (int i=0 ;i<n;i++) { cin >>uphour>>upminit; ti[i]=uphour*60 +upminit; } sort(ti,ti+n); cin >>uptime>>uphour>>upminit; int up=uphour*60 +upminit; for (int i=0 ;i<n;i++) { if (ti[i]+uptime>up) { cout <<ti[i-1 ]/60 <<" " <<ti[i-1 ]%60 <<endl ; break ; } } return 0 ; }
牛牛的背包问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include <cstdio> #include <vector> #include <unordered_map> using namespace std ;int n, w;vector <int > bag;unordered_map <string , int > dp;void read_in () { scanf ("%d%d" , &n, &w); int tmp; for (int i = 0 ; i < n; i++) { scanf ("%d" , &tmp); bag.push_back(tmp); } } int dfs (int num, int remain) { if (num == n) return 1 ; string key = to_string(remain) + '0' + to_string(num); if (dp.count(key)) return dp[key]; int ans = 0 ; if (remain > bag[num]) ans += dfs(num+1 , remain-bag[num]); ans += dfs(num+1 , remain); return dp[key] = ans; } int main () { read_in(); printf ("%d\n" , dfs(0 , w)); return 0 ; }