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2 2 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2
2 2 2 2 2 2
我想用编程的方式来完成上面的运行效果,这个二维数组群是用0,1,2 三个数字来填充的。
编程代码如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[6][6];
FILE *fp;
if ((fp = fopen ("D:\\wenjian.txt", "a+")) == NULL)
{
printf("打开文件失败\n");
exit(1);
}
for(int i = 0; i < 8 ; i++)//F
{
int n = i ;
int m = i ;
fprintf(fp,"i = %d \n",i);
for ( int j = 1; j<7; ++j)//G
{
a[0][j]=j;
fprintf(fp, "%3d ",a[0][j]);
}
fprintf(fp,"\n");
for(int j = 1; j < 3; j++)//H
{
for (int k=0; k<6; k++)//I
{
a[j][k]=(n&1);
fprintf(fp, "%3d ", a[j][k]);
}
fprintf(fp,"\n");
n = n>>1;
}
for(int j = 3; j < 6; j++)//J
{
for (int k=0; k<6; k++)//K
{
a[j][k]=!!(m&4);//L
fprintf(fp, "%3d ", a[j][k]);
}
fprintf(fp,"\n");
m = m<<1;
}
}
fclose(fp);
return 0;
}
这段代码肯定不对,因为它是用0和1 来填充二维数组群的,请问在原代码的基础上做如何的修改就可以完成该任务。谢谢!!