网易笔试编程(附ak代码)
1.十进制的和
#include <stdio.h> #include <stack> using namespace std; int main() { int n, num; scanf("%d", &n); while (n--) { scanf("%d", &num); stack<int> s; while (num > 9) { s.push(9); num -= 9; } s.push(num); while (!s.empty()) { printf("%d", s.top()); s.pop(); } printf("\n"); } return 0; }
2.ABpq(long long前90%)
#include <stdio.h> int main() { int n; scanf("%d", &n); while (n--) { int ans = 1; long long int a, b, p, q; scanf("%lld%lld%lld%lld", &a, &b, &p, &q); while (b - a > p) { p *= q; ans++; } printf("%d\n",ans); } return 0; }
3.积木堆(long long 前30%)
#include <stdio.h> int main() { int t; scanf("%d", &t); while (t--) { int n; long long int sum, now; scanf("%d%lld", &n,&sum); for (int i = 0; i < n; i++) { scanf("%lld", &now); if (sum >= 0) { if (now + sum >= i) { sum += now - i; } else { sum = -1; } } } if (sum < 0) { printf("NO\n"); } else { printf("YES\n"); } } return 0; }
4.跳木棍
判断vis[j]之前列了个表
- 若i到j不需要超能力
vis[i] \ vis[j] | -1 | 0 | 1 | 2 |
-1 | -1 | -1 | 1 | 2 |
1 | 1 | 1 | 1 | 1 |
2 | 2 | 2 | 1 | 2 |
- 若需要用到超能力
vis[i] \ vis[j] | -1 | 0 | 1 | 2 |
-1 | -1 | -1 | 1 | 2 |
1 | 2 | 2 | 1 | 2 |
2 | -1 | -1 | 1 | 2 |
#include <stdio.h> int main() { int t; scanf("%d", &t); while (t--) { int n, k; int h[1005], vis[1005];//vis为0表示未判断,为1表示可以跳到,为2表示超能力跳到,为-1表示不能跳到 scanf("%d%d", &n,&k); for (int i = 0; i < n; i++) { scanf("%d", &h[i]); vis[i] = 0; } vis[0] = 1; for (int i = 0; i < n; i++) { for (int j = i + 1; j <= i + k && j < n; j++) { if (vis[j] == 1) continue; if (h[j] <= h[i]) { if (vis[j] < 1 || vis[i] == 1) { vis[j] = vis[i]; } else { vis[j] = 2; } } else { if (vis[j] > 1 || vis[i] == 1) { vis[j] = 2; } else { vis[i] = -1; } } } } if (vis[n - 1] > 0) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }