11743 - Credit Check

All about problems in Volume 117. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
Mizanur Rahman(IUK)
New poster
Posts: 12
Joined: Wed Aug 18, 2010 12:07 pm

11743 - Credit Check

Post by Mizanur Rahman(IUK) » Fri Dec 10, 2010 8:38 pm

Code: Select all

#include<stdio.h>
#include<iostream.h>

int main()
{
	long int i,n;
	cin>>n;
	for(i=1;i<=n;i++)
	{long int j,sum1,k,count,tmp;
	char a[20]={'0'};
		sum1=0;
		scanf("%[^\n]s",a);getchar();
		count=0;j=0;
		while(count!=16)
		{
			if(a[j]!=32)
			{				
			k=int (a[j])-48;
			if(count%2==0)
				{
				k=k*2;
				if(k>=10)
					{
					tmp=k%10;
					k=k/10;
					k=k+tmp;
					}
				}
			sum1=sum1+k;
			count++;
			}
			j++;
		}
		if(sum1%10==0)
			cout<<"Valid"<<endl;
		else
			cout<<"Invalid"<<endl;
	}
	return 0;
}

Kamarul Kawnayeen
New poster
Posts: 2
Joined: Wed Feb 02, 2011 10:10 pm
Contact:

Re: 11743 - Credit Check Why WA please help me!!!!!

Post by Kamarul Kawnayeen » Thu Feb 03, 2011 12:39 pm

If you can change your approaches to input the card number, then the problem become easy. Try the following code segment for inputing card number:

Code: Select all

int credit[16], j;
		
	for(j=0;j<16;j++)
	{
		scanf("%1d", &credit[j]);
	}
Then sum the number in odd index of the array credit[] (i.e. at 1, 3, 5,...15 index) and store it in a variable.
Then sum the 2*number in even index of credit[](i.e. 0, 2, 4, ...14). if 2*number is greater than 9 then try the following code segment:

Code: Select all

         sum = sum + (a[j]*2)/10 + (a[j]*2)%10;
I hope it will help you.

naseef_07cuet
Learning poster
Posts: 62
Joined: Sat Nov 21, 2009 10:17 pm
Location: CUET,Chittagong,Bangladesh

Re: 11743 - Credit Check Why WA please help me!!!!!

Post by naseef_07cuet » Wed Mar 09, 2011 1:51 am

Kamarul got it right.I am agree with him.
If you have determination, you can do anything you want....:)

sarowar_csecu
New poster
Posts: 4
Joined: Wed Nov 04, 2009 6:08 am

Re: 11743 - Credit Check Why WA please help me!!!!!

Post by sarowar_csecu » Tue Sep 20, 2011 9:39 pm

getting WA! :( why??

Code: Select all

#include<stdio.h>

int gd(int n){
    n=n*2;
    if(n>9)
       return n/10+n%10;
    else return n;
}

int main(int argc, char** argv) {

    int n,i,j,k,f=0,d,ud,s;

    int c[20];


    scanf("%d",&n);
    
    while(n--){
        d=0;
        ud=0;

        for(i=0;i<16;i++){
            scanf("%1d",&c[i]);
            if(i%2==0){
                d+=gd(c[i]);
            }else
                ud+=c[i];
        }
        s=d+ud;
        if(s%10==0)
            printf("Valid");
        else printf("Invalid");

        if(n!=0)
            printf("\n");
    }

    return 0;
}
Sarowar

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 11743 - Credit Check Why WA please help me!!!!!

Post by uDebug » Mon Mar 24, 2014 11:58 am

sarowar_csecu wrote:getting WA! :( why??
Try the following input:

Code: Select all

1
8626 1020 0070 7050
AC Output:

Code: Select all

Valid
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 11743 - Credit Check Why WA please help me!!!!!

Post by uDebug » Mon Mar 24, 2014 12:18 pm

Here's some input / output I found useful during testing / debugging.

Input:

Code: Select all

9
9999 9999 9999 9999
0000 0000 0000 0000
5050 5050 5050 5050
1234 5678 1234 5678
8931 7853 8957 2362
1000 0000 1001 1009
8326 1829 0478 7841
8626 1020 0070 7050
8726 1020 0070 7050
AC Output:

Code: Select all

Invalid
Valid
Invalid
Invalid
Invalid
Invalid
Invalid
Valid
Invalid
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

Zyaad Jaunnoo
Experienced poster
Posts: 122
Joined: Tue Apr 16, 2002 10:07 am

Re: 11743 - Credit Check

Post by Zyaad Jaunnoo » Mon Sep 08, 2014 5:53 pm

A small tip to avoid recomputation.

Cache the sum of digits after doubling them:

Code: Select all

Digit   Double   Sum of digits
0         0           0
1         2           2
2         4           4
3         6           6
4         8           8
5         10          1
6         12          3
7         14          5
8         16          7
9         18          9

int sumOfDigits[] = { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 };

Post Reply

Return to “Volume 117 (11700-11799)”