Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1

    Ae có code ngắn hơn về nhập ma trận xoắn óc ko ????

    Mã:
    /*#include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #define max 100;
    FILE *fi;
    void input(int *n,int *m)
    {
    	FILE *fo=fopen("vd8p111.txt","r");
    	if(fo==NULL) printf("FILE RONG
    ");
    	fscanf(fo,"%d%d",n,m);
    	fclose(fo);
    }
    void xuly(int n,int m,int a[100][100])
    {
    	int i=0,j=0,k=0,t,u2=n-1,v2=m-1,u1=0,v1=0;
    	for (t=1;t<=(n*m);t++){
    
    		if (i==u1&&j!=v2) {
    			a[i][j]=t;
    			j++;
    		}		
    		if (i!=u2&&j==v2){
    			a[i][j]=t;
    			i++;
    		}		
    		if (i==u2&&j!=v1){
    			a[i][j]=t;
    			j--;
    		}
    		if (i!=u1&&j==v1){
    			a[i][j]=t;
    			i--;
    			k=1;
    		}
    		if(k==1){
    			if(i==u2&&j==v1)u1++;
    			if(i==u1&&j==v1)v2--;
    			if(i==u1&&j==v2)u2--;
    			if(i==u2&&j==v2)v1++;}
    	}
    }
    main()
    {
    	int i,j,n,m,a[100][100];
    	input(&n,&m);
    	xuly(n,m,a);
    	for(i=0;i<n;i++){
    		for(j=0;j<m;j++)
    			printf("%3d",a[i][j]);
    			printf("
    ");
    	}
    	getchar();
    }
    */
    
    
    
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #define max 100;
    FILE *fi;
    void input(int *n,int *m)
    {
    	FILE *fo=fopen("vd8p111.txt","r");
    	if(fo==NULL) printf("FILE RONG
    ");
    	fscanf(fo,"%d%d",n,m);
    	fclose(fo);
    }
    void xuly(int n,int m,int a[100][100])
    {
    	int i=0,j=0,k=1,t,u2=n-1,v2=m-1,u1=0,v1=0,dem=1;
    	while(dem<(m*n+1)){
    		while(j<v2&&dem<(m*n+1)){
    			a[i][j]=dem;
    			dem++;			
    			j++;
    		}
    		while(i<u2&&dem<(m*n+1)){
    			a[i][j]=dem;
    			dem++;
    			i++;
    		}
    		while(j>v1&&dem<(m*n+1)){
    			a[i][j]=dem;
    			dem++;
    			j--;
    		}
    		u1++;v1++;v2--;u2--;
    		while (i>u1&&dem<(m*n+1)){
    			a[i][j]=dem;
    			dem++;
    			i--;
    		}
    		
    	}
    }
    main()
    {
    	int i,j,n,m,a[100][100];
    	input(&n,&m);
    	xuly(n,m,a);
    	for(i=0;i<n;i++){
    		for(j=0;j<m;j++)
    			printf("%3d",a[i][j]);
    			printf("
    ");
    	}
    	getchar();
    }
    Đây là code của e. Nhưng mà thấy nhiều vòng lặp quá .
    Có bạn nào cho e code ngắn hơn đc không ạ???
    Với fileinput là : 6 7 in ra
    1 2 3 4 5 6 7
    22 23 24 25 26 27 8
    21 36 37 38 39 28 9
    20 35 42 41 40 29 10
    19 34 33 32 31 30 11
    18 17 16 15 14 13 12

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mã:
    #include<stdio.h>
    int kt(int a[][100],int dai, int rong)
    {
    	int i,j;
    	for(i=0;i<dai;i++)
    		for(j=0;j<rong;j++)
    			if(a[i][j]==0) return 1; 
    	return 0;
    
    }
    main()
    {
    	int dong,dai,dai1,rong1,i=0,cot,rong,k=1,a[100][100];
    	FILE*f=fopen("input.txt","rt");
    	fscanf(f,"%d%d",&dai,&rong);
    	for(dong=0;dong<rong;dong++)
    		for(cot=0;cot<dai;cot++)
    			a[dong][cot]=0;
    	dai1=dai;
    	rong1=rong;
    	dong=0;
    	cot=0;
    	while(kt(a,dai,rong))
    	{
    		for(;cot<dai;cot++,k++)
    			if(a[dong][cot]==0) a[dong][cot]=k;
    		for(cot--,dong++;dong<rong;dong++,k++)
    			if(a[dong][cot]==0) a[dong][cot]=k;
    		for(dong--,cot--;cot>=i;cot--,k++)
    			if(a[dong][cot]==0) a[dong][cot]=k;
    		for(dong--,cot++;dong>i;dong--,k++)
    			if(a[dong][cot]==0) a[dong][cot]=k;
    		dong++;
    		cot++;
    		dai--;
    		rong--;
    		i++;
    	}
    	
    	for(dong=0;dong<rong1;dong++)
    		for(cot=0;cot<dai1;cot++)
    			{
    				printf("%d\t",a[dong][cot]);
    				if(cot==dai1-1) printf("
    ");
    			}
    }

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •