Java - 数字中至少有一个7和一个9的数字 [英] Java - numbers with at least one 7 and one 9 in its digit
本文介绍了Java - 数字中至少有一个7和一个9的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须编写一个程序,通过检查每个数字中从1到999,999(强力方法)的数字来确定1,000,000下的正整数有多少至少有一个7和至少一个9。答案应该是199,262。请帮忙!
I have to code a program to determine how many positive integers under 1,000,000 have at least one 7 and at least one 9 among its digits by examining the digits in each number from 1 to 999,999 ("brute-force" method). The answer is supposed to be 199,262. Please help!
推荐答案
如果数字包含7 和 9,那么如何将数字转换为字符串和文本
How about converting the number to a String and text if it contains 7 and 9
int count = 0;
for (int i = 1; i < 1000000; i++) {
String text = String.valueOf(i);
// contains both
if (text.contains("7") && text.contains("9")) count++;
}
System.out.println(count);
这篇关于Java - 数字中至少有一个7和一个9的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文