国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

hdu1030【找規律】

2019-11-14 09:09:24
字體:
來源:轉載
供稿:網友

A triangle field is numbered with successive integers in the way shown on the picture below.

這里寫圖片描述

The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only, he can not travel from cell to cell through vertices. The number of edges the traveller passes makes the length of the traveller’s route.

Write the PRogram to determine the length of the shortest route connecting cells with numbers N and M.

Input Input contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).

Output Output should contain the length of the shortest route.

Sample Input 6 12

Sample Output 3

題解:找規律題,把整個三角形轉化成一個三維的坐標軸 這里寫圖片描述 例:對于6,在第三行,z=3, x=(9-6)/2+1=2, y=(6-4-1)/2+1=1,所以(2,1,3); 對于12,在第4行,z=4, x=(16-12)/2+1=3,y=(12-9-1)/2+1=2,所以(3,2,4); 兩個點的最短距離=x、y、z坐標差的絕對值

代碼:

#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <map>#define MST(s,q) memset(s,q,sizeof(s))#define INF 0x3f3f3f3f#define MAXN 1005using namespace std;int x[1000005];int main(){ int n, m; while (cin >> n >> m) { int Za = (int)ceil(sqrt(n * 1.0)); int Zb = (int)ceil(sqrt(m * 1.0)); int Xa = (Za * Za - n) / 2 + 1; int Xb = (Zb * Zb - m) / 2 + 1; int Ya = (n - (Za - 1) * (Za - 1) - 1) / 2 + 1; int Yb = (m - (Zb - 1) * (Zb - 1) - 1) / 2 + 1; int ans = (int)( fabs((Za - Zb) * 1.0) + fabs((Xa - Xb) * 1.0) + fabs((Ya - Yb) * 1.0) ); printf("%d/n", ans ); }}
上一篇:VC++簡單窗口

下一篇:[Java]網絡編程

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白银市| 伽师县| 香河县| 来宾市| 寿阳县| 若尔盖县| 黎川县| 达尔| 天台县| 咸丰县| 邵武市| 海阳市| 周口市| 甘孜县| 新邵县| 繁昌县| 绥棱县| 伽师县| 铁岭市| 台北县| 灌南县| 西乡县| 扎兰屯市| 荣昌县| 斗六市| 奇台县| 报价| 长治市| 青阳县| 修武县| 枣庄市| 嘉鱼县| 日照市| 台前县| 凌源市| 资溪县| 蕲春县| 沅陵县| 岳阳市| 义马市| 凉城县|