PostCode

Posts 1–5 of 5 · Page 1 of 1
Code
Can anybody explain something out of this?

#include <fstream>
#include <cmath>
using namespace std;
#define N 501
ifstream f("innn.in");
ofstream g("outtt.out");
int n, k, l=N, c=N;
int a[N][N], mx=-5000*N*N-1;

int main()
{
int i, j, s, ii, jj;
f>>n>>k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
f>>a[i][j];
for(i=1;i<=n-k+1;i++)
for(j=1;j<=n-k+1;j++){
s=0;
for(jj=j;jj<=j+k-1;jj++)
for(ii=1;ii<=n;ii++)s+=a[ii][jj];
for(ii=i;ii<=i+k-1;ii++)
for(jj=1;jj<j;jj++)s+=a[ii][jj];
for(ii=i;ii<=i+k-1;ii++)
for(jj=j+k;jj<=n;jj++)s+=a[ii][jj];
if(s>mx) mx=s, l=i, c=j;
}
g<<mx<<' '<<l<<' '<<c<<'\n';
return 0;
}
Arrays start at index 0. pls.
Quote Originally Posted by Hell_Demon View Post
Arrays start at index 0. pls.
I know all the initialization part but what the fuck is this:

int i, j, s, ii, jj;
f>>n>>k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
f>>a[i][j];
for(i=1;i<=n-k+1;i++)
for(j=1;j<=n-k+1;j++){
s=0;
for(jj=j;jj<=j+k-1;jj++)
for(ii=1;ii<=n;ii++)s+=a[ii][jj];
for(ii=i;ii<=i+k-1;ii++)
for(jj=1;jj<j;jj++)s+=a[ii][jj];
for(ii=i;ii<=i+k-1;ii++)
for(jj=j+k;jj<=n;jj++)s+=a[ii][jj];
if(s>mx) mx=s, l=i, c=j;
}
g<<mx<<' '<<l<<' '<<c<<'\n';
return 0;

Like what does it do.
What's the output?
the output depends on the innn.in file. It reads two numbers, which make up the grid size, then it reads that many entries into the grid, then does some magic.

Code:
#include <fstream>
#include <cmath>
using namespace std;
#define N 501
ifstream f("innn.in");
ofstream g("outtt.out");
int n, k, l=N, c=N;
int a[N][N], mx=-5000*N*N-1;

int main()
{
	int i, j, s, ii, jj;
	f>>n>>k;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			f>>a[i][j];
		}
	}
	for(i=1;i<=n-k+1;i++)
	{
		for(j=1;j<=n-k+1;j++)
		{
			s=0;
			for(jj=j;jj<=j+k-1;jj++)
			{
				for(ii=1;ii<=n;ii++)
				{
					s+=a[ii][jj];
				}
			}
			for(ii=i;ii<=i+k-1;ii++)
			{
				for(jj=1;jj<j;jj++)
				{
					s+=a[ii][jj];
				}
			}
			for(ii=i;ii<=i+k-1;ii++)
			{
				for(jj=j+k;jj<=n;jj++)
				{
					s+=a[ii][jj];
				}
			}
			if(s>mx) 
				mx=s, l=i, c=j;
		}
	}
	g<<mx<<' '<<l<<' '<<c<<'\n';
	return 0;
}
This is a much more readable variant. Variable names are still cuck.
Quote Originally Posted by Hell_Demon View Post
the output depends on the innn.in file. It reads two numbers, which make up the grid size, then it reads that many entries into the grid, then does some magic.

Code:
#include <fstream>
#include <cmath>
using namespace std;
#define N 501
ifstream f("innn.in");
ofstream g("outtt.out");
int n, k, l=N, c=N;
int a[N][N], mx=-5000*N*N-1;

int main()
{
	int i, j, s, ii, jj;
	f>>n>>k;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			f>>a[i][j];
		}
	}
	for(i=1;i<=n-k+1;i++)
	{
		for(j=1;j<=n-k+1;j++)
		{
			s=0;
			for(jj=j;jj<=j+k-1;jj++)
			{
				for(ii=1;ii<=n;ii++)
				{
					s+=a[ii][jj];
				}
			}
			for(ii=i;ii<=i+k-1;ii++)
			{
				for(jj=1;jj<j;jj++)
				{
					s+=a[ii][jj];
				}
			}
			for(ii=i;ii<=i+k-1;ii++)
			{
				for(jj=j+k;jj<=n;jj++)
				{
					s+=a[ii][jj];
				}
			}
			if(s>mx) 
				mx=s, l=i, c=j;
		}
	}
	g<<mx<<' '<<l<<' '<<c<<'\n';
	return 0;
}
This is a much more readable variant. Variable names are still cuck.
But the grid is:
int a[N][N]

and N is defined as 501

and I've only seen 2 nested for loops never like this:
for(i=1;i<=n-k+1;i++)
{
for(j=1;j<=n-k+1;j++)
{
s=0;
for(jj=j;jj<=j+k-1;jj++)
{
for(ii=1;ii<=n;ii++)
{
s+=a[ii][jj];
}
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us