import java.util.*;
public class magic_square {
public static void main(String[] args) {
boolean number = false;
int x = 0;
do {
try {
System.out.print("Please input a odd number and number >= 3: ");
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
}
catch (Exception e) {
}
if (x >= 3 && x % 2 != 0) {
break;
}
} while (!number);
int[][] arsq = new int[x][x];
arsq[0][x/2] = 1;
int sum = x * x;
int count = 2;
int a = x-1;
int b = x/2 + 1;
arsq[a][b] = count;
int a_ne = a - 1;
int b_ne = b + 1;
do {
count++;
if (a_ne < 0) {
if (b_ne > (x-1)) {
a_ne += 2;
b_ne -= 1;
}
else {
a_ne = x-1;
}
}
if (b_ne > (x-1)) {
b_ne = 0;
}
if (arsq[a_ne][b_ne] != 0) {
a_ne += 2;
b_ne -= 1;
arsq[a_ne][b_ne] = count;
}
else {
arsq[a_ne][b_ne] = count;
}
a_ne -= 1;
b_ne += 1;
} while (count < sum);
if (sum > 10) {
for (int i = 0; i <= (x-1); i++) {
for (int j = 0; j <= (x-1); j++) {
System.out.printf("%03d ", arsq[i][j]);
}
System.out.println();
}
}
else {
for (int i = 0; i <= (x-1); i++) {
for (int j = 0; j <= (x-1); j++) {
System.out.print(arsq[i][j] + " ");
}
System.out.println();
}
}
}
}
2017年9月25日 星期一
JAVA-魔方陣
匯入資料,抓文章
##設定自己jre位置
Sys.setenv(JAVA_HOME='C:/Program Files/Java/jdk1.8.0_121/jre')
library(rJava)
library(xlsx)
sd_content <- read.xlsx("sd_content.xlsx",header = TRUE,sheetIndex=1,encoding = "UTF-8")
class(sd_content$ar_ID)
library(openxlsx) ##檔案過大時,使用openxlsx
sd_ofile <- read.xlsx("sd_of_new_1.xlsx",sheet = 1)
ar_id_df <- read.xlsx("ar_id_df.xlsx",sheet = 1)
##check第一列第一個欄位,匯入時的多編碼
nchar(sd_content$ar_ID[1])
sd_content$ar_ID[1] <- c("1928")
nchar(sd_content$ar_ID[1])
## weblog與content結合,輸出有被點擊的文章
count <- nrow(ar_id_df)
for(i in 1:count){
ar_sit <- which((ar_id_df$Document[i]==sd_content$ar_ID)==TRUE)
ar_con <- sd_content$ar_content[c(ar_sit)]
write.table(ar_con,file=sprintf("sd_articles/sd_%s.utf8",ar_id_df$Document[i]),fileEncoding = "UTF-8",quote = F,row.names = F,col.names = F)
}
訂閱:
意見 (Atom)