The 9 numbers from 1 to 9 consist of 3 3 digits, and three numbers are 1:2:3 in proportion, trying to find out all the combinations

Source: Internet
Author: User

A classmate is doing ACM, gave me a problem, the title is the same. Finally write the following

/* Use 1 to 9 to make up 3 3-digit, and three-number ratio of 1:2:3, to find out all the number of satisfying conditions */#include <stdio.h> #include <stdbool.h> int noc_1 (int n);	Returns the number of the hundreds of n int noc_2 (int n);	Returns the N of the 10-bit numeric int noc_3 (int n);		Returns the digit of the n digit bool check (int x,int y,int z); Verify that the number on each bit of x y x is the same if it is not the same return true, otherwise it returns false int main (void) {int a,b,c;//a b C is 3 three-digit number int i,j,k;//i J K is a Bashing bit of a	the number int n_1,n_2,n_3;
		Use n_1 n_2 n_3 to rotate the three digits of a for (n_1=1;n_1<4;n_1++) {i=n_1;
			for (n_2=1;n_2<10;n_2++) {if (n_2==n_1)//If there is the same number that discards the combination of this series continue;
			
			j=n_2; for (n_3=1;n_3<10;n_3++) {if (n_2==n_3| |
				N_1==n_3)//If there is the same number to discard the combination of this series continue;
				K=n_3;
				A=i*100+j*10+k;
				b=a*2;
				c=a*3;
				if (c>999)//If this combination is discarded for a four-digit number continue;
			if (check (a,b,c) ==true) printf ("%5d%5d%5d\n", a,b,c);
}}} return 0;

} int noc_1 (int n) {return n/100;}
	int noc_2 (int n) {N=N/10;
return n%10;

} int Noc_3 (int n) {return n%10;}
	BOOL Check (int x,int y,int z) {int a[9]; A[0]=noc_1 (x);
	a[1]=noc_2 (x);
	A[2]=noc_3 (x);
	A[3]=noc_1 (y);
	A[4]=noc_2 (y);
	A[5]=noc_3 (y);
	A[6]=noc_1 (z);
	A[7]=noc_2 (z);
	A[8]=noc_3 (z);
	int i,j;
		for (i=0;i<9;i++) {if (a[i]==0)//b C may have a bit on the number of 0 if 0 then return false to false;
			for (j=0;j<9;j++) {if (j==i) continue;
		if (A[i]==a[j]) return false;
}} return true; }
Compile, run the following as shown below
hyp@debian:~$ gcc-wall shu.c 
hyp@debian:~$./a.out 
  192  384  576
  219  438  657
  273  546  819
  327  654  981
hyp@debian:~$ 

This I think there should be a lot of algorithms, I have not yet seen how many algorithmic knowledge, now written as above.


------------------------------------------20121127-------------------------------

I didn't think of that. Indeed this is much shorter and more efficient, and I have done a lot of useless thinking. In fact, tests can be put to the end, and some judgments can be streamlined. Haha, long insight, everyone has a misunderstanding of thinking, considered too hasty.

#include <stdio.h>
int main ()
{
	int a,b,c,i,j,s[9];
	for (a=100;a<333;a++)
	{
		b=2*a;c=3*a;
		s[0]=a%10;s[1]=a%100/10;s[2]=a/100;
		s[3]=b%10;s[4]=b%100/10;s[5]=b/100;
		s[6]=c%10;s[7]=c%100/10;s[8]=c/100;
		for (i=0;i<8;i++)
			{for
				(j=i+1;j<9;j++)
					if (s[i]==s[j]) break;
				if (j<9) break;
			}
		if ((i==8) && (j==9))
			printf ("%d,%d,%d\n", a,b,c);
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.