2

Assume the character a,b,c,d,e represent the number 1 to 9, and they cannot be equal to each other.

Question:

How many equals that can meet (ab * cde = adb * ce). Example: 36 * 495 = 396 * 45.

Here is my code,and the result is right.However,i think my code is too awkward,especially in (if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e&&c*d*e!=0))

I would appreciate it if someone could give me a better solution.

#include<stdio.h>
main(){
    int a,b,c,d,e,m,n,i=0;
    long f1,f2,f3,f4;
    for(m=11;m<=99;m++){
        a=m/10;
        b=m%10;
        if(a!=b&&a*b!=0) 
        {
            for(n=101;n<=999;n++)
            {
                c=n/100;
                d=n%100/10;
                e=n%10;
                if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e&&c*d*e!=0)
                {
                    f1=a*10+b;
                    f2=c*100+d*10+e;
                    f3=a*100+d*10+b;
                    f4=c*10+e;
                    if(f1*f2==f3*f4) i++;
                    printf("\n%d%d*%d%d%d*=%d%d%d*%d%d\n",a,b,c,d,e,a,d,b,c,e);
                }
            }
        }
    }
    printf("%d\n",i);
    return 0;
}
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
king
  • 300
  • 1
  • 13
  • 4
    A couple of spaces or parentheses would be great. – pzaenger Oct 24 '13 at 07:47
  • 10
    This question belongs on another site in the Stack Exchange network, http://codereview.stackexchange.com/ . – orlp Oct 24 '13 at 07:48
  • 2
    Remake your `a, b, c, d, e` to an array and awkwardness may vanish – mr5 Oct 24 '13 at 07:51
  • 1
    Looks like a homework problem. If it is, you could try peaking at your classmate's code. Otherwise my first suggestion is to use functions to break the program into smaller pieces. – Leonardo Oct 24 '13 at 07:56

2 Answers2

2

If you can, instead of

int a,b,c,d,e;

Try to use

int numbers[5];

And then to check if your numbers are all different, you can use for loops

doubleOccurence = FALSE; /* where FALSE = 0 */
for (i=0; i < 4; i++) {
    for (j=i+1; j < 5; j++) {
        doubleOccurence = doubleOccurence || (numbers[i] == numbers[j]);
    }
}

It looks a bit clearer to me.

Julien
  • 2,544
  • 1
  • 20
  • 25
2

Unfortunately you can't really iterate through a list of variables you are better off with an array of numbers like Julien mentions in his answer.

int nums[5];

replace a with nums[0], b with nums[1], etc....

But then I would go one step further to tidying up your code and call a function that takes in the array to check uniqueness:

if(listIsUnique(nums, 5)) // yes hardcoded the 5, but that can be sorted
{
    ...
}

And then:

bool listIsUnique(int* nums, int len)
{
    for (int i = 0; i < len; i++)
        for (int j = i + 1; j < len; j++)
            if (nums[i] == nums[j])
                return false; // return false as soon as you find a match - slightly faster :)

    return true; // if we get here its a unique list :)
}

Note: code is untested, there may be mistakes :o

BenMorel
  • 34,448
  • 50
  • 182
  • 322
code_fodder
  • 15,263
  • 17
  • 90
  • 167