最近在朋友圈有朋友问我,这样一道题,具体如图所示:
我的算法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | static void main( string [] args) { /* * a + b - 9 = 4 * + - - * c - d * e =4 * / * - * f + g - h=4 * || || || * 4 4 4 * */ int flag = 50; for ( int a=0;a<14; a++) //a { for ( int b = 0; b <14; b++) //b { //a+b-9=4 故 a+b=13 if ((a + b) == 13) { //9-e-h=4 故 e+h=5, e<6,h<6 for ( int e = 0; e < 6; e++) //e { for ( int h = 0; h < 6; h++) //h { if ((e + h) == 5) { for ( int c = 0; c < flag; c++) //c { //b-d*g=4,b[0-13] 故d*g<10,d,g<10 for ( int d = 0; d < 10; d++) //d d*g<=9 { for ( int g = 0; g < 10; g++) //g { //考虑到f为被除数,故f>0,考虑整除c%f==0 for ( int f = 1; f < flag; f++) //f { while ((c % f )== 0&&(c - d * e) == 4 && (f + g - h) == 4 && (a + c / f) == 4 && (b - d * g) == 4) { console.writeline( "----------------------------------" ); console.write( "a:{0} " , a.tostring()); console.write( "b:{0} " , b.tostring()); console.write( "c:{0} " , c.tostring()); console.write( "d:{0} " , d.tostring()); console.write( "e:{0} " , e.tostring()); console.write( "f:{0} " , f.tostring()); console.write( "g:{0} " , g.tostring()); console.writeline( "h:{0} " , h.tostring()); console.writeline( "----------------------------------" ); console.readline(); } } } } } } } } } } } } |
以上所述是小编给大家分享的基于c#代码实现九宫格算法横竖都等于4的相关内容,希望对大家有所帮助。