Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โค้ด C++ นับจำนวนครั้งที่ให้หินได้

โจทย์: ให้หิน n ก้อน ให้หลายครั้ง แต่ติดกันไม่ให้จำนวนเท่ากัน หาจำนวนครั้งสูงสุดที่ให้ได้

Input: n=4 → Output: 3 (ให้ 1, 2, 1)

ขั้นตอนแก้

return (n * 2 + 1) / 3

ตัวอย่างโค้ด

#include <bits/stdc++.h>
using namespace std;
int solve(int n) {
    return (n * 2 + 1) / 3;
}
int main() {
    int n = 4;
    cout << solve(n) << endl;
}

Input

4

Output

3