Practice Round China New Grad Test 2014

google一直是我梦想的公司,发现google对于算法的重视程度,随决定从code jam练手,同时打好算法基本功。

这次的练习共三道题目,第一题需要些对于二分图的了解,第二题和三题比较简单,基本都是拿来练习编程的,最近在学习python,所以都是基于python自己实现的。

属于菜鸟级别,代码写得比较基础,希望大家批评指正。

三道题的代码如下:

Problem A. Bad Horse

#-*- coding: UTF-8 -*- 
import string

def bfs(graph,n):
    ''' 染色法判定图是否为二分图,广序遍历图中所有节点,对节点染色,出现相邻节点同色,则返回0,否则返回1'''
    queue = []
    flag = []
    for i in range(n):
        flag.append(-1)
    for i in range(n):
        if flag[i] != -1:
            continue
        flag[i]=1
        queue.append(i)
        while len(queue)>0:
            k= queue.pop(0)
            for j in range(n):
                if(graph[k][j]&(flag[k]==flag[j])):
                    return 0
                if(graph[k][j]&(flag[j]==-1)):
                    queue.append(j)
                    flag[j]=1-flag[k]
    return 1
f=open('A-small-practice-1.in')
fw=open('A-small-practice-1.out','w')
caseNum=string.atoi(f.readline())   
#测试caseNum个case
for i in range(caseNum): 
    horseList = set()
    pairList = []
    graph = []
    pairNum = string.atoi(f.readline())
    #获取初始值
    for num in range(pairNum):
        pair = f.readline().strip().split()
        horseList = horseList|set(pair)
        pairList.append(pair)
    #将初始值用邻接矩阵表示,有冲突的对之间用1,其他值为0
    horseList = list(horseList)
    for a in range(len(horseList)):
        graph.append([])
        for b in range(len(horseList)):
            graph[a].append(0)
    for pair in pairList:
        a=horseList.index(pair[0])
        b=horseList.index(pair[1])
        graph[a][b],graph[b][a]=1,1
    if bfs(graph,len(horseList)):
        fw.write('Case   #{}: Yes\n'.format(i+1))
    else:
        fw.write('Case   #{}: No\n'.format(i+1))
    
f.close()
fw.close()

Problem B. Captain Hammer

#-*- coding: UTF-8 -*- 

import string
from math import asin
from math import cos,sin
from math import pi
f=open('B-small-practice.in')
fw=open('B-small-practice.out','w')
caseNum=string.atoi(f.readline())  
g=9.8
for i in range(caseNum):
    tempList=f.readline().split()
    v=string.atoi(tempList[0])
    d=string.atoi(tempList[1])
    o=0
    sin2o=0
    sin2o=g*d/(v*v)
    
    if sin2o<1.0:   #the value should be less than 1
        o=asin(sin2o)/pi*90
    else:
        o=45.0
    
    fw.write("Case #{0}: {1:.7f}\n".format(i+1,o))
f.close()
fw.close()

Problem C.  Moist

#-*- coding: UTF-8 -*- 

import string
f=open('C-small-practice-2.in')
fw=open('C-small-practice-2.out','w')
caseNum=string.atoi(f.readline())   

#add a new card to the sorted card list
def tradeCards(list,cardPos):
    list.append(cardPos)
    return sorted(list)

for i in range(caseNum):
    cardNum=string.atoi(f.readline())
    listCards,listSorted=[],[]
    count=0
    listCards.append(f.readline().strip())
    for j in range(1,cardNum):
        card = f.readline().strip()
        if card < listCards[len(listCards)-1]:
            count +=1
            listCards = tradeCards(listCards,card)
        else:
            listCards.append(card)

    fw.write("Case #{0}: {1}\n".format(i+1,count))
f.close()
fw.close()

代码使用两组小数据都correct。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值