武汉科技大学ACM :1009: 零起点学算法63——弓型矩阵

Problem Description

输出n*m的弓型矩阵

Input

多组测试数据
每组输入2个整数 n和m(不大于20)

Output

输出n*m的弓型矩阵,要求左上角元素是1,(每个元素占2个位置,靠右)

Sample Input

4 3

Sample Output

 1  2  3
 6  5  4
 7  8  9
12 11 10


我的代码:
 1 #include<iostream>
 2 #include <iomanip>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int m,n,i,p;
 8     while(cin>>m>>n)
 9     {
10         for(i=1;i<=m;i++)
11         {
12                 if(i%2==1)
13                 {    
14                     for(p=1;p<=n;p++)
15                         if(p!=n)
16                             cout<<setw(2)<<p+n*(i-1)<<" ";
17                         else
18                             cout<<setw(2)<<p+n*(i-1);
19                     cout<<endl;
20                 }
21                 else
22                 {
23                     for(p=1;p<=n;p++)
24                         if(p!=n)
25                             cout<<setw(2)<<(((i-1)*n)+(n+1-p))<<" ";
26                         else
27                             cout<<setw(2)<<(((i-1)*n)+(n+1-p));
28                     cout<<endl;
29                 }
30         }
31     }
32     return 0;
33 }

其他代码:

 1 #include<stdio.h>
 2 int main(){
 3     int m,n;
 4     while(scanf("%d%d",&n,&m)!=EOF)
 5     {
 6         int dir=1;
 7         int count=0;
 8         for(int i=0;i<n;i++){
 9             if(dir){
10                 for(int j=0;j<m;j++){
11                     
12                     if(j==m-1)
13                     {
14                         printf("%2d",++count);
15                     }
16                     else
17                     {
18                         printf("%2d ",++count);
19                     }
20                 }
21                 
22             }else{
23                 count=count+m;
24                 for(int j=0;j<m;j++){
25                     
26                     if(j==m-1)
27                     {
28                         printf("%2d",count--);
29                     }
30                     else
31                     {
32                     printf("%2d ",count--);
33                     }
34                 }
35                 count=count+m;
36             }
37             dir=!dir;
38             printf("\n");
39         }
40         
41     }    
42 }
 1 #include <iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int row,col;
 8     int k=-1;
 9     int *a=NULL;
10     int i,j;
11     while(cin>>row>>col)
12     {
13         k=-0;
14         if(row>20 || col>20)
15         {
16             return 0;
17         }
18 
19         a=new int[row*col];
20 
21         for(i=0;i<row;++i)
22         {
23             if(i%2==0)
24             {
25                  for(j=0;j<col;++j)
26                  {
27                      a[i*col+j]=++k;
28                  }
29 
30             }
31             else
32             {
33                   for(j=col-1;j>=0;--j)
34                  {
35                      a[i*col+j]=++k;
36                  }
37             }
38 
39         }
40         for(i=0;i<row;++i)
41         {
42             for(j=0;j<col-1;++j)
43             {
44                 cout<<setw(2)<<a[i*col+j]<<" ";
45             }
46             cout<<setw(2)<<a[i*col+j]<<endl;
47         }
48 
49 
50 
51     }
52     return 0;
53 }

 

转载于:https://www.cnblogs.com/liuwt365/p/4147426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值