-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP6_1.java
More file actions
51 lines (48 loc) · 876 Bytes
/
P6_1.java
File metadata and controls
51 lines (48 loc) · 876 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
49
50
51
import java.util.OptionalInt;
public class P6_1 {
public static void main(String[] args) {
System.out.println(STI("124"));
System.out.println(STI("-124"));
System.out.println(ITS(4653));
System.out.println(ITS(-4653));
}
public static int STI(String s)
{
if(s.equals(""))
return 0;
boolean neg = false;
if(s.charAt(0) == '-')
{
neg = true;
s = s.substring(1);
}
int num = 0;
int xy = s.chars().reduce(0, (sum,y) -> sum * 10 + y - '0');
for(char x: s.toCharArray())
{
num *= 10;
num += x - '0';
}
return xy;
//return neg ? -num : num;
}
public static String ITS(int x)
{
StringBuilder str = new StringBuilder();
boolean neg = false;
if(x < 0)
{
neg = true;
x = -x;
}
while(x != 0)
{
int j = x% 10;
x/= 10;
str.insert(0,j);
}
if(neg)
str.insert(0,'-');
return str.toString();
}
}