forked from KeepMoving/AlgorithmsLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum1s.java
More file actions
48 lines (39 loc) · 815 Bytes
/
Copy pathSum1s.java
File metadata and controls
48 lines (39 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
public class Sum1s
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int data = keyboard.nextInt();
int result = sum1s(data);
System.out.println("result = " + result);
}
public static int sum1s(int n)
{
int iCount = 0;
int iFactor = 1;
int iLowerNum = 0;
int iCurrNum = 0;
int iHigherNum = 0;
while(n/iFactor != 0)
{
iLowerNum = n - (n/iFactor)*iFactor;
iCurrNum = (n/iFactor)%10;
iHigherNum = n / (iFactor*10);
switch (iCurrNum)
{
case 0:
iCount += iHigherNum * iFactor;
break;
case 1:
iCount += iHigherNum * iFactor + iLowerNum + 1;
break;
default:
iCount += (iHigherNum + 1)*iFactor;
break;
}
iFactor *= 10;
}
return iCount;
}
}