1 约数

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int root = sqrt(n);
    for(int i = 2; i <= root; i++)
    {
        if(0 == n % i)
        {
            cout << n / i;
            return 0;
        }
    }

    cout << 1;
    return 0;
}

2 阶乘

#include<iostream>
using namespace std;

// zero count from tail of n!
int zeroCnt1(int n)
{
    int cnt = 0;
    while(n)
    {
        n /= 5;
        cnt += n;
    }

    return cnt;
}

// zero count from tail of n!! when n is even
int zeroCnt2(int n)
{
    n /= 10;
    int cnt = n;
    while(n)
    {
        n /= 5;
        cnt += n;
    }

    return cnt;
}

int main()
{
    int n;
    cin >> n;
    cout << zeroCnt1(n) << ' ';

    if(n % 2)
    {
        cout << 0;
    }
    else
    {
        cout << zeroCnt2(n) << endl;
    }

    return 0;
}

3 序列

#include <iostream>
using namespace std;
int a[200005];

int main()
{
    int n, i;
    cin >> n;
    for(i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    for(i = n - 1; i >= 0; i -= 2)
    {
        cout << a[i] << " ";
    }

    if(n % 2)
    {
        i = 1;
    }
    else
    {
        i = 0;
    }

    for(; i < n; i += 2)
    {
        cout << a[i] << " ";
    }

    return 0;
}

4 糖果

#include <iostream>
#include <memory.h>
#include <cstring> 
using namespace std;

char a[10000000 + 5];
inline string read()//inline继续加快速度
{
    // getchar()在数据量较大时比scanf和cin快
    char ch = getchar();
    string res = "";
    while(ch>='A' && ch<='Z')
    {
        res += ch;
        ch = getchar();
    }
    return res;
}

int main()
{
    freopen("candy.in", "r", stdin);
    freopen("candy.out", "w", stdout);

    int cnt[128];
    memset(cnt, 0, sizeof(cnt));

    int start = 0;
    int maxLen = 26;
    int len;
    bool same = false;

    //string a = read();
    scanf("%s", a);
    int Size = strlen(a);

    for(int i = 0; i < Size; i++)
    {
        cnt[a[i] - 65]++;
        if(2 == cnt[a[i] - 65])
        {
            same = true;

            for(int j = start; j < i; j++)
            {
                if(a[j] == a[i])
                {
                    len = i - j;
                    if(len < maxLen)
                    {
                        maxLen = len;
                    }

                    start = j + 1;
                    break;
                }
            }
            cnt[a[i] - 65] = 1;
        }
    }

    if(!same)
    {
        cout << "-1" << endl;
        return 0;
    }

    cout << maxLen << endl;

    return 0;
}

5 迷宫

#include <iostream>
using namespace std;

const int N = 205;
// 4 directions: right, left, down, up
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
int n, m, ans;
char a[N][N];
bool vis[N][N];

void dfs(int x, int y)
{
    for(int dir = 0; dir < 4; dir++)
    {
        int nextX = x + dx[dir];
        int nextY = y + dy[dir];
        if(!vis[nextX][nextY] && nextX >= 1 && nextX <= n && nextY >= 1 && nextY <= m && (a[nextX][nextY] == '.' || a[nextX][nextY] == '*'))
        {
            vis[nextX][nextY] = true;
            if(a[nextX][nextY] == '*')
            {
                ans++;
            }

            dfs(nextX, nextY);
        }
    }
}

int main()
{
freopen("maze.in", "r", stdin);
freopen(“maze.out”, “w”, stdout);
    int startX, startY;
    cin >> n >> m; // n rows m columns
    int row, col;
    for(row = 1; row <= n; row++)
    {
        for(col = 1; col <= m; col++)
        {
            cin >> a[row][col];
            if('S' == a[row][col])
            {
                startX = row;
                startY = col;
            }
        }
    }

    vis[startX][startY] = true;

    dfs(startX, startY);

    cout << ans << endl;

    return 0;
}



posted on 2019-05-14 23:32  Alan_Fire  阅读(720)  评论(0)    收藏  举报