帮我把这个代码翻译成c++import java.util.Scanner; class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } int m = in.nextInt(); int[] l = new int[m]; int[] r = new int[m]; char[] op = new char[m]; int[] x = new int[m]; for (int i = 0; i < m; i++) { l[i] = in.nextInt(); } for (int i = 0; i < m; i++) { r[i] = in.nextInt(); } String s = in.next(); for (int i = 0; i < m; i++) { op[i] = s.charAt(i); } for (int i = 0; i < m; i++) { x[i] = in.nextInt(); } for (int i = 0; i < m; i++) { if (op[i] == '|') { for (int j = l[i] - 1; j < r[i]; j++) { a[j] |= x[i]; } } else if (op[i] == '&') { for (int j = l[i] - 1; j < r[i]; j++) { a[j] &= x[i]; } } else { for (int j = l[i] - 1; j < r[i]; j++) { a[j] = x[i]; } } } for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } } }
时间: 2023-05-31 22:01:56 浏览: 96
Einleser:java.util.Scanner 的示例
#include <stdio.h>
int main(void) {
int n, m;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
scanf("%d", &m);
int l[m], r[m], x[m];
char op[m];
for (int i = 0; i < m; i++) {
scanf("%d", &l[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d", &r[i]);
}
scanf("%s", op);
for (int i = 0; i < m; i++) {
scanf("%d", &x[i]);
}
for (int i = 0; i < m; i++) {
if (op[i] == '|') {
for (int j = l[i] - 1; j < r[i]; j++) {
a[j] |= x[i];
}
} else if (op[i] == '&') {
for (int j = l[i] - 1; j < r[i]; j++) {
a[j] &= x[i];
}
} else {
for (int j = l[i] - 1; j < r[i]; j++) {
a[j] ^= x[i];
}
}
}
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}
阅读全文