• 武汉科技大学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("
    ");
    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 }
  • 相关阅读:
    C++ string 实现大整数相加减
    HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】
    Quick-Cocos2d3.2RC1在Code IDE中实现代码提示
    Codeforces 558C Amr and Chemistry
    Linux编程---进程通信
    HDU 5371 Hotaru&#39;s problem(Manacher算法+贪心)
    微社区
    创业忌讳
    微信公众平台开发(82) 天气预报
    天气预报接口
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4147426.html
Copyright © 2020-2023  润新知