zoukankan      html  css  js  c++  java
  • 因式分解+猴子分桃

    猴子分桃

    海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

    View Code
     1 #include<iostream>
    2 using namespace std;
    3
    4 int main()
    5 {
    6 int m=1;
    7 while(1)
    8 {
    9 int n=m;
    10 int i;
    11 for(i=1;i<=5;i++)
    12 {
    13 if((n-1)%5!=0)
    14 break;
    15 else
    16 n=(n-1)/5*4;
    17 }
    18 if(i==6)
    19 {
    20 cout<<m<<endl;
    21 return 0;
    22 }
    23 else
    24 m++;
    25 }
    26 }

    思路,直接穷举,假设最开始有N个,N-1/5*4。

    因式分解,很好的递归题,

    如:

    12可以分为2*6,2*2*3,3*4,

    注意不能重复。

    View Code
     1 //分解因式
    2
    3 #include<iostream>
    4 #include<math.h>
    5 using namespace std;
    6
    7
    8
    9 void print(int* a,int len)
    10 {
    11 for(int i=0;i<len-1;i++)
    12 cout<<a[i]<<"*";
    13 cout<<a[len-1]<<endl;
    14 }
    15
    16 void split(int* a,int len)
    17 {
    18 print(a,len);
    19 int temp=a[len-1];
    20 for(int i=a[len-2];i<sqrt((double)temp);i++)
    21 {
    22 if(temp%i!=0)
    23 continue;
    24 else
    25 {
    26 a[len-1]=i;
    27 a[len]=temp/i;
    28 split(a,len+1);
    29 }
    30 }
    31 }
    32
    33 void solve(int n)
    34 {
    35 int *a=new int[n];
    36 for(int i=2;i<=sqrt((double)n);i++)
    37 {
    38 if(n%i!=0)
    39 continue;
    40 else
    41 {
    42 a[0]=i;
    43 a[1]=n/i;
    44 split(a,2);
    45 }
    46 }
    47 }
    48
    49 int main()
    50 {
    51 solve(9);
    52 cout<<"hualidefenjixian"<<endl;
    53 solve(24);
    54 return 0;
    55 }

    今天打电话浪费了我很多时间,也影响了我的心情,

    我决定以后少打电话,聊天聊不出个什么来,

    排解寂寞,孤独?我需要吗?


     

  • 相关阅读:
    215. Kth Largest Element in an Array (have better solution )
    414. Third Maximum Number
    442. Find All Duplicates in an Array
    448. Find All Numbers Disappeared in an Array
    485. Max Consecutive Ones
    532. K-diff Pairs in an Array
    8. String to Integer (atoi)
    7. Reverse Integer
    [CTSC2012]熟悉的文章(广义后缀自动机+二分答案+单调队列优化DP)
    BZOJ 2119 股市的预测(后缀数组)
  • 原文地址:https://www.cnblogs.com/YipWingTim/p/2250558.html
Copyright © 2011-2022 走看看