"孫子算經"之"物不知數"題:中國剩餘定理

來源:互聯網
上載者:User

標籤:

 

 

 1 #include<stdio.h> 2  3 // 4 // 吳仟靈 5 // 時間:2016、04、21 6 // 7 // "孫子算經"之"物不知數"題: 8 // 今有物不知其數,三三數之剩二,五五數之剩七,七七數之剩二,問物幾何?  9 //  10 // 根據題意我們有如下同於方程組:11 // x=2%3   ---> x=3*k+212 // x=3%513 // x=2%714 //15 16 17 // 分析法求解 18 int analytical(float m1, float m2, float m3, float a1, float a2, float a3)19 {20     float x=0,k1=0.0,k2=0.0,k3=0.0;21     22     for(k1=0; ;k1++){ 23         x=m1*k1+a1;     // => x=3*k+224         k2=(x-a2)/m2;   // => k=(x-2)/325         if(k2==(int)k2) // 判斷k2是否為整數 26         {27             k3=(x-a3)/m3;28             if(k3==(int)k3) // 判斷k2是否為整數29                 break;30         }31     }32     return (int)x;33 }34 35 36 // 中國剩餘定理求解 37 int chineseRemainderTheorem(int m1, int m2, int m3, int a1, int a2, int a3)38 {39     int i,x=0; 40     int M,M1,M2,M3;41     int y1,y2,y3;42     43     M =m1*m2*m3;44     M1=m2*m3;45     M2=m1*m3;46     M3=m1*m2;47     y1=M1%m1;48     y2=M2%m2;49     y3=M3%m3;50     x=(a1*M1*y1+a2*M2*y2+a3*M3*y3)%M;51     52     return x;53 }54 55 56 int main()57 {58     // x=2%3  x=a1%m159     // x=3%5  x=a2%m260     // x=2%7  x=a3%m361     int m1=3,m2=5,m3=7;62     int a1=2,a2=3,a3=2;63     printf("分析法:\nx=%d \n\n",analytical(m1,m2,m3,a1,a2,a3)); 64     printf("中國剩餘定理:\nx=%d \n\n",chineseRemainderTheorem(m1,m2,m3,a1,a2,a3));65     return 0;66 }

 

"孫子算經"之"物不知數"題:中國剩餘定理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.