2015年4月17日 星期五

HOJ 401 - 完美小矩陣




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
 
using namespace std;
 
int sqrroot(int n){
    if(n < 0) return -1;
    int sqr1 = max(0, (int)(sqrt(n) - 2));
    for(int lx = sqr1;lx*lx <= n;lx++)
        if(lx*lx == n)
            return lx;
    return -1;
}
 
int main(){
    
    int n, k;
    while(scanf("%d %d", &n, &k) != EOF){
        if(n%2 == 0){
            printf("22222\n");
            for(int lx = 0;lx < n;lx++){
                for(int ly = 0;ly < n;ly++){
                    if(ly == lx-1 and ly%2 == 0) printf("1 ");
                    else if(ly == lx+1 and ly%2 == 1) printf("%d ", k);
                    else printf("0 ");
                }
                puts("");
            }
            continue;
        }
        int res = sqrroot(k);
        if(res == -1){
            puts("11111");
            continue;
        }
        puts("22222");
        for(int lx = 0;lx < n;lx++){
            for(int ly = 0;ly < n;ly++)
                printf("%d ", (lx==ly)*res);
            puts("");
        }
    }
    return 0;
}

沒有留言:

張貼留言