+ -
当前位置:首页 → 问答吧 → SPOJ 编程挑战题:情人节

SPOJ 编程挑战题:情人节

时间:2014-03-13

来源:互联网

明日系情人节,预祝大家开开心心、浪浪漫漫咁度过。

呢版系程式设计,当然要应下景。以下两题系我做过 SPOJ 有关情人节嘅编程挑战题:
1. http://www.spoj.com/problems/CRNVALEN/
2. http://www.spoj.com/problems/A_W_S_N/

第 1 题系 3 日前先至有,算系容易,比大家热一热身。第 2 题系旧年情人节出,有尐难度。如果明日冇约,亦都可以试解呢两题,唔洗咁闷。

作者: fitcat07   发布时间: 2014-03-13

你无约呀?????
引用:原帖由 fitcat07 於 2014-2-13 18:42 发表
明日系情人节,预祝大家开开心心、浪浪漫漫咁度过。

呢版系程式设计,当然要应下景。以下两题系我做过 SPOJ 有关情人节嘅编程挑战题:
1. http://www.spoj.com/problems/CRNVALEN/
2. http:/ ...

作者: a8d7e8   发布时间: 2014-03-13

跟c++谈情?

作者: Susan﹏汪汪   发布时间: 2014-03-13

引用:原帖由 a8d7e8 於 2014-2-13 06:58 PM 发表
你无约呀?????

我有约呀!
就算无约都唔会重做,已解决...

作者: fitcat07   发布时间: 2014-03-13

引用:原帖由 Susan﹏汪汪 於 2014-2-13 07:07 PM 发表
跟c++谈情?



汪汪你好专一呵...
呢个世界唔止 C++,重有好多好男人,呀,应该系好多好编程语言至啱...

作者: fitcat07   发布时间: 2014-03-13

唔知有呒理解错误,
复制内容到剪贴板代码:import unittest

class Confession(object):
def __init__(self, total_girls, confession):
self.total_girls = total_girls
self.confession = confession


def is_valid(self):
if len(self.confession) != self.total_girls:
return False

total_different_nunbers = len(set(self.confession))
if self.confession[::-1] == self.confession and total_different_nunbers == 1 and self.confession[0] == self.total_girls - 1:
return True

if total_different_nunbers != 2 :
return False

numbers = sorted(list(set(self.confession)))
print (numbers)
#if numbers[1] - numbers[0] == 1 : #and sum([1 for numbers[1] in numbers]) >= sum([1 for numbers[0] in numbers]):
if numbers[1] - numbers[0] == 1 and sum([1 for i in self.confession if i == 0]) < 2:
return True

return False

def find_double_datings(self):
if self.is_valid() :
if len(set(self.confession)) == 1:
return self.total_girls


numbers = sorted(list(set(self.confession)))

print (sum([1 for i in self.confession if i == numbers[0]]))
return sum([1 for i in self.confession if i == numbers[0]])


return -1


class TestCase(unittest.TestCase):

def test_1(self):
confession = Confession(3, [2, 2, 2])
self.assertEqual(confession.is_valid(), True)

def test_2(self):
confession = Confession(3, [1, 1, 0])
self.assertEqual(confession.is_valid(), True)

def test_3(self):
confession = Confession(3, [1, 0, 0])
self.assertEqual(confession.is_valid(), False)

def test_4(self):
confession = Confession(4, [1, 1, 1, 0])
self.assertEqual(confession.is_valid(), True)

def test_5(self):
confession = Confession(3, [2, 2, 2])
self.assertEqual(confession.find_double_datings(), 3)

def test_6(self):
confession = Confession(4, [3, 3, 3, 3])
self.assertEqual(confession.find_double_datings(), 4)

def test_6(self):
confession = Confession(4, [3, 2, 2, 2])
self.assertEqual(confession.is_valid(), True)

def test_7(self):
confession = Confession(4, [1, 1, 1, 0])
self.assertEqual(confession.find_double_datings(), 1)

def test_8(self):
confession = Confession(4, [3, 2, 2, 2])
self.assertEqual(confession.find_double_datings(), 3)


def test_9(self):
confession = Confession(4, [2, 2, 1, 1])
self.assertEqual(confession.find_double_datings(), 2)


def test_10(self):
confession = Confession(3, [1, 0, 0])
self.assertEqual(confession.find_double_datings(), -1)


def main():
unittest.main()

if __name__ == "__main__":
main()

作者: form5   发布时间: 2014-03-13

算法有问题,试试以下测试案例:
复制内容到剪贴板代码:3
0 0 0

5
1 1 1 1 2

作者: fitcat07   发布时间: 2014-03-13

引用:原帖由 fitcat07 於 2014-2-14 12:08 PM 发表
算法有问题,试试以下测试案例:3
0 0 0

5
1 1 1 1 2
系喔,thanks

作者: form5   发布时间: 2014-03-13

i just make a subtle change to the way that few tests is passed
复制内容到剪贴板代码:import unittest

class Confession(object):
def __init__(self, total_girls, confession):
self.total_girls = total_girls
self.confession = confession

def get_double_datings(self):
if len(self.confession) != self.total_girls:
return -1

if 0 in set(self.confession) and len(set(self.confession)) == 1:
return 0

sorted_list = sorted(self.confession)
smallest_number = sorted_list[0]

for i in sorted_list[:smallest_number+1]:
if smallest_number != i:
return - 1

for i in sorted_list[smallest_number+1:]:
if smallest_number + 1 != i:
return - 1

return smallest_number + 1


class TestCase(unittest.TestCase):

def test_1(self):
confession = Confession(3, [2, 2, 2])
self.assertEqual(confession.get_double_datings(), 3)

def test_2(self):
confession = Confession(2, [1, 0])
self.assertEqual(confession.get_double_datings(), 1)

def test_3(self):
confession = Confession(3, [1, 1, 0])
self.assertEqual(confession.get_double_datings(), 1)

def test_4(self):
confession = Confession(3, [2, 1, 0])
self.assertEqual(confession.get_double_datings(), -1)

def test_5(self):
confession = Confession(3, [1, 0, 0])
self.assertEqual(confession.get_double_datings(), -1)

def test_6(self):
confession = Confession(4, [1, 1, 1, 0])
self.assertEqual(confession.get_double_datings(), 1)

def test_7(self):
confession = Confession(4, [3, 3, 3, 3])
self.assertEqual(confession.get_double_datings(), 4)

def test_9(self):
confession = Confession(4, [2, 2, 1, 1])
self.assertEqual(confession.get_double_datings(), 2)

def test_10(self):
confession = Confession(3, [1, 0, 0])
self.assertEqual(confession.get_double_datings(), -1)

def test_11(self):
confession = Confession(3, [0, 0, 0])
self.assertEqual(confession.get_double_datings(), 0)

def test_12(self):
confession = Confession(5, [1, 1, 1, 1, 2])
self.assertEqual(confession.get_double_datings(), -1)

def test_13(self):
confession = Confession(5, [1, 1, 1, 1])
self.assertEqual(confession.get_double_datings(), -1)

def test_14(self):
confession = Confession(6, [4, 4, 3, 3, 3, 3])
self.assertEqual(confession.get_double_datings(), 4)

def test_14(self):
confession = Confession(6, [5, 4, 3, 3, 3, 3])
self.assertEqual(confession.get_double_datings(), -1)

def main():
unittest.main()

if __name__ == "__main__":
main()

作者: form5   发布时间: 2014-03-13

You're very close. Try this:
复制内容到剪贴板代码:3
3 3 3

作者: fitcat07   发布时间: 2014-03-13

引用:原帖由 fitcat07 於 2014-2-16 01:56 PM 发表
You're very close. Try this:3
3 3 3

终於submit上去 过左ac
复制内容到剪贴板代码:import sys
class Confession(object): def __init__(self, total_girls, confession): self.total_girls = total_girls self.confession = confession
def get_double_datings(self): if len(self.confession) != self.total_girls: return -1
if 0 in set(self.confession) and len(set(self.confession)) == 1: return 0
if self.total_girls in set(self.confession) and len(set(self.confession)) == 1: return -1
sorted_list = sorted(self.confession) smallest_number = sorted_list[0]
for i in sorted_list[:smallest_number+1]: if smallest_number != i: return - 1
for i in sorted_list[smallest_number+1:]: if smallest_number + 1 != i: return - 1
return smallest_number + 1
...

作者: form5   发布时间: 2014-03-13

引用:原帖由 fitcat07 於 2014-2-16 01:56 PM 发表
You're very close. Try this:3
3 3 3

终於submit上去 过左ac



复制内容到剪贴板代码:import sys

class Confession(object):
def __init__(self, total_girls, confession):
self.total_girls = total_girls
self.confession = confession

def get_double_datings(self):
if len(self.confession) != self.total_girls:
return -1

if 0 in set(self.confession) and len(set(self.confession)) == 1:
return 0

if self.total_girls in set(self.confession) and len(set(self.confession)) == 1:
return -1

sorted_list = sorted(self.confession)
smallest_number = sorted_list[0]

for i in sorted_list[:smallest_number+1]:
if smallest_number != i:
return - 1

for i in sorted_list[smallest_number+1:]:
if smallest_number + 1 != i:
return - 1

return smallest_number + 1

...


[ 本帖最后由 form5 於 2014-2-16 08:08 PM 编辑 ]

作者: form5   发布时间: 2014-03-13

做得好

作者: fitcat07   发布时间: 2014-03-13