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
明日系情人节,预祝大家开开心心、浪浪漫漫咁度过。

呢版系程式设计,当然要应下景。以下两题系我做过 SPOJ 有关情人节嘅编程挑战题:
1. http://www.spoj.com/problems/CRNVALEN/
2. http:/ ...
作者: a8d7e8 发布时间: 2014-03-13

作者: Susan﹏汪汪 发布时间: 2014-03-13
你无约呀?????

就算无约都唔会重做,已解决...

作者: fitcat07 发布时间: 2014-03-13
跟c++谈情?


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

作者: fitcat07 发布时间: 2014-03-13
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
0 0 0
5
1 1 1 1 2
作者: fitcat07 发布时间: 2014-03-13
算法有问题,试试以下测试案例:3
0 0 0
5
1 1 1 1 2
作者: form5 发布时间: 2014-03-13
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
3 3 3
作者: fitcat07 发布时间: 2014-03-13
You're very close. Try this:3
3 3 3

终於submit上去 过左ac
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
You're very close. Try this:3
3 3 3
终於submit上去 过左ac
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
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28