課題か

シン@C.B
記事: 0
登録日時: 14年前
住所: 石川県金沢市
連絡を取る:

課題か

投稿記事 by シン@C.B » 14年前

このプログラムやばい効率わるいな

3桁+3桁=3桁

になり、1~9までの数字をそれぞれ入れろというプログラムです。



ちなみにこれから作らなきゃいけないのは

□□□×□□=□□□□

□はどこにおいてもよくて□の数の合計が9個になればいい。

どうすっかなww

CODE:

#include 
int z[10000],x[11100],y[10000000],k,t[100],j[100],l[100],a,b,c;
int main(){

	a=100;
   b=100;
   c=100;
while(1){ 

y[c] = a + b; 



if(a==999){ 

	a=100;
b=b+1;



if(b==500)break;
}

if(y[c]>=100){

t[0]=a/100;
t[1]=a/10;
t[2]=t[1]%10;
t[3]=a%t[1];

j[0]=b/100;
j[1]=b/10;
j[2]=j[1]%10;
j[3]=b%j[1];

l[0]=y[c]/100;
l[1]=y[c]/10;
l[2]=l[1]%10;
l[3]=y[c]%l[1];

}







if(  y[c]<=1000 && t[0] !=t[2] && t[0] !=t[3] && t[2] !=t[3]



     && j[0] !=j[2] && j[0] !=j[3] && j[2] !=j[3]




&& l[0] !=l[2] && l[0] !=l[3] && l[2] !=l[3]

&&t[0] !=j[0] &&t[0] !=j[2] && t[0] != j[3]

&&t[0] !=l[0] &&t[0] !=l[2] && t[0] != l[3]

&&t[2] !=j[0] && t[2] != j[0] &&t[2] !=l[0]

&&t[2] !=j[2] && t[2] != j[2] &&t[2] !=l[2]

&&t[2] !=j[3] && t[2] != j[3] &&t[2] !=l[3]


&&t[3] !=j[0] && t[3] != j[0] &&t[3] !=l[0]
			 
&&t[3] !=j[2] && t[3] != j[2] &&t[3] !=l[2]
			 
			 
&&t[3] !=j[3] && t[3] != j[3] &&t[3] !=l[3]



 && t[0] !=l[0] && t[0] !=l[2] && t[0] !=l[3]
&&t[2] !=l[0] &&t[2] !=l[2] && t[2] != l[3]
&&t[3] !=l[0] &&t[3] !=l[2] && t[3] != l[3]

			
			                 && j[0] !=l[0] && j[0] !=l[2] && j[0] !=l[3]

&&j[2] != l[0] && j[2] !=l[2] && j[3] != l[3]
   &&j[3] !=l[0] &&  j[3] !=l[3] && j[3] != l[3]

	&&j[3] != l[2] &&j[2] != l[3] 
			 
			 
	&&t[0] !=0 &&t[2] !=0 &&t[3] !=0
	&&j[0] !=0 &&j[2] !=0 &&j[3] !=0
     &&l[0] !=0 &&l[2] !=0 &&l[3] !=0


){

printf("%d",a);
printf
("+");
printf("%d=",b);
	
printf("%d\n",y[c]);
}


c=c++;







a++;


}

scanf("%d",j[0]);

return 0;}

アバター
a5ua
記事: 199
登録日時: 15年前

Re: 課題か

投稿記事 by a5ua » 14年前

□の数字の重複はなしですか?だとしたら、順列を列挙する必要がありますね。

C++でやるならこんな感じかな(Cなら、next_permutation相当のものを自分で作る必要があります)

CODE:

#include 
#include 
 
// 各桁を表す数字の配列をintに変換
int to_number(const int D[], int n)
{
	int i;
	int x = 0;
	int d = 1;
 
	for (i = n - 1; i >= 0; i--) {
		x += D[i] * d;
		d *= 10;
	}
	return x;
}
 
int main(void)
{
	int D[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
 
	do {
		// □□□+□□□=□□□なら表示 
		int x = to_number(D + 0, 3);
		int y = to_number(D + 3, 3);
		int z = to_number(D + 6, 3);
		if (x + y == z) {
			printf("%d + %d = %d\n", x, y, z);
		}
	} while (std::next_permutation(D, D + 9));
 
	return 0;
}