Problem Description
Bow-type matrix for output n*m
Input multiple sets of test data
Input 2 integers n and m (not greater than) output per group
The bow matrix of the output n*m requires that the upper-left element is 1, (each element occupies 2 positions, on the right)
Sample Input
4 3
Sample Output
1 2 3 6 5 4 7 8 912 11 10
My Code:
1#include <iostream>2#include <iomanip>3 using namespacestd;4 5 intMain ()6 {7 intm,n,i,p;8 while(cin>>m>>N)9 {Ten for(i=1; i<=m;i++) One { A if(i%2==1) - { - for(p=1;p <=n;p++) the if(p!=N) -COUT<<SETW (2) <<p+n* (I-1) <<" "; - Else -COUT<<SETW (2) <<p+n* (I-1); +cout<<Endl; - } + Else A { at for(p=1;p <=n;p++) - if(p!=N) -COUT<<SETW (2) << (((I-1) *n) + (n+1-p)) <<" "; - Else -COUT<<SETW (2) << (((I-1) *n) + (n+1-p)); -cout<<Endl; in } - } to } + return 0; -}
Other code:
1#include <stdio.h>2 intMain () {3 intM,n;4 while(SCANF ("%d%d", &n,&m)! =EOF)5 {6 intDir=1;7 intCount=0;8 for(intI=0; i<n;i++){9 if(dir) {Ten for(intj=0; j<m;j++){ One A if(j==m-1) - { -printf"%2d",++count); the } - Else - { -printf"%2d",++count); + } - } + A}Else{ atcount=count+m; - for(intj=0; j<m;j++){ - - if(j==m-1) - { -printf"%2d", count--); in } - Else to { +printf"%2d", count--); - } the } *count=count+m; $ }Panax Notoginsengdir=!dir; -printf"\ n"); the } + A } the}
1#include <iostream>2#include <iomanip>3 using namespacestd;4 5 intMain ()6 {7 intRow,col;8 intk=-1;9 int*a=NULL;Ten inti,j; One while(cin>>row>>Col) A { -k=-0; - if(row> -|| Col> -) the { - return 0; - } - +A=New int[row*Col]; - + for(i=0; i<row;++i) A { at if(i%2==0) - { - for(j=0; j<col;++j) - { -a[i*col+j]=++K; - } in - } to Else + { - for(j=col-1; j>=0;--j) the { *a[i*col+j]=++K; $ }Panax Notoginseng } - the } + for(i=0; i<row;++i) A { the for(j=0; j<col-1;++j) + { -COUT<<SETW (2) <<a[i*col+j]<<" "; $ } $COUT<<SETW (2) <<a[i*col+j]<<Endl; - } - the - Wuyi } the return 0; -}
Wuhan University of Science and technology acm:1009:0 start-up algorithm 63--bow matrix