卡片— 蓝桥杯(简单)

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
    public static void main(String[] args) {
        int i=2021;    //0-9的数字各有2021张 设置循环使用2021次
        int count=0;
        String str;
        while(true){
          if(i==0)break;  //当循环使用了2021次后跳出循环
          count++;
          str=""+count;  //为什么要加“”,再+count,转换成String类型
          for(char c:str.toCharArray()){  //使用toCharArray对str字符串进行遍历匹配
            if(c=='1'){
              i--;
            }
          }
        }
        System.out.println(count);
    }
}

ToCharArray( )的用法,将字符串对象中的字符转换为一个字符数组。

原题:例如,当小蓝有 3030 张卡片,其中 00 到 99 各 33 张,则小蓝可以拼出 11 到 1010,但是拼 1111 时卡片 11 已经只有一张了,不够拼出 1111。

现在小蓝手里有 00 到 99 的卡片各 20212021 张,共 2021020210 张,请问小蓝可以从 11 拼到多少?

posted @ 2022-12-27 16:02  抹茶泡芙  阅读(49)  评论(0)    收藏  举报