Input 包含多组测试数据,第一行一个正整数T,表示数据组数。 每组数据共两行。 第一行包含N+1个非负整数。其中第一个数为N,表示序列的长度;接下来N 个数,依次描述序列{ ai}中的每个数。第二行包含M+1个整数。其中第一个数为M,表示询问的个数;接下来M个数 xi,每个数对应题目描述中的一个询问。保证 1 <= N <= 100,1 <= M <= 100,ai <= 1024,|xi| <= 1024,数据组数 <= 100。
Output 对于每组数据输出M + 1行。前M行对应晨晨M个询问的回答,第M + 1行为空行
Sample Input 2 2 1 1 2 0 2 3 1 2 4 3 10 5 1
Sample Output 2 1 3 2 1
Source 2016年中国大学生程序设计竞赛(合肥)-重现赛(感谢安徽大学) #include<bits/stdc++.h> using namespace std; #define ll long long const int inf = 0x3f3f3f3f; const int mod = 1000000007; const int maxn = 10000 + 8; int t, n, m, a[100 + 8], x[100 + 8], sum[1000 + 8], res[1000 + 8][1000 + 8]; int main() { scanf("%d", &t); while(t--) { scanf("%d", &n); memset(sum, 0, sizeof(sum)); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum[i] = sum[i - 1] ^ a[i]; } for(int i = 1; i <= n; i++) for(int j = i; j <= n; j++) res[i][j] = sum[j] ^ sum[i - 1];///区间[i, j]的所有数值的异或值 scanf("%d", &m); for(int i = 0; i < m; i++) { int miao = 1e9, id = 0, x; scanf("%d", &x); for(int j = 1; j <= n; j++) for(int k = j; k <= n; k++) if(miao > abs(res[j][k] - x)) { miao = abs(res[j][k] - x); id = k - j + 1; } else if(miao == abs(res[j][k] - x)) id = max(id, k - j + 1); printf("%d\n", id); } printf("\n"); } return 0; }
转载于:https://www.cnblogs.com/RootVount/p/11581414.html
相关资源:JAVA上百实例源码以及开源项目