<一道题>abc+cba=1333,求满足条件的abc的值,隐含条件a!=0,c!=0
这类东西,无非就是穷举法。见下面代码:
#include <stdio.h> #include <stdlib.h> /* *abc + cba = 1333 * *a = ? *b = ? *c = ? */ int main(int argc ,char **argv) { int a=0; int b=0; int c=0; int index = 0; printf("abc + cba == 1333\n"); for(a = 1 ; a <= 9 ; a++)//a [1,9] { for(b = 0 ; b <= 9; b++)//b [0,9] { for(c = 1 ; c <= 9; c++)//c [1,9] { if(1333 == (100*a + 10*b + c + 100*c + 10*b + a)) { index++; printf("I have found it !!! index : %d , a= %d, b= %d, c=%d\n",index,a,b,c);//多谢1楼仁兄,找到了,却没有注意英文语法,已改found及I } } } } printf("find over\n"); return 0; }
运行结果;
tiger@ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/EXERCISE$ ./abc
abc + cba == 1333
I have found it !!! index : 1 , a= 4, b= 1, c=9
I have found it !!! index : 2 , a= 5, b= 1, c=8
I have found it !!! index : 3 , a= 6, b= 1, c=7
I have found it !!! index : 4 , a= 7, b= 1, c=6
I have found it !!! index : 5 , a= 8, b= 1, c=5
I have found it !!! index : 6 , a= 9, b= 1, c=4
find over
tiger@ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/EXERCISE$