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 ...
随机推荐
- UIButton基础知识
基本属性 1.frame;坐标:title:titlecolor:字体颜色:titleShadowColor:字体阴影:image:图片: backgroundImage:背景图片: 2.forsta ...
- 客户端数据持久化解决方案: localStorage
客户端数据持久化解决方案: localStorage localStorage主要用来替代cookie,解决cookie读写困难.容量有限的问题. localStorage有以下几个特点 localS ...
- 读懂系统负载(Load Avg)的含义 | Devops
有过运维Linux服务器的选手,想必对于系统平均负载(load averages)参数不会陌生吧,我们可以通过top, htop, uptime这些命令找到它们(如下图),那么我们又改如何理解它们呢, ...
- 《windows核心编程系列》二谈谈ANSI和Unicode字符集 .
http://blog.csdn.net/ithzhang/article/details/7916732转载请注明出处!! 第二章:字符和字符串处理 使用vc编程时项目-->属性-->常 ...
- A Guide to the Multiboot Process
A Guide to the Multiboot Process The XP and Vista boot process in general.The Windows dual and multi ...
- [转载自 文顶顶]iOS开发UI篇—程序启动原理和UIApplication
一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序. (2)每一个应用都有自己的UIApplica ...
- api(一) 创建窗口 (转)
所有的Windows SDK编程都有一个类似的框架,本文就说说这个框架,Windows程序设计的框架分为“三部曲”: 一.注册窗口类 注册窗口类的API函数是RegisterClass或者Regist ...
- http request parameter
http request parameter add htmlspecialchars host?vendor_id=1000000&q=Some%20children%20wish%20to ...
- Filter - Surge.js模板引擎过滤器
版权所有,转载请注明出处:http://guangboo.org/2014/01/05/filter-surgejs-template-engine 过滤器在surge.js模板引擎中多处用到,其类似 ...
- Python封装的访问MySQL数据库的类及DEMO
# Filename:mysql_class.py # Author:Rain.Zen; Date: 2014-04-15 import MySQLdb class MyDb: '''初始化[类似于构 ...