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);
}
}