鱼C论坛

 找回密码
 立即注册
查看: 3002|回复: 2

算法题目

[复制链接]
发表于 2017-9-5 17:58:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
# Randomly fills a grid of size height and width whose values are input by the user,
# with nonnegative integers randomly generated up to an upper bound N also input the user,
# and computes, for each n <= N, the number of paths consisting of all integers from 1 up to n
# that cannot be extended to n+1.
# Outputs the number of such paths, when at least one exists.


from random import seed, randint
import sys
from collections import defaultdict


def display_grid():
    for i in range(len(grid)):
        print('   ', ' '.join(str(grid[i][j]) for j in range(len(grid[0]))))

def get_paths():
    pass
# Replace pass above with your code

# Insert your code for other functions
   
try:
    for_seed, max_length, height, width = [int(i) for i in
                                                  input('Enter four nonnegative integers: ').split()
                                       ]
    if for_seed < 0 or max_length < 0 or height < 0 or width < 0:
        raise ValueError
except ValueError:
    print('Incorrect input, giving up.')
    sys.exit()

seed(for_seed)
grid = [[randint(0, max_length) for _ in range(width)] for _ in range(height)]
print('Here is the grid that has been generated:')
display_grid()
paths = get_paths()
if paths:
    for length in sorted(paths):
        print(f'The number of paths from 1 to {length} is: {paths[length]}')
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-9-5 17:59:33 | 显示全部楼层
  1. # Randomly fills a grid of size height and width whose values are input by the user,
  2. # with nonnegative integers randomly generated up to an upper bound N also input the user,
  3. # and computes, for each n <= N, the number of paths consisting of all integers from 1 up to n
  4. # that cannot be extended to n+1.
  5. # Outputs the number of such paths, when at least one exists.



  6. from random import seed, randint
  7. import sys
  8. from collections import defaultdict


  9. def display_grid():
  10.     for i in range(len(grid)):
  11.         print('   ', ' '.join(str(grid[i][j]) for j in range(len(grid[0]))))

  12. def get_paths():
  13.     pass
  14.     # Replace pass above with your code

  15. # Insert your code for other functions
  16.    
  17. try:
  18.     for_seed, max_length, height, width = [int(i) for i in
  19.                                                   input('Enter four nonnegative integers: ').split()
  20.                                        ]
  21.     if for_seed < 0 or max_length < 0 or height < 0 or width < 0:
  22.         raise ValueError
  23. except ValueError:
  24.     print('Incorrect input, giving up.')
  25.     sys.exit()

  26. seed(for_seed)
  27. grid = [[randint(0, max_length) for _ in range(width)] for _ in range(height)]
  28. print('Here is the grid that has been generated:')
  29. display_grid()
  30. paths = get_paths()
  31. if paths:
  32.     for length in sorted(paths):
  33.         print(f'The number of paths from 1 to {length} is: {paths[length]}')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-5 18:01:51 | 显示全部楼层

得出的要求是这样

本帖最后由 DavidHsin 于 2017-9-5 18:04 编辑

不知应该从何入手,请大神指点迷津
1504605690995.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-23 17:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表