Code Ma trận ZigZag ngang trong C++

Đây là bài hoàn chỉnh, code trên visual studio 2010

#include <iostream>
using namespace std;

int a[100][100],d,c;
void XuLy(int a[][100],int d,int c)
{
    int dem=1;
    for(int i=0;i<d;i++)
    {
            if(i%2==0)
            for(int j=0;j<c;j++)
              a[i][j]=dem++;
            else
            for(int j=c-1;j>=0;j--)
              a[i][j]=dem++;
    }
}
void Xuat(int a[][100],int d,int c)
{
    for(int i=0;i<d;i++)
    {
      for(int j=0;j<c;j++)
      if(a[i][j]>=10)
          cout<<a[i][j]<<" ";
          else
          cout<<" "<<a[i][j]<<" ";
          cout<<endl<<endl;
    }
}
void main()
{
   cout<<"Nhap dong: ";cin>>d;
   cout<<"Nhap cot: ";cin>>c;
   XuLy(a,d,c);
   cout<<"Ma tran zigzag ngang la: \n\n";
   Xuat(a,d,c);
}  

Share this

Related Posts

Previous
Next Post »