2019暑假牛客多校训练-第八场-C-CDMA(递归、水题)
观察前3组可以推出递归规律,生成下一个类型时,每行copy自身与自身相反。
题目描述
Gromah and LZR have entered the third level. There is a blank grid of size m\times mm×m, and above the grid is a word "CDMA".
The inner product of two sequences s,t_{}s,t with the same length n_{}n equals to \sum_{i=1}^{n} s_it_i∑i=1nsiti.
So, the key to the next level is to construct a grid of size m\times mm×m, whose elements are all -1_{}−1 or 1_{}1, and for any two different rows, the inner product of them should be exactly 0_{}0.
输入描述:
Only one positive integer m_{}m in one line.
m \in \{2^k \; | \;k = 1, 2, \cdots, 10\}m∈{2k∣k=1,2,⋯,10}
输出描述:
Print m_{}m lines, each contains a sequence with length m_{}m, whose elements should be all -1_{}−1 or 1_{}1 satisfying that for any two different rows, the inner product of them equals 0_{}0.
You may print multiple blank spaces between two numbers or at the end of each line, and you may print any number of blank characters at the end of whole output file.
示例1
说明
The inner product of the two rows is 1\times(-1) + 1\times 1 = 01×(−1)+1×1=0.
代码如下
1 #include <iostream> 2 #include <math.h> 3 using namespace std; 4 char a[1050][1050]; 5 int main(){ 6 int n,m,aa(1); 7 a[0][0]='1'; 8 a[0][1]='1'; 9 a[1][0]='1'; 10 a[1][1]='0'; 11 cin>>n; 12 if(n==2){ 13 cout<<"1 1\n"; 14 cout<<"1 -1\n"; 15 return 0; 16 } 17 m=log2(n)-1; 18 while(m--){ 19 aa=aa*2; 20 for(int k=0, t=aa;k<aa;k++,t++){ 21 for(int i=0,j=aa;j<2*aa;j++,i++){ 22 if(a[k][i]=='1') { 23 a[k][j]='0'; 24 a[t][i]='1'; 25 a[t][j]='1'; 26 } 27 else { 28 a[k][j]='1'; 29 a[t][i]='0'; 30 a[t][j]='0'; 31 } 32 } 33 } 34 } 35 for(int i=0;i<n;i++){ 36 for(int j=0;j<n;j++){ 37 if(a[i][j]=='1') cout<<"1 "; 38 else cout<<"-1 "; 39 } 40 cout<<'\n'; 41 } 42 }
posted on 2019-08-11 12:06 Where_Free 阅读(245) 评论(0) 编辑 收藏 举报