编程马拉松算法题

xiaoxiao2021-02-28  5

/ write your code here cpp #include<iostream> using namespace std; int  LCM(int num1,int num2){     int x,y;     if(num1<num2){         num1^=num2;num2^=num1;num1^=num2;     }     x=num1;y=num2;     while(y!=0)     {int temp=x%y;     x=y;     y=temp;     }     return (num1*num2)/x; } int main(){     int n,m;     while (cin>>m>>n&&n!=0&&m!=0)     {         int temp=1;int sum=0;int Multiply=1;         int heritage[10];         for (int i = 0; i <n; i++)         {             cin>>heritage[i];         }         for (int i = 0; i <n; i++)         {             temp=LCM(temp,heritage[i]);             Multiply*=heritage[i];         }         for (int i = 0; i <n; i++)         {             sum+=temp/heritage[i];         }         if (m%sum==0)         {   int k=m/sum;         for (int i = 0; i <n-1; i++)         {             cout<<k*temp/heritage[i]<<" ";         }         cout<<k*temp/heritage[n-1]<<endl;         }         else         {             cout<<"Can't Solve"<<endl;         }     }     return 0; } 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
转载请注明原文地址: https://www.6miu.com/read-1750097.html

最新回复(0)