完美世界实习生招聘 C++

1.时间转换小工具

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void get_time(int *hour, int *min, int *second, int const time_diff_second)
{
    // implement your code here
    if (time_diff_second > 0) {
        *second += time_diff_second;
        *min = *min + (*second) / 60;
        *second = (*second) % 60;
        *hour = *hour + (*min) / 60;
        *min = (*min) % 60;
        *hour = (*hour) % 60;
    }
    else {
        *second += time_diff_second;
        if (*second < 0) {
            int tem = abs(*second / 60);
            *second = (*second % 60);
            if (*second < 0) {
                *second += 60;
                tem++;
            }

            *min -= tem;
            if (*min < 0) {
                tem = abs(*min / 60);
                *min = (*min % 60);
                if (*min < 0) {
                    *min += 60;
                    tem++;
                }
                *hour -= tem;
                *hour = (*hour) % 24;
                if (*hour < 0) {
                    *hour += 24;
                }
            }
        }
    }
}

int main()
{
    int hour, min, second, time_diff_second;

    while (scanf("%d:%d:%d,%d", &hour, &min, &second, &time_diff_second) != EOF) {

        get_time(&hour, &min, &second, time_diff_second);
        printf("%02d %02d %02d\n", hour, min, second);
    }
}

2.弹球游戏

#include <stdio.h>
int ans[100];
int ans_num;
int heng, shu;
int all;
//上:1,下:2,左:3,右:4
void dfs(int w, int h, int x, int index, int pos, int heng,int shu) {

    if (ans_num >= all)
        return;
    if (pos == 1 || pos == 2) {
        shu = 0 - shu;
    }
    else {
        heng = 0 - heng;
    }
    if (pos == 2 && index != 0) {
        if (ans_num == 0)
            printf("%d", x);
        else
            printf(" %d", x);
        ans_num++;
        if (ans_num >= all) {
            printf("\n");
            return;
        }
    }
    if (pos == 1) {
        if (heng > 0) {
            if (w - x > h) {
                dfs(w, h, x + h, index + 1, 2, heng, shu);
            }
            else {
                dfs(w, h, h - (w - x), index + 1, 4, heng, shu);
            }
        }
        else {
            if (x > h) {
                dfs(w, h, x - h, index + 1, 2, heng, shu);
            }
            else {
                dfs(w, h, h - x, index + 1, 3, heng, shu);
            }
        }
    }
    else if (pos == 2) {
        if (heng > 0) {
            if (w - x > h) {
                dfs(w, h, x + h, index + 1, 1, heng, shu);
            }
            else {
                dfs(w, h, w - x, index + 1, 4, heng, shu);
            }
        }
        else {
            if (x > h) {
                dfs(w, h, x - h, index + 1, 1, heng, shu);
            }
            else {
                dfs(w, h, x, index + 1, 3, heng, shu);
            }
        }
    }
    else if (pos == 3) {
        if (shu > 0) {
            if (h - x > w) {
                dfs(w, h, x + w, index + 1, 4, heng, shu);
            }
            else {
                dfs(w, h, h - x, index + 1, 1, heng, shu);
            }
        }
        else {
            if (x > w) {
                dfs(w, h, x - w, index + 1, 4, heng, shu);
            }
            else {
                dfs(w, h, x, index + 1, 2, heng, shu);
            }
        }
    }
    else {
        if (shu > 0) {
            if (h - x > w) {
                dfs(w, h, x + w, index + 1, 3, heng, shu);
            }
            else {
                dfs(w, h, w - (h - x), index + 1, 1, heng, shu);
            }
        }
        else {
            if (x > w) {
                dfs(w, h, x - w, index + 1, 3, heng, shu);
            }
            else {
                dfs(w, h, w - x, index + 1, 2, heng, shu);
            }
        }
    }
}
void ball(int w, int h, int x, int n)
{
    all = n;
    ans_num = 0;
    dfs(w, h, x, 0, 2, 1,-1);
}

int main()
{
    int w, h, x, n;
    while (scanf("%d%d%d%d", &w, &h, &x, &n) != EOF) {
        ball(w, h, x, n);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值