Codeforces Round #678 (Div. 2)补题

简介: Codeforces Round #678 (Div. 2)补题

B. Prime Square


传送门

2db526edcf35c4aac0e2c536fabd44e.png题目大意:输出一个方阵,使方阵每行每列的和都是素数,同时方阵的组成不能是素数

解法

  1. 我原来的思路是素数筛,然后发现太麻烦,方阵中允许重复数字出现,我无法用代码实现
  2. 正确思路:方阵行列最小为2,找到两个数字a,b,是的a+b=素数,吧这两个数字放进数组中,然后对这个数组全排列输出。对此可以建立一个方阵,行列相同

1

2

3

4

2

3

4

1

3

4

1

2

4

1

2

3

代码如下:

import java.util.*;
public class B1 {
  public static void main(String args[])throws java.lang.Exception
  {
    Scanner sc  = new Scanner(System.in);
    int t = sc.nextInt();
    while(t-->0) {
      int  n = sc.nextInt();
      int arr []= new int[n];
      arr[0]=1;arr[1]=1;
      int count=0;
      for(int i=0;i<n;i++) {
        int j=i;
        count=0;
        while(count<n) {
          System.out.print(arr[j]+" ");
          j++;
          j=j%n;//保证下标不越界
          count++;
        }
        System.out.println();
      }
    }
  }
}
相关文章
|
6月前
Codeforces Round #178 (Div. 2)
在n条电线上有不同数量的鸟, Shaass开了m枪,每一枪打的是第xi条电线上的第yi只鸟,然后被打中的这只鸟左边的飞到第i-1条电线上,右边的飞到i+1条电线上,没有落脚点的鸟会飞走。
27 0
|
8月前
|
人工智能 算法 BI
Codeforces Round 891 (Div. 3)
Codeforces Round 891 (Div. 3)
92 0
Codeforces Round 891 (Div. 3)
|
8月前
|
机器学习/深度学习 Go
codeforces round 885 (div. 2)
codeforces round 885 (div. 2)
62 0
|
9月前
|
人工智能
Codeforces Round #786 (Div. 3)(A-D)
Codeforces Round #786 (Div. 3)(A-D)
54 0
Codeforces Round #644 (Div. 3)(A~G)
Codeforces Round #644 (Div. 3)(A~G)
94 0
Equidistant Vertices-树型dp-Codeforces Round #734 (Div. 3)
Description A tree is an undirected connected graph without cycles. You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words,
119 0