数字谜2 C实现

来源:互联网 发布:java javascript区别 编辑:程序博客网 时间:2024/06/11 16:19

该问题出自《C语言名题精选百则技巧篇》

在数字谜1的基础上的练习

    SEND

+ MORE

MONEY

#include <stdio.h>#include <stdlib.h>int main(int argc,char *argv[]){int S,E,N,D,M=1,O,R,Y;long SEND,MORE,MONEY;long sum;printf("\nNumber Puzzle\n");printf("\n    SEND");printf("\n+)  MORE");printf("\n---------");printf("\n   MONEY\n");for(S=8;S<=9;S++)for(E=1;E<=9;E++)    if(E!=S&&E!=M)        for(N=0;N<=9;N++)            if(N!=E&&N!=S&&N!=M)                for(D=1;D<=9;D++)                    if(D!=N&&D!=E&&D!=S&&D!=M)                        for(O=0;O<=1;O++)                            if(O!=D&&O!=N&&O!=E&&O!=S&&O!=M)                                for(R=0;R<=9;R++)                                    if(R!=O&&R!=D&&R!=N&&R!=E&&R!=S&&R!=M)                                        for(Y=0;Y<=9;Y++)                                            if(Y!=R&&Y!=O&&Y!=D&&Y!=N&&Y!=E&&Y!=S&&Y!=M){MONEY = (((M*10+O)*10+N)*10+E)*10+Y;SEND = ((S*10+E)*10+N)*10+D;MORE = ((M*10+O)*10+R)*10+E;sum = SEND+MORE;if(sum == MONEY){printf("\n\nThe answer is:\n");printf("\n%8ld",SEND);printf("\n+)%6ld",MORE);<span style="white-space:pre"></span>printf("\n--------");printf("\n%8ld",MONEY);         }                    }                                                     getchar();return 0;} 
运行结果



0 0
原创粉丝点击