武汉科技大学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 }
时间: 2024-08-24 13:56:34

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

1156: 零起点学算法63——弓型矩阵

1156: 零起点学算法63--弓型矩阵 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 3403  Accepted: 1284[Submit][Status][Web Board] Description 输出n*m的弓型矩阵 Input 多组测试数据 每组输入2个整数 n和m(不大于20) Output 输出n*m的弓型矩阵,要求左上角元素是1,(每个元素占2个位置,靠右) Sample In

Problem D: 零起点学算法95——弓型矩阵

#include<stdio.h> #include<string.h> int main() { int n,m,a[20][20]; while(scanf("%d%d",&n,&m)!=EOF) { int t,i,j; t=a[i=0][j=0]=1; while(t<n*m) { while(j+1<m) a[i][++j]=++t; a[++i][j]=++t; while(j-1>=0) a[i][--j]=++t

1165: 零起点学算法72——首字母变大写

1165: 零起点学算法72--首字母变大写 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 705  Accepted: 439[Submit][Status][Web Board] Description 输入一个英文句子,将每个单词的第一个字母改成大写字母. Input 输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行. Output 请输出按照要求改写后的英文句

1122: 零起点学算法29——等级分制度

1122: 零起点学算法29--等级分制度 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 2657  Accepted: 1379[Submit][Status][Web Board] Description ACM集训队每年都要招新队员.他们很多从大一开始就练了.当然一开始都是从hello world练起的,后来很多人成了牛人. 你想参加吗?如果你感兴趣,赶快加入.可以写Email武科大ACM俱

1169: 零起点学算法76——绝对公正的裁判

1169: 零起点学算法76--绝对公正的裁判 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 510  Accepted: 336[Submit][Status][Web Board] Description 大家知道我们学校的OnlineJudge吗?,你知道他会告诉你什么呢? Compiling : 您提交的代码正在被编译.Running : 您的程序正在OJ上运行.Judging : OJ

1127: 零起点学算法34——继续求多项式

1127: 零起点学算法34--继续求多项式 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 3481  Accepted: 1985[Submit][Status][Web Board] Description 输入1个正整数n, 计算1+(1+2)+(1+2+3)+...+(1+2+3+...+n) Input 输入正整数n(多组数据) Output 输出1+(1+2)+(1+2+3)+...+

1128: 零起点学算法35——再求多项式(含浮点)

1128: 零起点学算法35--再求多项式(含浮点) Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 2141  Accepted: 1002[Submit][Status][Web Board] Description 输入一个整数n,计算 1+1/(1-3)+1/(1-3+5)+...+1/(1-3+5-...+2n-1)的值 Input 输入一个整数n(多组数据) Output 出1+1/(1

1097:零起点学算法04——再模仿一个算术题

1097: 零起点学算法04--再模仿一个算术题 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 2627  Accepted: 2202[Submit][Status][Web Board] Description 上题会模仿了吧.再来模仿一个. 现在要求你模仿一个乘法的算术题 Input 没有输入 Output 输出9乘以10的值 Sample Output 90 Source 零起点学算法

1098: 零起点学算法05——除法算术题

1098: 零起点学算法05--除法算术题 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 2346  Accepted: 1932[Submit][Status][Web Board] Description 乘法会了,除法也一样的.不要跟我说不会哦. Input 没有输入 Output 输出12除以2的值,计算让计算机去做哦 Sample Output 6 Source 零起点学算法 1 #i