题目大意:给定n个数据a1,a2,a3,a4……an,求出bi,使得∑ai*bi = 0
解题思路:
考虑一种情况:a1对应的b1 = - b2 ,a2 对应的b2 = a1,那么a1b1 + a2b2 = -a1b2 + a1b2 = 0
那么对n为偶数的情况考虑,只需要对每两个a进行如上操作即可
对n为奇数的情况,考虑讲三个a中的两个化为一个,再转化成n为偶数的情况即可
AC代码:
#include<bits/stdc++.h> using namespace std; int main(){ int n,T; scanf("%d",&T); while(T --){ scanf("%d",&n); if(n % 2 == 0){ int a,b; for(int i = 0;i < n;i += 2){ scanf("%d%d",&a,&b); printf("%d %d ",-b,a); } } else { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(a + b != 0){ printf("%d %d %d ",-c,-c,a + b); } else if(a + c != 0){ printf("%d %d %d ",-b,a + c,-b); } else{ printf("%d %d %d ",b + c,-a,-a); } for(int i = 3;i < n;i += 2){ scanf("%d%d",&a,&b); printf("%d %d ",-b,a); } } printf("\n"); } return 0; }