Problem C - You can say 11
Time Limite: 1 second
Time Limite: 1 second
Introduction to the problem
Your job is, given a positive number N, determine if it is a multiple of eleven.
Description of the input
The input is a file such that each line contains a positive number. A line containing the number 0 is the end of the input. The given numbers can contain up to 1000 digits.
Description of the output
The output of the program shall indicate, for each input number, if it is a multiple of eleven or not.
Sample input:
112233
30800
2937
323455693
5038297
112234
0
Sample output
112233 is a multiple of 11.
30800 is a multiple of 11.
2937 is a multiple of 11.
323455693 is a multiple of 11.
5038297 is a multiple of 11.
112234 is not a multiple of 11.
大意:
給1個1000位以下的數字 求可否被11整除
import java.util.Scanner;
public class UVA10929 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
String N = sc.next();
if (N.equals("0"))
break;
long sum = 0;
for (int i = 1; i <= N.length(); i++) {
if (i % 2 != 0) {
sum += Integer.parseInt(N.substring(i - 1, i));
} else if (i % 2 == 0) {
sum -= Integer.parseInt(N.substring(i - 1, i));
}
}
if (sum % 11 == 0)
System.out.println(N + " is a multiple of 11.");
else
System.out.println(N + " is not a multiple of 11.");
}
}
}
留言
張貼留言