因为卡片1先用完,所以只要计算卡片1的数量即可。
#include<bits/stdc++.h>
using namespace std;
int sum(int num){
int count = 0;
while(num){
if(num%10==1)
count++;
num/=10;
}
return count;
}
int main(){
int number = 2021;
int i=1;
while(true){
int count = sum(i);
if(number<count) //如果剩余的卡片不足拼出当前的数字则退出循环
break;
number -= count;
i++;
}
cout<<i--; //因为卡片已经不足拼出当前数字,所以需要减去1
}