码迷,mamicode.com
首页 > 其他好文 > 详细

哈理工热身赛

时间:2015-04-05 10:34:25      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

2238.

题目连接:点击打开链接


解题思路:

并查集判断下是否成环即可


完整代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n , m;
bool flag;
const int maxn = 10001;
int f[maxn];

void init()
{
    for(int i = 0 ; i < maxn ; i ++)
        f[i] = i;
}

int find(int x)
{
    return f[x] = f[x] == x ? x : find(f[x]);
}

void solve(int a , int b)
{
    int x = find(a);
    int y = find(b);
    if(x == y)
        flag = true;
    else
        f[x] = y;
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt" , "r" , stdin);
    #endif
    while(cin >> n >> m)
    {
        init();
        int a , b;
        flag = false;
        for(int i = 0 ; i < m ; i ++)
        {
            cin >> a >> b;
            solve(a , b);
        }
        if(flag)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
}

2236.

题目连接:点击打开链接


解题思路:

遍历数组,取起始点与障碍物差的最小值即可


完整代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <complex>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0); //M_PI;
int n , x , y;
string s;
char g[101][101];
int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif

    while(cin >> n >> x >> y)
    {
        for(int i = 1 ; i <= n ; i ++)
        {
            cin >> s;
            for(int j = 1 ; j <= n ; j ++)
                g[i][j] = s[j-1];
        }

        int minn = INF;
        for(int i = 1 ; i <= n ; i ++)
        {
            for(int j = 1 ; j <= n ; j ++)
            {
                if(g[i][j] == '#')
                {
                    int k = abs(i - x) + abs(j - y);
                    if(k < minn)
                        minn = k;
                }
            }
        }
        cout << minn + 1 << endl;
    }
    return 0;
}

2235.

题目连接:点击打开链接


解题思路:

先求出各个毒药的毒性,然后从大到小排序,依次混合即可,这是一种贪心的解法。


完整代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n , m , k;
const int maxn = 1000001;
double f[maxn];

bool cmp(double a , double b)
{
    return a > b;
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt" , "r" , stdin);
    #endif
    int T;
    cin >> T;
    while(T--)
    {
        cin >> n >> k >> m;
        f[1] = 1.0;
        for(int i = 2 ; i <= n ; i ++)
        {
            f[i] = f[i-1] + ((i-1) * k) % m;
        }
        sort(f + 1 , f + n + 1 , cmp);
        double sum = 2.0 * sqrt(f[1] * f[2]);
        for(int i = 3 ; i <= n ; i ++)
        {
            sum = 2 * sqrt( sum * f[i]);
        }
        printf("%.3lf\n" , sum);
    }
}

2234.

题目连接:点击打开链接


解题思路:

如果p == q,那么一张就可以;如果p>q,那么需要q张;如果p < q,那么我们去暴力计算;此题最坑的是当p == 1的情况,这种情况下我们需要q张!!!

完整代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = 1000001;
LL p , q;
LL f[maxn];


int deal(int a , int b)
{
    int sum = 1;
    for(int i = 1 ; i <= b ; i ++)
        sum *= a;
    return sum;
}


int main()
{
    #ifdef DoubleQ
    freopen("in.txt" , "r" , stdin);
    #endif
   int T;
   scanf("%d",&T);
   while(T--)
   {
        scanf("%d%d",&p,&q);
        if(p == 1)
        {
            cout << q << endl;
            continue;
        }
        if(q < p)
        {
            printf("%d\n" , q);
        }
        else if(q == p)
        {
            printf("1\n");
        }
        else
        {
            int cnt = 1;
            int temp = q , tot = 0;
            while(temp > 1)
            {
                int key = 1;
                while(key * p <= temp)
                {
                    key *= p;
                }
                temp -= key;
                tot ++;
            }
            if(temp == 1)
                tot ++;
            printf("%d\n" , tot);
        }
   }
}

2233.

题目连接:点击打开链接


解题思路:

暴力的模拟!!!


完整代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
string s;
int page;

const int maxn = 1111;
char g[maxn][maxn];

void f1()
{
    int key = 4;
    for(int k = 1 ; k <= page ; k ++)
    {
        int key = k * 4;
        for(int j = 1 ; j <= 4 ; j ++)
        {
            for(int i = key ; i > key - 4 ; i --)
            {
                cout << g[i][j];
            }
        }
    }
    cout << endl;
}

void f2()
{
    int key = 4;
    for(int k = 1 ; k <= page ; k ++)
    {
        int key = k * 4;
        for(int i = key ; i > key - 4 ; i --)
        {
            for(int j = 4 ; j >= 1 ; j --)
            {
                cout << g[i][j];
            }
        }
    }
    cout << endl;
}


void f3()
{
    int key = -3;
    for(int k = 1 ; k <= page ; k ++)
    {
        key += 4;
        for(int j = 4 ; j >= 1 ; j --)
        {
            for(int i = key ; i < key + 4 ; i ++)
            {
                cout << g[i][j];
            }
        }
    }
    cout << endl;
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt" , "r" , stdin);
    #endif // DoubleQ
    int n;
    while(cin >> n)
    {
        getchar();
        getline(cin , s);
        int len = s.length();
        page = len % 16 == 0 ? len / 16 : len / 16 + 1;
        int cnt = 0;
        for(int i = 1 ; i <= page * 4 ; i ++)
        {
            for(int j = 1 ; j <= 4 ; j ++)
            {
                if(cnt < len)
                    g[i][j] = s[cnt++];
                else
                    g[i][j] = '-';
            }
        }
        if(n == 1)
        {
            f1();
        }
        else if(n == 2)
        {
            f2();
        }
        else if(n == 3)
        {
            f3();
        }
    }
}


哈理工热身赛

标签:

原文地址:http://blog.csdn.net/u013447865/article/details/44874317

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!