首页 > 其他分享 >UVa 10161 Ant on a Chessboard (简单数学)

UVa 10161 Ant on a Chessboard (简单数学)

时间:2023-04-12 11:41:04浏览次数:43  
标签:grids sqr Chessboard 10161 Ant grid she corner was


10161 - Ant on a Chessboard

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=1102

Background

  One day, an ant called Alice came to an M*M chessboard. She wanted to go around all the grids. So she began to walk along the chessboard according to this way: (you can assume that her speed is one grid per second)

  At the first second, Alice was standing at (1,1). Firstly she went up for a grid, then a grid to the right, a grid downward. After that, she went a grid to the right, then two grids upward, and then two grids to the left…in a word, the path was like a snake.

  For example, her first 25 seconds went like this:

  ( the numbers in the grids stands for the time when she went into the grids)

 

25

24

23

22

21

10

11

12

13

20

9

8

7

14

19

2

3

6

15

18

1

4

5

16

17

5

4

3

2

1

 


1    2    3    4     5

At the 8th second , she was at (2,3), and at 20th second, she was at (5,4).

Your task is to decide where she was at a given time.

(you can assume that M is large enough)

Input

  Input file will contain several lines, and each line contains a number N(1<=N<=2*10^9), which stands for the time. The file will be ended with a line that contains a number 0.

Output

  For each input situation you should print a line with two numbers (x, y), the column and the row number, there must be only a space between them.

Sample Input

8

20

25

0

Sample Output

2 3

5 4

1 5


题意:找到数字对应的行和列。

思路:找规律,奇数行,起始为行数的平方。偶数列,起始为列数的平方。行和列有与数字匹配的规律。依次确定所在的行和列。




完整代码:

/*0.012s*/

#include<cstdio>
#include<cmath>

int main(void)
{
	int sqr, n, x, y, corner;
	while (scanf("%d", &n), n)
	{
		sqr = (int)sqrt(n - 1) + 1;
		corner = sqr * sqr - sqr + 1;
		if ((sqr & 1) == 0)
		{
			if (n >= corner)
			{
				x = sqr;
				y = sqr - (n - corner);
			}
			else
			{
				x = sqr - (corner - n);
				y = sqr;
			}
		}
		else
		{
			if (n >= corner)
			{
				x = sqr - (n - corner);
				y = sqr;
			}
			else
			{
				x = sqr;
				y = sqr - (corner - n);
			}
		}
		printf("%d %d\n", x, y);
	}
	return 0;
}




标签:grids,sqr,Chessboard,10161,Ant,grid,she,corner,was
From: https://blog.51cto.com/u_5535544/6185319

相关文章

  • Semantic Kernel 入门系列:
    语义的归语义,语法的归语法。基础定义最基本的NativeFunction定义只需要在方法上添加SKFunction的特性即可。usingMicrosoft.SemanticKernel.SkillDefinition;usingMicrosoft.SemanticKernel.Orchestration;namespaceMySkillsDirectory;publicclassMyCSharpSkill......
  • React+Antd在使用form表单提交DatePicker日期框的时候会出现少八小时的情况
    在使用antd做form表单提交的时候,突然发现了一个很有意思的bug.就是在使用datepicker组件日期框的时候会出现提交后少一天的问题我在网上搜索了许多解决办法,也是困扰了我一天的时间,下面代码.letformData=JSON.parse(JSON.stringify(form.getFieldsValue()));......
  • ant-design 表单form label不换行
    <a-form:model="rightData"labelAlign="left"><a-form-itemlabel="标识名"><a-inputv-model:value="rightData.name":placeholder="input"/></a-form-item></a-form>......
  • electron+antdesignpro+react
    如果你想在Electron应用中使用AntDesignPro和React框架,可以按照以下步骤进行:创建一个基于React的AntDesignPro项目。将该项目作为渲染进程代码集成到Electron中。根据需要添加Electron的主进程代码。在第二步中,有两种方法将React和AntDesignPro项目......
  • Semantic Kernel 入门系列:
    如果把提示词也算作一种代码的话,那么语义技能所带来的将会是全新编程方式,自然语言编程。通常情况下一段prompt就可以构成一个SemanticFunction,如此这般简单,如果我们提前可以组织好一段段prompt的管理方式,甚至可以不需要写任何的代码,就可以构造出足够多的技能来。使用文件夹管......
  • “JUC锁”02之 互斥锁ReentrantLock
    本章对ReentrantLock包进行基本介绍,这一章主要对ReentrantLock进行概括性的介绍,内容包括:ReentrantLock介绍ReentrantLock函数列表ReentrantLock示例在后面的两章,会分别介绍ReentrantLock的两个子类(公平锁和非公平锁)的实现原理。转载请注明出处:http://www.cnblogs.com/skywang123......
  • ChatGPT垂直行业私有数据知识库向量数据库-Linux Ubuntu下安装docker-并且安装运行qdr
    现在基于GPT相应实现自建本地知识库,必不可少的就是向量数据库,现在介绍下qdrant向量数据库的安装。因为qdrant向量数据库只支持docker部署,所以在服务器上安装一下docker,下面是在ubutnu下安装dockeraptupdateaptinstallapt-transport-httpsca-certificatescurlgnupglsb-r......
  • g_variant_new G_TP_L G_TP_R
    g_variant_newG_TP_LG_TP_R g_variant_newG_TP_LG_TP_Rg_variant_newG_TP_LG_TP_R我觉得,首先你需要解构元组:GVariant*inner;g_variant_get(ret,"(v)",&inner);然后分解一个元组:guint32num;gbooleanb;g_variant_get(inner,"(ub)",&......
  • Semantic Kernel 入门系列:
    理解了LLM的作用之后,如何才能构造出与LLM相结合的应用程序呢?首先我们需要把LLMAI的能力和原生代码的能力区分开来,在SemanticKernel(以下简称SK),LLM的能力称为semanticfunction,代码的能力称为nativefunction,两者平等的称之为function(功能),一组功能构成一个技能(skill)。SK的基......
  • 初学ANTLR4(一)
    初次接触ANTLR,我是根据《antlr4权威指南》这本书进行了解,并将自己所学,所感总结与此。0.相关资料关于官网:ANTLR官网关于书籍:《ANTLR4权威指南》1.关于ANTLR4ANTLR是一款强大的语法分析器生成工具,用于读取,处理,执行和翻译结构化的文本或二进制文件。对于初学者的我,目前还不......