UVA 11470 Square Sums Solution
UVA 11470 Square Sums Solution
#include<iostream>
using namespace std;
int arr[10][10],n,res[5];
int calculateSquare(int x)
{
int sum=0,i,j;
i=n-1-x;
for(j=x;j<n-x;j++)
sum=sum+arr[x][j]+arr[i][j];
j=n-1-x;
for(i=x+1;i<j;i++)
sum=sum+arr[i][x]+arr[i][j];
return sum;
}
int main()
{
int i,j,k,sum,lim,count,test=1;
while(cin>>n)
{
if(n==0)
break;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
cout<<"Case "<<test++<<": ";
if(n==1)
{
cout<<arr[0][0]<<endl;
continue;
}
lim=n/2;
count=0;
for(i=0;i<lim;i++)
{
res[count++]=calculateSquare(i);
}
if(n%2==1)
res[count++]=arr[n/2][n/2];
for(i=0;i<count-1;i++)
cout<<res[i]<<" ";
cout<<res[i]<<endl;
}
return 0;
}
using namespace std;
int arr[10][10],n,res[5];
int calculateSquare(int x)
{
int sum=0,i,j;
i=n-1-x;
for(j=x;j<n-x;j++)
sum=sum+arr[x][j]+arr[i][j];
j=n-1-x;
for(i=x+1;i<j;i++)
sum=sum+arr[i][x]+arr[i][j];
return sum;
}
int main()
{
int i,j,k,sum,lim,count,test=1;
while(cin>>n)
{
if(n==0)
break;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
cout<<"Case "<<test++<<": ";
if(n==1)
{
cout<<arr[0][0]<<endl;
continue;
}
lim=n/2;
count=0;
for(i=0;i<lim;i++)
{
res[count++]=calculateSquare(i);
}
if(n%2==1)
res[count++]=arr[n/2][n/2];
for(i=0;i<count-1;i++)
cout<<res[i]<<" ";
cout<<res[i]<<endl;
}
return 0;
}
Comments
Post a Comment