9.22 3 The problem of summing numbers

/*

  • Xiao Ming is very interested in numbers containing 2, 0, 1, and 9 in the digits. In 1 to 40, such numbers include 1, 2, 9, 10 to 32, 39 and 40, a total of 28,
  • Their sum is 574. Excuse me, what is the sum of all such numbers from 1 to 2019?
    */
    public class Demo1 { public static void main(String[] args) { int sum=0; String n; for(int i=1;i<=2019;i++){ n=""+i; for(int j=0;j<n.length();j++){ if(n.charAt(j)






    ‘2’ || n.charAt(j)‘0’ || n.charAt(j)‘1’ || n.charAt(j)‘9’){ sum+=i; break; } } } System.out.println(sum); } }







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325397361&siteId=291194637