problem link:
Discuss: if you know the 2D. then the problem may pretty for you. how i did it first calculate all corner index value.then just increase the value of i and j. and you may trouble how to choose the corner point as it change then use them and analyse ara[i][j], ara[j][n-1-i], ara[n-1-i][n-1-j], ara[n-1-j][i] first traverse till n/2 and second loop traverse till n-1-i; examine it . it traverse all the square of the grid. then just print it..
try yourself, before see the code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,cas=0;
while(cin >>n&&n)
{
cas++;
int i,j,cnt=0,flag=0,ara[11][11];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin >>ara[i][j];
}
}
cout <<"Case "<<cas<<':';
for(i=0;i<n/2;i++)
{
int sum=0;
for(j=i;j<n-1-i;j++)
{
sum+=ara[i][j]+ara[j][n-1-i]+ara[n-1-i][n-1-j]+ara[n-1-j][i];
}
cout <<' '<<sum;
}
if(n%2)
cout <<' '<<ara[n/2][n/2];
cout <<endl;
}
return 0;
}
Discuss: if you know the 2D. then the problem may pretty for you. how i did it first calculate all corner index value.then just increase the value of i and j. and you may trouble how to choose the corner point as it change then use them and analyse ara[i][j], ara[j][n-1-i], ara[n-1-i][n-1-j], ara[n-1-j][i] first traverse till n/2 and second loop traverse till n-1-i; examine it . it traverse all the square of the grid. then just print it..
try yourself, before see the code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,cas=0;
while(cin >>n&&n)
{
cas++;
int i,j,cnt=0,flag=0,ara[11][11];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin >>ara[i][j];
}
}
cout <<"Case "<<cas<<':';
for(i=0;i<n/2;i++)
{
int sum=0;
for(j=i;j<n-1-i;j++)
{
sum+=ara[i][j]+ara[j][n-1-i]+ara[n-1-i][n-1-j]+ara[n-1-j][i];
}
cout <<' '<<sum;
}
if(n%2)
cout <<' '<<ara[n/2][n/2];
cout <<endl;
}
return 0;
}
No comments:
Post a Comment