Gridland(规律)
Gridland
Time Limit: 2 Seconds Memory Limit: 65536 KB
Background
For years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are the "easy" problems like sorting, evaluating a polynomial or finding the shortest path in a graph. For the "hard" ones only exponential-time algorithms are known. The traveling-salesman problem belongs to this latter group. Given a set of N towns and roads between these towns, the problem is to compute the shortest path allowing a salesman to visit each of the towns once and only once and return to the starting point.
Problem
The president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwest, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North-South or East-West is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 * 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 * 3-Gridland, the shortest tour has length 6.
Figure 7: A traveling-salesman tour in 2 * 3-Gridland.
Input
The first line contains the number of scenarios.
For each scenario, the grid dimensions m and n will be given as two integer numbers in a single line, separated by a single blank, satisfying 1 < m < 50 and 1 < n < 50.
Output The output for each scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. In the next line, print the length of the shortest traveling-salesman tour rounded to two decimal digits. The output for every scenario ends with a blank line.
Sample Input
2 2 2 2 3
Sample Output
Scenario #1: 4.00
Scenario #2: 6.00
题意:给你一个网格,让从一个点出发,访问所有的城市回到起点,规律是如果两个数都是奇数要加0.41;即:a*b-1+sqrt(2.0);否则肯定可以回到起点,直接是a*b;
c代码:
#include<iostream>
#include<algorithm>
using namespace std;
#include<cstdio>
#include<cmath>
int main(){
int T,kase=0;
cin>>T;
while(T--){
int a,b;
double ans;
cin>>a>>b;
if(a&1&&b&1)ans=a*b+0.41;
else ans=(double)a*b;
printf("Scenario #%d:\n%.2lf\n",++kase,ans);
puts("");
}
return 0;
}
python:
import sys an=sys.stdin.readline()
t=an.split();
n=int(t[0])
k=0
while n:
n-=1
k+=1
an=sys.stdin.readline()
t=an.split();
a=int(t[0])
b=int(t[1])
if (a%2) and (b%2):
sum=a*b+0.41
else:
sum=a*b
print('Scenario #%d:'%k)
print("%.2f\n"%sum)
#真心无奈了,各种出问题,最终发现python的对齐方式这么重要。。。
__author__ = 'cuijunyong' import math
T = int(raw_input(), 10);
i = 1
while i <= T:
a = raw_input().split();
print "Scenario #%d:" %i;
if((int(a[0]) & 1 == 1) and (int(a[1]) & 1 == 1)):
print "%.2f" %(round((float(a[0])*float(a[1]) + math.sqrt(2) - 1), 2));
else:
print "%.2f" %(round((float(a[0])*float(a[1])), 2));
i += 1;
print ;
Gridland(规律)的更多相关文章
- zju 1037 Gridland(找规律,水题)
题目链接 多写几个案例,根据数据的奇偶性,就能找到规律了 #include<stdio.h> int main() { int t,n,m; double ans; scanf(" ...
- HDU ACM 1046 Gridland 找规律
分析:给出一个矩阵.问最短从一个点经过全部点以此回到起点的长度是多少.绘图非常好理解.先画3*4.3*3.4*4的点阵图案.试着在上面用最短路走一走,能够发现当矩形点阵的长宽都是奇数时,最短路中必然有 ...
- HDU1046:Gridland
Problem Description For years, computer scientists have been trying to find efficient solutions to d ...
- TJU Problem 1015 Gridland
最重要的是找规律. 下面是引用 http://blog.sina.com.cn/s/blog_4dc813b20100snyv.html 的讲解: 做这题时,千万不要被那个图给吓着了,其实这题就是道简 ...
- hdu1452 Happy 2004(规律+因子和+积性函数)
Happy 2004 题意:s为2004^x的因子和,求s%29. (题于文末) 知识点: 素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en 因子 ...
- Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)
传送门 Description Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems ...
- ACM/ICPC 之 DP解有规律的最短路问题(POJ3377)
//POJ3377 //DP解法-解有规律的最短路问题 //Time:1157Ms Memory:12440K #include<iostream> #include<cstring ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
- 在sqlserver中做fibonacci(斐波那契)规律运算
--利用sqlserver来运算斐波那契规律 --利用事物与存储过程 declare @number intdeclare @A intdeclare @B intdeclare @C int set ...
随机推荐
- Python学习之编写登陆接口(Day1,作业一)
作业一:编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定(下次登陆还是锁定) 知识点:while循环,for循环,文件操作,if判断,列表操作 思路: 1.登陆,三次登陆失败,锁定 ...
- C# 单向链表数据结构 (一)
单向链表数据结构是有节点组成,每个节点包含两部分,第一部分为存储数据,第二部分为指向下一个节点的指针.注意,有两个特色的节点,分别为“头节点”和“尾节点”,头节点本身没有数据,只存储下一个节点的指针, ...
- GDOI模拟赛Round 1
GDOI模拟赛Round 1 数据结构 题目描述:给出一个长度为\(n\)的序列,支持两种操作: 1.对某段区间都加上一个数 2.给出\(p.k\),求下面表示式对\((10^9+7)\)取模 \[\ ...
- nodejs学习笔记-1
nodejs入门-安装 nodejs是什么,刚接触了一段时间,我自己也说不清楚它.按我个人的简单理解,nodejs就是一个javascript的解析器,它让javascript不在局限于浏览器客户端. ...
- ecshop后台添加菜单项,权限问题
ecshop后台自定义菜单涉及到几个重要的权限控制的文件,先做如下总结: 后台添加菜单项,并设置权限的步骤.:(以在系统模块添加申请友链菜单为例)commn.php : \language ...
- Yii的场景
先上代码 class User extends CActiveRecord{ public function rules() { return array( ...
- C++ 矩阵乘法
//构造矩阵类,重载乘法操作符 //作者:nuaazdh //时间:2011年12月1日 #include <iostream> using namespace std; //Matrix ...
- jQuery实现拖动布局并将排序结果保存到数据库
很多网站的拖动布局的例子都是采用浏览器的COOKIE来记录用户拖动模块的位置,也就是说拖动后各模块的排序位置信息是记录在客户端的cookie里的.当用户清空客户端的cookie或浏览器的cookie过 ...
- Velocity引擎导致jvm内存外内存泄露
我公司一兄弟,在controller层,每次调用controller的时候都创建了velocity引擎,而且没有去关闭,最终导致的现象就是jvm的内存信息正常,但是jvm之外的内存发生了泄露,导致是用 ...
- C#根据汉字生成拼音首字母全称
static void Main(string[] args) { string s = GetChineseSpell("周杰伦"); Console.WriteLine(s.T ...