title: 华为软件笔试---汽水瓶编程
localimage: image1
urlname: huawei-qishuiping
categories:
summary
tags: [writen, exam]
date: 2019-9-18 14:57:00
---

摘要

更多文章请移步我的博客网站: Porter.Pan Blog

题目:

有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?

思路及解决

输入描述:

输出描述:

输入例子1:

3
10
81
0

输出例子1:

1
5
40

python 代码实现

'''
有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,
方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后
你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多
少瓶汽水喝?
'''

import sys


def data_capture():
    data = []
    while True:
        # print("请输入数据:")
        line = sys.stdin.readline().strip()
        if line == '0' or not line:
            # print("输入的数据为:", data)
            break
        else:
            # tmp = list(map(int, line.split(" ")))
            tmp = int(line)
        data.append(tmp)
    return data


def get_result(data_in):
    data_he = 0
    data_kong = 0
    while True:
        data_he = data_he + data_in//3
        data_kong = data_in % 3 + data_in // 3
        data_in = data_in // 3 + data_in % 3

        if data_in < 2:
            break
        elif data_in == 2:
            data_he = data_he + 1
            break
    # print("可以喝: %d , 留有空瓶:%d" % (data_he, data_kong))
    print(data_he)
    return data_he


if __name__ == "__main__":
    get_data = []
    data_get = data_capture()
    for k in data_get:
        get_value = get_result(k)
        get_data.append(get_value)
01-13 13:57