新手求助:小学数学
一个数被2除余1;3除余2;
4除余3;
5除余4;
6除余5;
7除余6;
8除余7;
9除余8;
10除余9;
求最小的符合以上条件的数。
谢谢各位老师。
#include <stdio.h> int main( void ) { unsigned n = 1; for( ; ; ++n ) { if( n% 2 != 1 ) continue; if( n% 3 != 2 ) continue; if( n% 4 != 3 ) continue; if( n% 5 != 4 ) continue; if( n% 6 != 5 ) continue; if( n% 7 != 6 ) continue; if( n% 8 != 7 ) continue; if( n% 9 != 8 ) continue; if( n%10 != 9 ) continue; break; } printf( "%u\n", n ); }输出 2519
#include <stdio.h> unsigned std_gcd( unsigned a, unsigned b ) { for( ; b!=0; ) { unsigned t = a; a = b; b = t%b; } return a; } unsigned std_lcm( unsigned a, unsigned b ) { return (a==0&&b==0) ? 0 : a/std_gcd(a,b)*b; } int main( void ) { unsigned lcm = 1; for( unsigned i=2; i<=10; ++i ) lcm = std_lcm( lcm, i ); printf( "%u\n", lcm-1 ); }输出 2519