分组

[题目链接](https://www.nowcoder.com/practice/4ab6bd6f830b41078660cbde7b86800c)

思路

题意很直接:有 个同学,要把他们分成若干组,每组不少于 个人,问最多能分多少组。

要让组数尽可能多,每组的人数就应该尽可能少。既然下限是 ,那我们就让每组恰好 人,能分出 组。剩下的 个人(0、1 或 2 人)不够凑成一组,就并入已有的某个组即可。

所以答案就是 ,一行整除搞定。

代码

#include <iostream>
using namespace std;
int main() {
    long long n;
    cin >> n;
    cout << n / 3 << endl;
    return 0;
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        System.out.println(n / 3);
    }
}
n = int(input())
print(n // 3)
const n = parseInt(require("fs").readFileSync("/dev/stdin", "utf8").trim());
console.log(Math.floor(n / 3));

复杂度分析

  • 时间复杂度:,只需一次读入和一次整除运算。
  • 空间复杂度:,只用了一个变量。