LightOJ - 1008
以下为翻译
题意
也就是问每个数在哪一格
思路
同样是找规律
斜线:n*(n-1)+1
从最下面这一行和最左边这一列来看
偶数在偶数列/行,奇数在奇数列/行
奇数层数字大小从下到左(逆时针)顺序
偶数层数字大小从左到下(顺时针)顺序
因此判断时奇数层与偶数层应有一个相反的判断点
错误示范
#include<stdio.h>
#include<math.h>
int main()
{
long long s,n,sum,i=1,t;
double m;
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&s);
m=sqrt(s);
n=(long long)m;
if(m>n)
{
n++;
}
sum=n*(n-1)+1;
if(n%2>0)
{
if(s>sum)
{
printf("Case %lld:%lld %lld\n",i,n-(s-sum),n);//输出少了个空格,下面问题一样
}
else
{
printf("Case %lld:%lld %lld\n",i,n,n-(sum-s));
}
}
else
{
if(s>sum)
{
printf("Case %lld:%lld %lld\n",i,n,n-(s-sum));
}
else
{
printf("Case %lld:%lld %lld\n",i,n-(sum-s),n);
}
}
i++;
}
return 0;
}
正确代码
#include<stdio.h>
#include<math.h>
int main()
{
long long s,n,sum,i=1,t;
double m;
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&s);
m=sqrt(s);
n=(long long)m;
if(m>n)
{
n++;
}
sum=n*(n-1)+1;
if(n%2>0)
{
if(s>sum)
{
printf("Case %lld: %lld %lld\n",i,n-(s-sum),n);
}
else
{
printf("Case %lld: %lld %lld\n",i,n,n-(sum-s));
}
}
else
{
if(s>sum)
{
printf("Case %lld: %lld %lld\n",i,n,n-(s-sum));
}
else
{
printf("Case %lld: %lld %lld\n",i,n-(sum-s),n);
}
}
i++;
}
return 0;
}