CSP考试 2014年12月第2题 Z字形扫描 C语言实现

《CSP考试 2014年12月第2题 Z字形扫描 C语言实现》

《CSP考试 2014年12月第2题 Z字形扫描 C语言实现》

#include <stdio.h>
int main()
{
	//int NUM=4;
	int NUM;
	scanf("%d",&NUM);
	int a[NUM][NUM];
	/*
	a[0][0]=1;
	a[0][1]=5;
	a[0][2]=3;
	a[0][3]=9;
		
	a[1][0]=3;	
	a[1][1]=7;
	a[1][2]=5;
	a[1][3]=6;
	
	a[2][0]=9;
	a[2][1]=4;
	a[2][2]=6;
	a[2][3]=4;
	
	a[3][0]=7;
	a[3][1]=3;
	a[3][2]=1;
	a[3][3]=3;
	*/
	int b[NUM*NUM];
	int c[NUM*NUM];
	
	int i;
	int j;
	
	for(i=0;i<NUM;i++)
    {
    	for(j=0;j<NUM;j++)
    	{
    		scanf("%d",&a[i][j]);
		}
	}
	
	int temp=0;
	
	for(i=1;i<=NUM;i++)
	{
	    int h=0;
	    int k=i-1;
		for(j=0;j<i;j++)
		{
			b[temp]=a[h][k];
			c[temp]=b[temp];
			k--;
			temp++;
			h++;
		}
	} 
	/*
	for(i=0;i<NUM*NUM;i++)
	{
		printf("b %d = %d\n",i,b[i]);
	}*/

	//printf("b 10 = %d\n",b[10]);
	for(i=NUM-1;i>0;i--)
	{
		//printf("hello\n");
		int h=NUM-i; 
		int k=NUM-1;
		for(j=0;j<i;j++)
		{
			b[temp]=a[h][k];
			c[temp]=b[temp];
			k--;
			temp++;
			h++;
		} 
	}
	/*
	for(i=0;i<NUM*NUM;i++)
	{
		printf("b %d = %d\n",i,b[i]);
	}*/
	//
	temp=0;
	for(i=1;i<=NUM;i++)
	{
		if(i%2==1)
		{
			int t;
			t=temp+i;
			for(j=0;j<i;j++)
			{
				c[temp]=b[t-j-1];//
				temp++;
			}
		}
		else if(i%2==0)
		{
			temp=temp+i;
		}
	}
	
	for(i=NUM-1;i>0;i--)
	{
			if(i%2==1)
		{
			int t;
			t=temp+i;
			for(j=0;j<i;j++)
			{
				c[temp]=b[t-j-1];//
				temp++;
			}
		}
		else if(i%2==0)
		{
			temp=temp+i;
		}
	}
	
	
	for(i=0;i<NUM*NUM;i++)
	{
		printf("%d ",c[i]);
	}
     /*	
	int i=0;
	int j=0;
	int n[NUM*2-1];
	n[0]=1;//斜行中数的个数 
	printf("n[0]=%d\n",n[0]);
	int k;
	for(k=1;k<(NUM*2-1);k++)
	{
		if(k<NUM)
		{
			n[k]=n[k-1]+1;
		}
		if(k>=NUM)
		{
			n[k]=n[k-1]-1;
		}
		printf("n[%d]=%d\n",k,n[k]);
	}
	printf("\n");
	
	int t[NUM*2-1];
	t[0]=0;//i+j 
	for(k=1;k<(NUM*2-1);k++)
	{
		t[k]=t[k-1]+1;
	}
	for(k=0;k<(NUM*2-1);k++)
	{
		printf("t[%d]=%d\n",k,t[k]);
	}*/ 
	return 0;
} 

《CSP考试 2014年12月第2题 Z字形扫描 C语言实现》

    原文作者:Z字形编排问题
    原文地址: https://blog.csdn.net/woniupengpeng/article/details/51042448
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞