栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

牛客网-汽水瓶问题

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

牛客网-汽水瓶问题

问题描述:

问题解析:
问题的本质就是求输入数据/2。
编程问题(C++语言或C语言):

1.多输入数据怎么读取?
2.多输出数据怎么显示?
C++语言:

#include 
#include
#include
#include
using namespace std;
int main() {
	int n;
	while (cin >> n && n != 0 && n >= 1 && n <= 100) {
		int a = n / 2;
		cout << a << endl;
	}
	return 0;

}

C语言:

#include 
#include
#include
#include
using namespace std;

int main(void)
{
	int i, n;
	int j = 0;
	int a[10] = { 0 };
	while (scanf_s("%d", &n) != EOF && n != 0)
	{
		a[j++] = n;
	}
	for (i = 0; i < 10&&a[i]!=0; i++)
		printf("%dn", a[i]/2);
	return 0;

python语言:
方法1:

import sys
pyin = [int(i) for i in sys.stdin.read().split()]
for n in pyin:
    z_num = 0
    if n == 0:
        exit();
    if n > 100 and n < 0:
        exit();
    kong = n
    while kong // 3:
        yu = kong % 3
        zheng = kong // 3
        z_num += zheng
        kong = yu + zheng
    if kong % 3 == 2:
        z_num += 1
    print(z_num)

方法2:

import sys
inpy = [int(i) for i in sys.stdin.read().split()]
for i in inpy:
    if i == 0:
        break
    print(i // 2)

方法3:

int getValid(int number)
{
    int x=0;
    int y=0;
    int rtn=0;

    if(number == 1)
    {
        return 0;
    }
    else if(number == 2)
    {
        return 1;
    }
    else
    {
        x=number%3;
        y=number/3;
        rtn = y;
        rtn += getValid(x+y);
        return rtn;
    }
}

void main (void)
{
    int num,result=0;
    while(scanf("%d",&num)&&(num!=0))
    {
        result = getValid(num);
        printf("%dn",result); 
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/457659.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号