FtSan


私信TA

用户名:ft1075576

访问量:56889

签 名:

等  级
排  名 120
经  验 7583
参赛次数 0
文章发表 24
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:

每次换购的瓶子a = bottle / 3


每次换购以后剩下的空瓶数为b

b= bottle / 3 + bottle % 3


以上两部进行循环

对a进行累加计数

用b来循环控制,并对b==2时候对累加器再加1




参考代码:

#include<stdio.h>
int bottle_count(int bottle);

int main()
{
      int bottle, exchange_bottle[10],i = 0;
      scanf("%d",&bottle);
      while(bottle != 0)
      {
              exchange_bottle[i++] = bottle_count(bottle);
              scanf("%d",&bottle);
      }

      for(int j = 0 ; j < i  ; j ++)
      {
               printf("%d\n",exchange_bottle[j]);	
      }
      return 0;
}

int bottle_count(int bottle)
{
	int havedrink = 0 , nowdrink;
	while(bottle > 1)
	{
		nowdrink = bottle / 3; //每次可以换购的数量
		bottle = nowdrink + bottle % 3; //喝完后余下的空瓶
		havedrink += nowdrink; //对换购的进行累加
		if(bottle == 2) //如果还剩下两瓶再向店家要一瓶后喝完抵一瓶
		{
			havedrink++;
			break;
		}
	}	
	return  havedrink;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

666
2017-10-05 17:48:44
  • «
  • 1
  • »