
Ярослав
6 год назад
ПОМОГИТЕ! МНОГО БАЛЛОВ! Написать алгоритм/программу для n>32768

ОТВЕТЫ

Северин
Jul 1, 2019
#include lt;iostreamgt;
#include lt;math.hgt;
using namespace std;
int main() {
int n;
cin gt;gt; n;
int a[n];
bool flag;
for(int i = 0; i lt; n; a[i]=i+1, i++);
for(int i = 0; i lt; sqrt(n); i++) {
flag = true;
for (int j = 0; j lt; sqrt(n); flag = !flag,j++) {
if (flag) {
cout lt;lt; a[j*(int)sqrt(n) + i] lt;lt; ;
} else {
cout lt;lt; a[j*(int)sqrt(n) + (int)sqrt(n)- 1 - i] lt;lt; ;
}
}
cout lt;lt; endl;
}
}
#include lt;math.hgt;
using namespace std;
int main() {
int n;
cin gt;gt; n;
int a[n];
bool flag;
for(int i = 0; i lt; n; a[i]=i+1, i++);
for(int i = 0; i lt; sqrt(n); i++) {
flag = true;
for (int j = 0; j lt; sqrt(n); flag = !flag,j++) {
if (flag) {
cout lt;lt; a[j*(int)sqrt(n) + i] lt;lt; ;
} else {
cout lt;lt; a[j*(int)sqrt(n) + (int)sqrt(n)- 1 - i] lt;lt; ;
}
}
cout lt;lt; endl;
}
}
Считается, что должна получиться матрица n х n. И, конечно, элементы матрицы не вводятся, а вычисляются.
Вот вариант программы:
#include lt;iostreamgt;
using namespace std;
int main() {
int n;
cin gt;gt; n;
int a[n][n];
for(int j = 0; j lt; n; j++){
if (j2 == 0)
for(int i = 0; i lt; n; i++) a[i][j]=(i+1)+2*n*((j+1)/2);
else for(int i = 0; i lt; n; i++) a[i][j]=-i+2*n*((j+1)/2);
}
for(int i = 0; i lt; n; i++) {
for(int j = 0; j lt; n; j++)
cout lt;lt; a[i][j] lt;lt; ;
cout lt;lt; endl;
}
}
Пример:
5
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
Вот вариант программы:
#include lt;iostreamgt;
using namespace std;
int main() {
int n;
cin gt;gt; n;
int a[n][n];
for(int j = 0; j lt; n; j++){
if (j2 == 0)
for(int i = 0; i lt; n; i++) a[i][j]=(i+1)+2*n*((j+1)/2);
else for(int i = 0; i lt; n; i++) a[i][j]=-i+2*n*((j+1)/2);
}
for(int i = 0; i lt; n; i++) {
for(int j = 0; j lt; n; j++)
cout lt;lt; a[i][j] lt;lt; ;
cout lt;lt; endl;
}
}
Пример:
5
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
236
Смежные вопросы: