1.每日吐槽
啊~~~~~我吐了呀,PTA怕是对java有意见。。。。。c语言可以过,java就超时,虽然Java运行是比c慢,但是对于不同语言运行时间的标准就不是应该不一样嘛,硬是让我优化了一天,最后被迫捡起我的c语言呜呜呜~(ToT)
2.PTA 7-4 分形的递归输出
分形,具有以非整数维形式充填空间的形态特征。通常被定义为“一个粗糙或零碎的几何形状,可以分成数个部分,且每一部分都(至少近似地)是整体缩小后的形状”,即具有自相似的性质。
一个盒状分形定义如下: 度为1的盒分形为:
X
度为2的盒分形为:
X X
X
X X
依次类推,如果B(n-1)表示n-1度的盒分形,则n度的盒分形递归定义如下:
B(n - 1) B(n - 1)
B(n - 1)
B(n - 1) B(n - 1)
请画出度为n的盒分形的图形
原始的java代码
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
char [][]boxInit = {{'X'}};
int i = 1;
char [][]box = boxInit;
while (n != -1){
while (n != i){
int rowAndCol = (int) Math.pow(3,i);
box = new char[rowAndCol][rowAndCol];
//将BoxInit填入box
copyIn(boxInit,box);
i++;
boxInit = box;
}
if(n == i){
print(box);
}
n = in.nextInt();
}
}
static void print(char [][]box){
for (char[] chars : box) {
for (int j = 0; j < box[0].length; j++) {
if (chars[j] == '\0') {
System.out.print(" ");
} else {
System.out.print(chars[j]);
}
}
System.out.println();
}
System.out.println("-");
}
static void copyIn(char [][]srcBox,char [][]posBox){
int size = srcBox.length;
for(int i = 0;i < size;i++){
for(int j = 0;j < size;j++){
posBox[i][j] = srcBox[i][j];
posBox[i][2 * size + j] = srcBox[i][j];
posBox[i + size][j + size] = srcBox[i][j];
posBox[i + 2 * size][j] = srcBox[i][j];
posBox[i + 2 * size][j + 2 * size] = srcBox[i][j];
}
}
}
}
修改后的java代码
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int [][]matrix = new int[2187][2187];
matrix[0][0] = 1;
int i = 1;
while (i < 7){ //直接生成n = 7时候的矩阵
int rowAndCol = (int) Math.pow(3,i - 1);
for(int r = 0;r < rowAndCol;r++){
for(int c = 0;c < rowAndCol;c++){
matrix[r][c + 2 * rowAndCol] = matrix[r][c];
matrix[r + rowAndCol][c + rowAndCol] = matrix[r][c];
matrix[r + 2 * rowAndCol][c] = matrix[r][c];
matrix[r + 2 * rowAndCol][c + 2 * rowAndCol] = matrix[r][c];
}
}
i++;
}
int n = in.nextInt();
while (n != -1){
int size = (int) Math.pow(3,n - 1);
print(matrix,size);
n = in.nextInt();
}
}
static void print(int [][]matrix,int size){
for(int i = 0;i < size;i++){
for(int j = 0;j < size;j++){
if(matrix[i][j] == 0){
System.out.print(" ");
}else {
System.out.print("X");
}
}
System.out.println();
}
System.out.println("-");
}
}
修改后的c语言版:
#include<stdio.h>
#include<math.h>
void printTo(int matrix[2187][2187],int size){
for(int i = 0;i < size;i++){
for(int j = 0;j < size;j++){
if(matrix[i][j] == 0){
printf(" ");
}else {
printf("X");
}
}
printf("\n");
}
printf("-\n");
}
int main()
{
static int matrix[2187][2187];
matrix[0][0] = 1;
int i = 1;
while (i < 7){ //生成n = 7时候的矩阵
int rowAndCol = (int) pow(3,i - 1);
for(int r = 0;r < rowAndCol;r++){
for(int c = 0;c < rowAndCol;c++){
matrix[r][c + 2 * rowAndCol] = matrix[r][c];
matrix[r + rowAndCol][c + rowAndCol] = matrix[r][c];
matrix[r + 2 * rowAndCol][c] = matrix[r][c];
matrix[r + 2 * rowAndCol][c + 2 * rowAndCol] = matrix[r][c];
}
}
i++;
}
int n;
scanf("%d",&n);
while (n != -1){
int size = (int) pow(3,n - 1);
printTo(matrix, size);
scanf("%d",&n);
}
return 0;
}