2017Nowcoder Girl初赛重现赛
https://ac.nowcoder.com/acm/contest/315#question
A.平方数
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; int n; int ans = 0; int main() { scanf("%d", &n); for(int i = n; i >= 1; i --) { if((int)sqrt(i) == sqrt(i)) { ans = i; break; } } printf("%d\n", ans); return 0; }
B.勇气获得机
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; int n; vector<char> ans; int main() { scanf("%d", &n); while(n) { if(n % 2 == 0) { ans.push_back('G'); n = (n - 2) / 2; } else { ans.push_back('N'); n = (n - 1) / 2; } } for(int i = ans.size() - 1; i >= 0; i --) printf("%c", ans[i]); printf("\n"); return 0; }
C.排列
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int n; int a[maxn]; int main() { scanf("%d", &n); for(int i = 1; i <= n; i ++) scanf("%d", &a[i]); int cnt = 0; for(int i = 1; i < n; i ++) if(a[i] == i) { cnt ++; swap(a[i], a[i + 1]); } if(a[n] == n) { swap(a[n], a[n - 1]); cnt ++; } printf("%d\n", cnt); return 0; }
D.打车
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; int n, s; int a[15]; int main() { scanf("%d%d", &n, &s); for(int i = 1; i <= n; i ++) scanf("%d", &a[i]); sort(a + 1, a + 1 + n); int sum = 0, i = 0, ans = 0;; while(sum < s && i <= n) { i ++; sum += a[i]; } //printf("%d\n", i); int cnt = 0; for(int j = i; j >= 1; j --) { if(sum - a[j] >= s) { cnt ++; sum -= a[j]; } else continue; } //printf("%d\n", cnt); printf("%d\n", i - cnt); return 0; }
F.美丽的项链
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n, m; ll dp[25][220]; ll l[22], r[22]; int main() { scanf("%d%d", &n, &m); memset(dp, 0, sizeof(dp)); for(int i = 1; i <= n; i ++) scanf("%d%d", &l[i], &r[i]); dp[0][0] = 1; for(int i = 1; i <= n; i ++) { for(int j = 0; j <= m; j ++) { for(int k = l[i]; k <= r[i]; k ++) dp[i][j + k] += dp[i - 1][j]; } } printf("%lld\n", dp[n][m]); return 0; }