andy6804tw
9/28/2017 - 12:29 PM

Q11743 : Credit Check

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2843

106.09.26 CPE第一題 這題很簡單只是要驗證此信用卡數字串是否合法 他有說到奇數位乘以2再把每位數字加起來,當偶數位時直接該數加起來不做任何加成 最後算出來有個加總值當最後一位為0時就是合法

最後判斷直接用%10看看能否被整除就行拉~

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int n = Integer.parseInt(scn.nextLine());
		while (n-- != 0) {
			String arr[] = scn.nextLine().split(" ");
			int tot = 0;
			for (int i = 0; i < arr.length; i++) {
				char ary[] = arr[i].toCharArray();
				for (int j = 0; j < ary.length; j++) {
					if (j % 2 == 0)
						tot = tot + (ary[j] - '0') * 2 % 10 + (ary[j] - '0') * 2 / 10;
					else
						tot += ary[j] - '0';
				}
			}
			if (tot % 10 == 0)
				System.out.println("Valid");
			else
				System.out.println("Invalid");
		}
	}
/*題目:Q11743 : Credit Check
    作者:1010
    時間:西元 2017 年9 月 */
}