网易3.25实习笔试

https://www.nowcoder.com/discuss/22696?type=0&order=0&pos=6&page=1

上面的是官方题解。

1. 奇怪的表达式

因为没有优先级,从左到右计算。后来看题解,发现运算的数字也是只有1位数。比较简单,有时间再练一下leetcode的basiccalculator 1, 2.

 1 /*
 2 ID: y1197771
 3 PROG: test
 4 LANG: C++
 5 */
 6 #include<bits/stdc++.h>
 7 #define pb push_back
 8 #define FOR(i, n) for (int i = 0; i < (int)n; ++i)
 9 #define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
10 typedef long long ll;
11 using namespace std;
12 typedef pair<int, int> pii;
13 const int maxn = 1e3 + 10;
14 void solve() {
15     string s;
16     cin >> s;
17     int res = 0;
18     int n = s.size();
19     int i = 0;
20     while(i < n && isdigit(s[i])) {
21         res = res * 10 + s[i] - '0';
22         i++;
23     }
24     while(i < n) {
25         char op = s[i];
26         i++;
27         int t = 0;
28         while(i < n && isdigit(s[i])) {
29             t = t * 10 + s[i] - '0'; i++;
30         }
31         if(op == '+') {
32                 res += t;
33         } else if(op == '-') {
34             res -= t;
35         } else {
36             res *= t;
37         }
38     }
39     cout << res << endl;
40 }
41 int main() {
42     freopen("test.in", "r", stdin);
43     //freopen("test.out", "w", stdout);
44     ios::sync_with_stdio(0);
45     cin.tie(0); cout.tie(0);
46     solve();
47     return 0;
48 }
View Code

2. 集合

我直接就用set搞了,刚开始以为是int,然后错了,改用double,就a了。也很水。

看标程,使用gcd和pair来做的,这样比较保险。

 1 /*
 2 ID: y1197771
 3 PROG: test
 4 LANG: C++
 5 */
 6 #include<bits/stdc++.h>
 7 #define pb push_back
 8 #define FOR(i, n) for (int i = 0; i < (int)n; ++i)
 9 #define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
10 typedef long long ll;
11 using namespace std;
12 typedef pair<int, int> pii;
13 const int maxn = 1e3 + 10;
14 void solve() {
15     int a, b, x, y;
16     cin >> a >> b >> x >> y;
17     set<double> se;
18     for (int i = a; i <= b; i++) {
19         for (int j = x; j <= y; j++) {
20 
21             se.insert(1.0 * i / j);
22         }
23     }
24     cout << se.size() << endl;
25 }
26 int main() {
27     freopen("test.in", "r", stdin);
28     //freopen("test.out", "w", stdout);
29     ios::sync_with_stdio(0);
30     cin.tie(0); cout.tie(0);
31     solve();
32     return 0;
33 }
View Code

3. 双核处理

题目说是1024的倍数,那就除以1024进行处理,然后要求2个核分配的任务差不多,题目就变成一堆数字分成2堆,使得最大值最小,最好的情况是2堆相等,贪心是不行的,无法找到好的

贪心方法,然后就是dp,测算数据范围为,忘了,反正可以开这么大的数组,然后01背包吧。

别忘了最后的结果要乘以1024.

 1 /*
 2 ID: y1197771
 3 PROG: test
 4 LANG: C++
 5 */
 6 #include<bits/stdc++.h>
 7 #define pb push_back
 8 #define FOR(i, n) for (int i = 0; i < (int)n; ++i)
 9 #define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
10 typedef long long ll;
11 using namespace std;
12 typedef pair<int, int> pii;
13 const int maxn = 1e3 + 10;
14 int n, a[70];
15 bool dp[210000];
16 void solve() {
17     cin >> n;
18     int s = 0;
19     for (int i = 0; i < n; i++) {
20         cin >> a[i];
21         a[i] /= 1024;
22         //cout << a[i] << endl;
23         s += a[i];
24     }
25     dp[0] = 1;
26     for (int i = 1; i <= n; i++) {
27         for (int j = s; j >= a[i - 1]; j--) {
28             dp[j] = dp[j] || dp[j - a[i - 1]];
29         }
30     }
31     int res = INT_MAX;
32     for (int i = 0; i <= s; i++) {
33         if(dp[i]) {
34             res = min(res, max(i, s - i ));
35         }
36     }
37     cout << res * 1024 << endl;
38 
39 }
40 int main() {
41     freopen("test.in", "r", stdin);
42     //freopen("test.out", "w", stdout);
43     ios::sync_with_stdio(0);
44     cin.tie(0); cout.tie(0);
45     solve();
46     return 0;
47 }
View Code

我的题目就是上面3道,有时间,练习一下其他职位的题目,好像有些题目挺难的。

原文地址:https://www.cnblogs.com/y119777/p/6654762.html