牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com)
A-游游的整数翻转_牛客周赛 Round 18 (nowcoder.com)
题目描述
游游拿到了一个正整数�x,她希望把这个整数的前�k位进行翻转。你能帮帮她吗?
简单的字符串操作
#include<bits/stdc++.h>
using namespace std;
int main()
{int k;string s;cin >> s >> k;int n = s.size();string t = s.substr(0,k);reverse(t.begin(),t.end());while(t.size() && t[0] == '0')t.erase(0,1);string ans = t + s.substr(k,n);cout << ans << endl;return 0;
}
Java版
import java.util.Scanner;
public class Main
{public static void main(String[] args) {Scanner sc = new Scanner(System.in);String s = sc.next();int k = sc.nextInt();int n = s.length();String t = s.substring(0, k);t = new StringBuilder(t).reverse().toString();while (t.length() > 0 && t.charAt(0) == '0')t = t.substring(1);String ans = t + s.substring(k, n);System.out.println(ans);}
}
B-游游的排列统计_牛客周赛 Round 18 (nowcoder.com)
游游想知道,有多少个长度为nnn的排列满足任意两个相邻元素之和都不是素数。你能帮帮她吗?
我们定义,长度为nnn的排列值一个长度为nnn的数组,其中1到nnn每个元素恰好出现了一次。
小数素数判断 + 全排列即可
#include<bits/stdc++.h>
using namespace std;
bool No_prime[20];
void Init()
{for(int i = 3; i < 20; i++){for(int j = 2;j < i;j++)if(!(i % j))No_prime[i] = true;}
}
int a[11];
int main()
{Init();int n;cin >> n;int ans = 0;for(int i = 1;i <= 10;i++)a[i] = i;do{bool is = true;for(int i = 1;i < n;i++){if(false == No_prime[a[i] + a[i + 1]]){is = false;break;}}ans += is;}while(next_permutation(a + 1,a + 1 + n));cout << ans << endl;return 0;
}
Java版
import java.util.*;public class Main
{static boolean[] No_prime = new boolean[20];static int[] a = new int[11];public static void init() {for (int i = 3; i < 20; i++) for (int j = 2; j < i; j++) if (i % j == 0) {No_prime[i] = true;break;}}public static void main(String[] args) {init();Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int ans = 0;for (int i = 1; i <= 10; i++)a[i] = i;do {boolean is = true;for (int i = 1; i < n; i++) if (!No_prime[a[i] + a[i + 1]]) {is = false;break;}ans += is ? 1 : 0;} while (next_permutation(a, 1, 1 + n));System.out.println(ans);}public static boolean next_permutation(int[] a, int l, int r) {int i = r - 1;while (i > l && a[i - 1] >= a[i])i--;if (i <= l)return false;int j = r - 1;while (a[j] <= a[i - 1])j--;int temp = a[i - 1];a[i - 1] = a[j];a[j] = temp;j = r - 1;while (i < j) {temp = a[i];a[i] = a[j];a[j] = temp;i++;j--;}return true;}
}
C-游游刷题_牛客周赛 Round 18 (nowcoder.com)
游游制定了一个刷题计划,她找到了nnn套试卷,每套试卷的题目数量为aia_iai。游游每天上午最多打开一套试卷,下午最多打开一套试卷,也可以选择不刷题而摸鱼。当游游打开一套试卷后,她就会把上面的题目全部刷完。但是游游有强迫症,她希望每天刷的题目总数均为kkk的倍数。请你计算游游最多能刷多少天的题?
思路类似力扣中的俩数之和,很好理解。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn];
int main()
{int ans = 0;int n, k;cin >> n >> k;map<int,int>M;for(int i = 1;i <= n;i++){int m;cin >> m;if(m % k == 0)ans++;else{int p = m % k;if(M[k - p] > 0){ans++;M[k - p]--;}elseM[m % k]++;}}cout << ans << endl;return 0;
}
Java版
import java.util.*;
public class Main
{public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int ans = 0;int n = scanner.nextInt();int k = scanner.nextInt();Map<Integer, Integer> M = new HashMap<>();for (int i = 1; i <= n; i++) {int m = scanner.nextInt();if (m % k == 0)ans++;else {int p = m % k;if (M.containsKey(k - p) && M.get(k - p) > 0) {ans++;M.put(k - p, M.get(k - p) - 1);} else M.put(m % k, M.getOrDefault(m % k, 0) + 1); }}System.out.println(ans);}
}
D-游游买商品_牛客周赛 Round 18 (nowcoder.com)
背包问题变形(太久没写,晚上看的时候细节容易写错)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
#define endl '\n'
#define int long long
int a[maxn], b[maxn];
int dp[maxn][maxn];//前i件商品价格为j的最大满足
int n, x;signed main()
{cin.tie(0) -> sync_with_stdio(false);int n, x; cin >> n >> x;for(int i = 1;i <= n; i++) cin >> a[i];for(int i = 1;i <= n; i++) cin >> b[i];for(int i = 1;i <= n; i++){for(int j = 1;j <= x; j++){if(j >= a[i])dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - a[i]] + b[i]);else dp[i][j] = dp[i - 1][j]; int t = (a[i] / 2 + a[i - 1]);if(i > 1 && j >= t)dp[i][j] = max(dp[i][j], dp[i - 2][j - t] + b[i] + b[i - 1]);}}cout << dp[n][x] << endl;return 0;
}
Java版
import java.util.*;public class Main
{public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int maxn = 1010;long [][] dp = new long [maxn][maxn];int n, x;n = scanner.nextInt();x = scanner.nextInt();int[] a = new int[maxn];int[] b = new int[maxn];for (int i = 1; i <= n; i++) a[i] = scanner.nextInt();for (int i = 1; i <= n; i++) b[i] = scanner.nextInt();for (int i = 1; i <= n; i++) {for (int j = 1; j <= x; j++){if (j >= a[i]) dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - a[i]] + b[i]);else dp[i][j] = dp[i - 1][j];int t = (a[i] / 2 + a[i - 1]);if (i > 1 && j >= t) dp[i][j] = Math.max(dp[i][j], dp[i - 2][j - t] + b[i] + b[i - 1]);}}System.out.println(dp[n][x]);}
}