Luo Gu solution to a problem --P1008
Title Description
The 1,2, ⋯, 9 99 total number of divided into three groups, each consisting of three digits 3 and 3 so that these constitute a three-digit number: 2: 3 ratio, satisfy the conditions of the test to obtain all three three digits.
Input Format
wood input
Output format
several rows of three numbers. Each row arranged in a numerical order.
Code
#include<cstdio>
#include<cstring>
int i,j,v;
bool a[10];//ai表示第i个数已经用过了
int main()
{
for(i=123;i<=329;++i)//第一个数的范围,也可以写成100-333
{
memset(a,0,sizeof(a));
v=0;
a[i%10]=a[i/10%10]=a[i/100]=1;
a[i*2%10]=a[i*2/10%10]=a[i*2/100]=1;
a[i*3%10]=a[i*3/10%10]=a[i*3/100]=1;
//如果没有那个数字,那么对应的a就为0
for(j=1;j<=9;j++)//看看1-9是否都有了
{
v+=a[j];
}
if(v==9) printf("%d %d %d\n",i,i*2,i*3);
}
return 0;
}
Another thought: when the sum of the numbers A and set equal to a set number of the set A and B and the number of set B is equal to by digital multiplication, set A equal to B element.
#include <stdio.h>
int main()
{
int a,b,c;
for(a=123;a<=333;a++)
{
b=a*2;
c=a*3;
if((a/100+a/10%10+a%10+b/100+b/10%10+b%10+c/100+c/10%10+c%10==1+2+3+4+5+6+7+8+9)&&((a/100)*(a/10%10)*(a%10)*(b/100)*(b/10%10)*(b%10)*(c/100)*(c/10%10)*(c%10)==(1)*(2)*(3)*(4)*(5)*(6)*(7)*(8)*(9)))
printf("%d %d %d\n",a,b,c);
}
return 0;
}
Another idea:
# include<stdio.h>
int main()
{
int b, c, a[10], flag=0;
for (int i = 100; i <= 333; i++)//第一个数的范围
{
b = i * 2; c = i * 3;
//a中存储的是对应的数字
a[0] = i / 100 % 10; a[1] = i / 10 % 10; a[2] = i % 10;
a[3] = b / 100 % 10; a[4] = b / 10 % 10; a[5] = b % 10;
a[6] = c / 100 % 10; a[7] = c / 10 % 10; a[8] = c % 10;
for (int j = 0; j < 9; j++)//用标记看是否有两个数字相等
{
for (int k = j + 1; k < 9; k++)
{
if (a[j] == a[k]||a[j]==0||a[k]==0)flag = 1;
}
}
if (flag == 0)printf("%d %d %d\n", i, b, c);
flag = 0;
}
}