---
# System prepended metadata

title: 'a159: 11743 - Credit Check'
tags: [Zerojudge]

---

# a159: 11743 - Credit Check
題目連結:[a159](https://zerojudge.tw/ShowProblem?problemid=a159)
題目要驗證卡號是否正確，但給定長度卻是分成四組
我的解法是以分四組當成一個數字處理，讀進來後直接處理，再分別放到偶位數和奇位數，最後驗證

以下為處理數字部分：
```c=
check1=check1+(2*(credit/1000))/10+(2*(credit/1000))%10;
credit=credit%1000;

check2=check2+(credit/100);
credit=credit%100;

check1=check1+(2*(credit/10))/10+(2*(credit/10))%10;
credit=credit%10;

check2=check2+credit;
```
/1000、/100、/10 是分成兩個偶位數和兩個奇位數的取值
%1000、%100、%10 則是將前一個運算的值處理掉

以下為程式碼：
```c=
#include<stdio.h>

int main(){
	int r,credit,e,check1,check2,i;
	while(scanf("%d",&r)!=EOF){
		for(e=0;e<r;e++){
			check1=0;
			check2=0;
			for(i=0;i<4;i++){
				scanf("%d",&credit);
				check1=check1+(2*(credit/1000))/10+(2*(credit/1000))%10;
				credit=credit%1000;
				check2=check2+(credit/100);
				credit=credit%100;
				check1=check1+(2*(credit/10))/10+(2*(credit/10))%10;
				credit=credit%10;
				check2=check2+credit;
			}
			if((check1+check2)%10==0){
				printf("Valid\n");
			}else{
				printf("Invalid\n");
			}
		}
	}
	return 0;
}
```

