1부터 10,000까지 8이라는 숫자가 총 몇번 나오는가?
8이 포함되어 있는 숫자의 갯수를 카운팅 하는 것이 아니라 8이라는 숫자를 모두 카운팅 해야 한다.
(※(注記) 예를들어 8808은 3, 8888은 4로 카운팅 해야 함)
using System;
class Program
{
static void Main(string[] args)
{
int numberEightCount = 0;
for (int i = 1; i <= 10000; i++)
{
char[] digits = i.ToString().ToCharArray();
foreach (var aDigit in digits)
{
if (aDigit == '8')
{
numberEightCount++;
}
}
}
Console.WriteLine("numberEightCount = " + numberEightCount);
}
}
2014年09月25日 16:17
objective c 풀었습니다. 문자열로 변환이후 각 자리수를 어떻게 구해야 하나 생각하느라 시간이 좀 걸렸네요
- (int) CountNumber:(NSString *)num
{
self.countNum = 0;
for(int i = 1; i<=10000; i++)//1~10000까지 숫자를 한번씩 계산합니다
{
NSString *temp = [NSString stringWithFormat:@"%d", i];
//문자열로 전환
for(int j=0; j < temp.length; j++)//그 전환한 문자열의 자릿수 만큼 반복합니다
{
if('8' == [temp characterAtIndex:j])//각 자리수를 체크
{
countNum++;
}
}
}
return countNum;
};
//main
CountNumber *myCountNum = [[CountNumber alloc] init];
[myCountNum CountNumber:@"8"];
NSLog(@"%d", myCountNum.countNum);
//결과
CodingDojang[965:303] 4000
2014年09月30日 16:19
성능을 위해 StringBuilder를 사용했습니다
public class Main {
public static void main(String[] ar){
int cnt = 0;
StringBuilder s = new StringBuilder();
for(int i = 1; i <= 10000; i++){
s.append(i);
}
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '8')
cnt++;
}
System.out.println(cnt);
}
}
2014年09月30日 23:17
puts ("1".."10000").to_a.join.count("8")
2014年10月05日 05:37
package Coding;
public class coding2 {
public static void main(String args[]){
int ard=0; // 나누어줄 값
int res=0; // 계산의 결과
int cor=0; // 결과값, 갯수
int incr=88; // 구해야할 값의 한계
for(int j=0;j<=incr;j++){
ard = j;
for(int i=0; i<=ard;i++){
res = ard % 10;
ard /= 10;
if(res == 8){
++cor;
}
res = 0;
}
System.out.println(cor);
}
}
}
total = 0
for x in str(range(1,10001)):
for n in x:
if n == '8':
total += 1
print total
'길가의 풀'님의 풀이를 파이썬으로 더 풀어봤습니다. 더 길어서 의미는 없습니다만ᄒ
2014年10月20日 11:24
sum([str(x).count('8') for x in range(10001)])
2014年11月01日 05:55
public class CountEightMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "";
char cArray[] = null;
long cnt = 0;
for(int i = 1; i <= 10000; i++) {
s = Integer.toString(i);
cArray = s.toCharArray();
for (int j = 0; j < cArray.length; j++) {
if (cArray[j] == '8') {
cnt = cnt + 1;
}
}
}
System.out.println(cnt);
}
}
2014年11月14日 22:39
scala로 풀었습니다.
(1 to 10000).map(_.toString.count(_ == '8')).sum
2014年12月04日 16:08
풀이 작성