ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1319. Hotel

easy solution in c++
Posted by Yucheng 27 Feb 2019 15:13
#include<iostream>

using namespace std;

int main(){
int a;
cin>>a;
int board[a][a];
int k=1;
for(int i=1;i<=a*a;i++){
for(int m=0;m<a;++m){
for(int n=0;n<a;++n){
if(m-n+a==i){
board[m][n]=k;
k++;
}
}
}

}

for(int m=0;m<a;++m){
for(int n=0;n<a;++n){
cout<<board[m][n]<<" ";
}
cout<<endl;
}

return 0;
}
Re: easy solution in c++
Posted by Md Asaduzzaman 30 Apr 2021 21:19
I think it has less run time memory.
Yucheng wrote 27 February 2019 15:13
#include<bits/stdc++.h>
using namespace std;

int main(){
int n,num=1;

cin>>n;

int arr[n][n];

for (size_t i = 0; i < 2*n-1; i++)
{
int j=n-1-i,k=0;
do
{
if(j<0){
j=0;
k=i-n+1;
}
arr[k][j]=num;
num++;
j++;
k++;
} while (j<n && k<n);
}

for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < n; j++)
{
cout<<arr[i][j]<<" ";
}

cout<<endl;

}
}

Edited by author 30.04.2021 21:19

Edited by author 30.04.2021 21:21