Brush Questions-Math

Unavailable number

Ideas:

For this kind of problem, when there is no idea, you can use the method of looking for rules by looking at the table:

#include <iostream>
using namespace std;

bool dfs(int i , int n , int m)
{
    if(i == 0)   return true;
    if(i < n)    return false;
    if(dfs(i - n , n , m))
        return true;
    if(dfs(i - m , n , m))
        return true;
    
    return false;
}
int main(void)
{
    int n,m;
    cin >> n >> m;
    for(int i = 1000; i > 0; i --)
        if(!dfs(i , n , m))
        {
            cout << i << endl;
            break;
        }
    
    return 0;
}

Results: 2 3 1
2 5 3 3 5 7 4 5 11 2 7 5 3 7 11 4 7 17 The law is: (n-1) (m-1)-1

Code:

#include <iostream>
using namespace std;

int main(void)
{
    int n,m;
    cin >> n >> m;
    cout << (n-1)*(m-1)-1 << endl;
    
    return 0;
}

Ant cold

Ideas:

The most important point: two ants making a U-turn can be equivalent to two ants. After thinking of the above, the analysis is simple. It is only necessary to separately count the total number of ants on the right and left of the first cold ant.

### Code:

#include <iostream>
using namespace std;

int main(void)
{
    int n;
    cin >> n;
    int a[55];
    for(int i = 0; i < n;i++)
        cin >> a[i];
    
    int l = 0,r = 0;
    for(int i = 1; i < n;i++)
    {
        if(abs(a[i]) > abs(a[0]) && a[i] < 0) l++;
        if(abs(a[i]) < abs(a[0]) && a[i] > 0) r++;
    }
    //特殊情况:例如感冒蚂蚁向右,如果右边向左的蚂蚁数为0,则不会有其他蚂蚁被传染
    if(a[0] > 0 && l == 0 || a[0] < 0 && r == 0)
        cout << 1 << endl;
    else
        cout << l + r + 1 << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zy200128/p/12673944.html