Magic Square (zjut1005) and anti-Magic Square (HDU 3927)

Source: Internet
Author: User

Zjut1005 magic

This question is to solve the fantasy party

Note: The key is to determine the location of the next digit. The rule is: 1. Each digit in the future can only be filled in the next column (as a) in the next row of its location ), if a already has a number, enter the next number on the current number (that is, the previous position in the same column as this number)

#include<stdio.h>
#include<string.h>
#define nmax 101
int num[nmax][nmax];
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int n, nn, i, j, k;
while (~scanf("%d", &n) && n) {
memset(num, 0, sizeof(num));
i = n - 1, j = n / 2, num[i][j] = 1;
for (k = 2, nn = n * n; k <= nn; k++) {
if (!num[(i + 1) % n][(j + 1) % n]) {
i = (i + 1) % n, j = (j + 1) % n;
} else {
i = (i - 1 + n) % n;
}
num[i][j] = k;
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%3d", num[i][j]);
}
printf("\n");
}
printf("\n");
}
return 0;
}

HDU 3927 math geek

/*
* hdu3927.c
*
* Created on: 2011-9-22
* Author: bjfuwangzhu
*/

#include<stdio.h>
#define nmax 201
int num[nmax][nmax];
void solve(int n) {
int i, j, k;
for (i = 0, k = 1; i < n; i++) {
for (j = 0; j < n - 1; j++) {
num[i][j] = k, k++;
}
}
for (i = 0; i < n; i++) {
num[i][n - 1] = k, k++;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int t, k, i, j, n;
while (~scanf("%d", &t)) {
for (k = 1; k <= t; k++) {
scanf("%d", &n);
solve(n);
printf("Case #%d:\n", k);
for (i = 0; i < n; i++) {
printf("%d", num[i][0]);
for (j = 1; j < n; j++) {
printf(" %d", num[i][j]);
}
printf("\n");
}
}
}
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.