-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathA Simple Problem With Matrix.cpp
137 lines (132 loc) · 3.17 KB
/
A Simple Problem With Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <string.h>
#include <fstream>
using namespace::std;
int main()
{
char a[100010],b[500][500];
int flag=1;
while (gets(a))
{
memset(b, 0, sizeof(b));
unsigned long i,j,k,lab,count=1,len=strlen(a);
for (k=0; k<len;)
{
if (count%2==0)
{
for (i=1,j=count; j>=1&&k<len; i++,j--)
b[i][j]=a[k++];
count++;
}
else
{
for (i=count,j=1; i>=1&&k<len; i--,j++)
b[i][j]=a[k++];
count++;
}
}
cout << "Matrix " << flag++ << ":\n";
k=0;
for (i=1; i<=count; i++)
{
lab=1;
for (j=1; j<=count&&b[i][j]!='\0'; j++)
{
if (j>1)
cout << ' ';
cout << b[i][j];
if (k>=len)
break;
k++;
lab=0;
}
if (lab==0)
cout << '\n';
}
}
return 0;
}
/*
#include <string.h>
#include <fstream>
using namespace::std;
int main()
{
char a[100010],b[500][500];
int flag=1;
while (gets(a))
{
for(int i=0;i<500;i++)
for(int j=0;j<500;j++)
b[i][j]=0;
unsigned long i,j,k,lab,count=1,len=strlen(a);
for (k=0; k<len;)
{
if (count%2==0)
{
for (i=1,j=count; j>=1&&k<len; i++,j--)
b[i][j]=a[k++];
count++;
}
else
{
for (i=count,j=1; i>=1&&k<len; i--,j++)
b[i][j]=a[k++];
count++;
}
}
cout << "Matrix " << flag++ << ":\n";
k=0;
for (i=1; i<=count; i++)
{
lab=1;
for (j=1; j<=count&&b[i][j]!='\0'; j++)
{
if (j>1)
cout << ' ';
cout << b[i][j];
if (k>=len)
break;
k++;
lab=0;
}
if (lab==0)
cout << '\n';
}
}
return 0;
}
*/
/*
Problem description
This is a very simple problem. Given an array A[1: n*n](n*n<=100000),your task is to put the n*n elements into a two-dimension array B(1: n, 1: n) with snap shape.
That is:
B(1, 1)=A(1),
B(1, 2)=A(2), B(2, 1)=A(3),
B(3, 1)=A(4), B(2, 2)=A(5), B(1, 3)=A(6),
……
The rest may be deduced by analogy.
The array B can be showed as following:
A[1] A[2] A[6] A[7] ---
A[3] A[5] A[8] A[14] ---
A[4] A[9] A[13] --- ---
A[10] A[12] --- --- ---
A[11] --- --- --- ---
Input
The input consists of several test cases.Each case contains a line with m(m <= 100000) characters,indicating the array A.
Output
For each test case, output the array B using the exact output format shown below.On each line, two nearby elements should be seperated by a single space.
Sample Input
*
kkd kagak gakg k j
Sample Output
Matrix 1:
*
Matrix 2:
k k a g k
d k a g
k k
a
g
j
*/