跳到正文

个人记录

6.7二分答案3---姜嘉懿

Jaxent约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

课堂练习

P1150 Peter 的烟

code:

C++
#include<bits/stdc++.h>
using namespace std;
int n,k,d,ans;
int main(){
	cin>>n>>k;
	while(n!=0){//由于1次1根太慢了,所以1次n根
		ans+=n;//抽n根
		d+=n;//烟蒂++
		n=d/k;//下次抽d/k根
		d%=k;//烟蒂取余
	}
	cout<<ans;//输出
	return 0;
}

T616649 吃冰棍

code:

C++
#include<bits/stdc++.h>
using namespace std;
int s,n,b,ans;
int main(){
	cin>>s;
	for(int i=s/2;i<=s;i++){
	//这里的i=s/2是因为分析后发现结果大于s/2,所以优化一下
		ans=0;
		n=i;
		b=0;
		//初始化
		while(n!=0){//一次吃三根冰棍,具体思路P1150一致
			ans+=n;
			b+=n;
			n=b/3;
			b%=3;
		}
		if(ans>=s){
			cout<<i;//输出
			return 0;
		}
	}
	return 0;
}
原文