yt99 题解分享 · 2024/4/11
饮料换购(编程题) - 题解
``` #include <bits/stdc++.h> typedef long long LL; using namespace std; int main() { int n,res=0; cin>>n; res=n; while(n>=3) { n-=2;//换掉3个瓶盖,得到一个瓶盖,相当于花了两个瓶盖 //能得到的饮料数(答案)加一 res++; } cout<<res<<endl; return 0; } ```
查看全文
0 0 1 1
didhv 题解分享 · 2024/4/4
饮料换购(编程题) - 题解
``` import java.util.*; public class Main { /* 初始瓶盖数 = 初始购买的饮料数 换来的饮料数 = 初始瓶盖数 / 3 剩余瓶盖数 = 初始瓶盖数 % 3 + 换来的饮料数 */ public static int calculateTotalDrinks(int n) { int totalDrinks = n; // 初始购买的饮料数 int bottleCaps = n; // 初始瓶盖数 while (bottleCaps >= 3) { // 换来的饮料数 int newDrinks = bottleCaps / 3; totalDrinks += newDrinks; // 更新总饮料数 // 剩余瓶盖数 bottleCaps = bottleCaps % 3 + newDrinks; } return totalDrinks; } public static void main(String[] args) { int n = new Scanner(System.in).nextInt(); int totalDrinks = calculateTotalDrinks(n); System.out.println(totalDrinks); } } ```
查看全文
0 0 0 7
kaisyuantseng 题解分享 · 2024/4/11
饮料换购(编程题) - 题解
纯模拟 ```cpp #include <bits/stdc++.h> using namespace std; #define int long long // #define LL long long #define endl '\n' int n; void solve() { cin >> n; int drink = 0; int cnt = 0; while (n > 0) { n--, drink++; // 每开一瓶,就喝下一瓶 cnt++; // 总共喝的数量+1 if (drink == 3) // 喝了三瓶 { n++; // 三个瓶盖换一瓶 drink = 0; } } cout << cnt << endl; } signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); solve(); return 0; } ```
查看全文
0 0 0 1
yt99 题解分享 · 2024/4/11
饮料换购(编程题) - 题解
``` #include <bits/stdc++.h> typedef long long LL; using namespace std; int main() { int n,res=0; cin>>n; res=n; while(n>=3) { res+=(n/3); n=n/3+(n%3); } cout<<res<<endl; return 0; } ```
0 0 0 1
2835778687 题解分享 · 2024/4/6
饮料换购(编程题) - 题解
``` #include<iostream> using namespace std; int main() { int n; cin>>n; int res=n; while(n>=3) { res+=n/3; n=n/3+n%3; } cout<<res; return 0; } ```
0 0 0 1