code forces Hopscotch (找规律)
来源:互联网 发布:java 泛型编程 pdf 编辑:程序博客网 时间:2024/06/05 15:07
So nearly half of the winter is over and Maria is dreaming about summer. She's fed up with skates and sleds, she was dreaming about Hopscotch all night long. It's a very popular children's game. The game field, the court, looks as is shown in the figure (all blocks are square and are numbered from bottom to top, blocks in the same row are numbered from left to right). Let us describe the hopscotch with numbers that denote the number of squares in the row, staring from the lowest one: 1-1-2-1-2-1-2-(1-2)..., where then the period is repeated (1-2).
The coordinate system is defined as shown in the figure. Side of all the squares are equal and have length a.
Maria is a very smart and clever girl, and she is concerned with quite serious issues: if she throws a stone into a point with coordinates (x, y), then will she hit some square? If the answer is positive, you are also required to determine the number of the square.
It is believed that the stone has fallen into the square if it is located strictly inside it. In other words a stone that has fallen on the square border is not considered a to hit a square.
The only input line contains three integers: a, x, y, where a (1 ≤ a ≤ 100) is the side of the square, x and y ( - 106 ≤ x ≤ 106, 0 ≤ y ≤ 106) are coordinates of the stone.
Print the number of the square, inside which the stone fell. If the stone is on a border of some stone or outside the court, print "-1" without the quotes.
1 0 0
-1
3 1 1
1
3 0 10
5
3 0 7
-1
3 4 0
-1
【题解】
就是思维灵活点 找规律,除了第一个,其他都是一个模型,我的做法是先打表,找出层数和每一层和该层第一个数之间的关系,然后判断只有一个的层,这个只要x坐标在a/2范围内,直接用纵坐标y/a,向上取整,就是该层的第一个数,输出即可,然后是判断一层两个方块的,可以发现,只要是一层两块的,它的第一个数都是3的倍数,所以y/a向上取整后的数如果能被3整除,就表示该层有两个方块,这时候再判断一下横坐标x是不是0,0的话输出-1,大于零的话就再加一(因为一层有两个的话,右边的数比左边的数大1),输出即可。这样就解出来了。
【AC代码】
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespace std;int a[1000005];float m,n,k;void f(){ a[1] = 1; int k=0; for(int i=2;i<=1000003;i+=2) { a[i]=i+k; a[i+1]=i+k+1; k++; }}int main(){ f(); while(~scanf("%f%f%f",&k,&m,&n)) { int tag = 0; if(fabs(m)<k/2) { if(ceil(n/k) != (int)n/k){ int x = ceil(n/k); if(x==1 || x%2==0){ int y = a[x]; if(y%3==0 && m>0) y++; printf("%d\n",y); tag = 1; } else if(x%2==1&&m!=0){ int y = a[x]; if(y%3==0 && m>0) y++; printf("%d\n",y); tag = 1; } } } else if(fabs(m)<k&&m!=0) { if(ceil(n/k) != n/k){ int x = ceil(n/k); int y = a[x]; if(y%3==0) { if(m>0) y++; printf("%d\n",y); tag=1; } } } if(!tag) printf("-1\n"); } return 0;}
- code forces Hopscotch (找规律)
- code forces 443 A.Borya's Diagnosis(找规律)
- leetcode:Gray Code (找规律)
- Digital Root----code forces
- Code Forces #3
- Code Forces #4 D
- Code Forces #5C
- code forces K Indivisibility
- Code forces 612C
- Code forces 699B
- code forces 1A
- code forces 377A
- code forces 505A
- code forces Harmony Analysis
- Code Forces 587A
- Code Forces 588A
- [code forces] 762A
- code forces Amusing Joke
- PHP文件上传:\$_FILES数组及$_FILES['userfile']['error']中返回的错误代码
- 代码重构实践
- Slim研读笔记一之如何读框架
- 程序跳过了validation.xml 直接执行后面的execute();求解????
- 如何充分利用多核CPU,计算很大的List中所有整数的和
- code forces Hopscotch (找规律)
- Java 继承
- C#高级部分
- git删除master分支后重建
- 【我的Java笔记】IO流_使用IO流进行文件的复制和粘贴
- 构建DevOps落地的自动化持续交付流水线的工具链
- http协议_Cookie 和 Session
- 第三篇:spring webflow
- 算法提高 幸运顾客