# zerojudge a159: UVA 11743 Credit Check ###### tags: `zerojudge` 題目: [https://zerojudge.tw/ShowProblem?problemid=a159](https://zerojudge.tw/ShowProblem?problemid=a159) ## 題目要求 1. 奇數位的數字 需乘2 2. 再將乘2的數字各位數字相加 3. 偶數位的數字相加 4. 將步驟2和步驟4的數字相加為答案 ## 解題方法: 1. 首先,我用陣列card 將測資分為四串數字 2. 接著,我採用餘除10和除10 獲得各位數字 並以temp和temp2儲存奇數位 和 偶數位的和 3. 最後,將temp和temp2相加便能得到答案 #### 附註:為了避免TLE,可以使用printf和scanf提升效率 --- ### 以下是程式碼: ```c= #include<stdio.h> using namespace std; int main(){ int num; scanf("%d" , &num); int card[4]; int code[4]; int temp; int temp2 = 0; for(int i=0 ; i<num ; i++){ temp2 = 0; for(int j=0 ; j<4 ; j++){ scanf("%d", &card[j]); code[j] = 0; temp = card[j]/10; code[j] = ( temp%10 ) * 2; code[j] = code[j]/10 + code[j]%10; code[j] = code[j] + ( ( temp/100 ) * 2/10 + ( temp/100 ) * 2%10 ); temp = card[j] % 1000; temp2 = temp2 + temp/100 + temp%10; } temp = 0; for(int j=0 ; j<4 ; j++){ //printf("%d\t",code[j]); temp = temp +code[j]; } //printf("temp %d \t temp2 \t %d \t",temp,temp2); temp = temp+temp2; //printf("%d \t",temp); if(temp%10 == 0){ printf("Valid\n"); }else{ printf("Invalid\n"); } } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up