coding 拿糖果

题目:N袋糖果,每次从所有非空袋子中分别取出x个,0 < x <= 当前非空袋中糖果数量
      要把糖果拿完,至少需要拿几次?

#include
#include
#include
using namespace std;

void solve2() {
	unordered_set used;
	int n;
	cin >> n;				//一共n个袋子
	for (int i = 0; i < n; ++i) {
		int num = 0;		//袋子中糖果数量
		cin >> num;
		used.insert(num);
	}
	int ans = used.size();

	if (used.find(0) != used.end()) {
		--ans;
	}
	cout << (ans>0 ? ans:0 )<< endl;

}


int main22() {
	solve2();
	return 0;
}

python版本:

def solve():
    num = input()
    mydic = dict()
    for i in range(int(num)):
        candy_number = input()
        mydic[candy_number] = 1
    
    return len(mydic)-1 if mydic.get('0')==None else len(mydic)


if __name__=='__main__':
    solve()

你可能感兴趣的:(Leetcode刷题,算法,c++,数据结构)