HDU1065——I Think I Need a Houseboat,HDU1066——Last non-zero Digit in N!,HDU1067——Gap

HDU1065——I Think I Need a Houseboat

题目描述

Problem - 1065

运行代码

#include <iostream>
#define PI 3.1415926

int main() {
    int cnt;
    std::cin >> cnt;
    for (int i = 0; i < cnt; ++i) {
        double x, y;
        std::cin >> x >> y;
        double area = PI * (x * x + y * y) / 2;
        int year = static_cast<int>(area / 50 + 1);
        std::cout << "Property " << i + 1 << ": This property will begin eroding in year " << year << ".\n";
    }
    std::cout << "END OF OUTPUT.\n";
    return 0;
}

代码思路

  1. 首先定义了一些变量:n 表示输入的测试用例数量,z 、m 、i 用于循环和计数,x 和 y 用于存储输入的坐标,r2 和 d2 用于计算和比较。

  2. 在外部的 while 循环中,通过 ~scanf_s("%d", &n) 读取测试用例的数量。

  3. 对于每个测试用例(内部的 while 循环):

    • 变量 i 自增,表示当前处理的是第 i 个房产。
    • 读取房产的坐标 x 和 y 。
    • 计算坐标的平方和并存入 d2 。
    • 然后通过一个内层的 for 循环从 1 开始递增 z ,计算 r2 = z * s / pi ,直到 r2 大于 d2 。
    • 此时找到了房产开始被侵蚀的年份 z ,并输出相应的信息。
  4. 当所有测试用例处理完毕,输出 "END OF OUTPUT." 表示输出结束。

HDU1066——Last non-zero Digit in N!

题目描述

Problem - 1066

运行代码

#include <cstdio>
#include <cstring>
const int MAXN = 1e3 + 10;
int lst[] = {1, 1, 2, 6, 4, 2, 2, 4, 2, 8};
int lst1[] = {3, 3, 0, 3, 1, 1, 1, 2, 1, 3};
int lst2[] = {2, 4, 8, 6};
int num[MAXN], len;
char str[MAXN];
int solve(){
    if (len == 1) return lst[num[0]];
    int tail = num[0], c = 0;
    for (int i = len - 1; i >= 0; i--){
        c = c * 10 + num[i];
        num[i] = c / 5;
        c %= 5;
    }
    while (!num[len-1]) len--;
    int k = (num[1] * 10 + num[0]) & 3;
    return lst2[(lst1[tail] - k + 4) & 3] * solve() % 10;
}
int main(){
    while (~scanf("%s",str)){
        len = strlen(str);
        for (int i = 0; i < len; i++)
            num[i] = str[len - i - 1] - '0';
        printf("%d\n",solve());
    }
}

代码思路

  1. 定义了一些常量和数组:

    • MAXN 定义了一个较大的整数常量,用于限制一些操作的范围。
    • lst 数组存储了个位数阶乘的最后一位非零数字。
    • lst1 和 lst2 数组可能用于后续的计算。
    • num 数组用于存储输入数字的每一位,len 记录数字的长度,str 用于接收输入的字符串形式的数字。
  2. solve 函数用于计算输入数字阶乘的最后一位非零数字:

    • 如果数字长度为 1,直接返回 lst 数组中对应位置的数字。
    • 通过一个循环从后往前处理数字,进行除以 5 的操作,并更新数字和余数。
    • 去掉前面的 0 。
    • 计算一个 k 值,用于后续的计算。
    • 通过递归调用 solve 函数,并结合一些计算得到最终结果。
  3. main 函数:

    • 不断接收输入的字符串形式的数字。
    • 将字符串转换为数字存储在 num 数组中,并获取数字长度。
    • 调用 solve 函数计算结果并输出。

HDU1067——Gap

题目描述

Problem - 1067

运行代码

#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;
const int goal[32] = { 11, 12, 13, 14, 15, 16, 17, 0, 21, 22, 23, 24, 25, 26, 27, 0, 31, 32, 33, 34, 35, 36, 37, 0, 41, 42, 43, 44, 45, 46, 47, 0 };
const int MAXN = 110;
struct ABC {
    pair<int, int> po[50];
    pair<int, int> kong[5];
    int dis;
    string s;
};
char chushi[5][10];
string sgoal, ts;
map<string, int> dic;
queue<ABC> dl;

bool bfs() {
    dic.clear();
    while (!dl.empty()) dl.pop();

    ABC pre;
    pre.dis = 0;
    pre.s = " ";
    int num = 0;

    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 8; j++) {
            pre.s += chushi[i][j];
            if (chushi[i][j] == 0) {
                pre.kong[++num] = { i, j };
            }
            else {
                pre.po[int(chushi[i][j])] = { i, j };
            }
        }
    }

    dic[pre.s] = 1;

    if (dic[sgoal]) {
        cout << "0" << endl;
        return true;
    }

    dl.push(pre);

    while (!dl.empty()) {
        ABC temp = dl.front();
        dl.pop();

        for (int i = 1; i <= 4; i++) {
            ABC t = temp;
            int tx = t.kong[i].first, ty = t.kong[i].second;
            int sz = (tx - 1) * 8 + ty;
            int judge = t.s[sz - 1];

            if (judge == 0 || judge % 10 == 7) continue;

            int tx1 = t.po[judge + 1].first, ty1 = t.po[judge + 1].second;
            int sz1 = (tx1 - 1) * 8 + ty1;

            swap(t.s[sz], t.s[sz1]);

            if (!dic[t.s]) {
                dic[t.s] = 1;
                t.kong[i] = { tx1, ty1 };
                t.po[judge + 1] = { tx, ty };
                t.dis++;

                if (dic[sgoal]) {
                    cout << t.dis << endl;
                    return true;
                }

                dl.push(t);
            }
        }
    }

    return false;
}

int main() {
    sgoal = " ";
    for (int i = 0; i <= 31; i++) {
        sgoal += goal[i];
    }

    int T;
    cin >> T;
    while (T--) {
        memset(chushi, 0, sizeof(chushi));
        for (int i = 1; i <= 4; i++) {
            for (int j = 2; j <= 8; j++) {
                int x;
                cin >> x;
                chushi[i][j] = x;
                if (x % 10 == 1) {
                    swap(chushi[x / 10][1], chushi[i][j]);
                }
            }
        }
        if (!bfs()) {
            cout << "-1" << endl;
        }
    }
    return 0;
}

代码思路

  1. 首先定义了一些常量和数据结构:

    • goal 数组表示最终的目标状态。
    • ABC 结构体用于表示游戏的状态,包括每个数字的位置、空位位置、移动次数和状态字符串。
  2. bfs 函数是广度优先搜索的主体:

    • 初始化时,将字典清空,队列清空,并构建初始状态的 ABC 对象 pre 。
    • 如果初始状态就是目标状态,直接输出 0 并返回 true 。
    • 将初始状态放入队列和字典。
    • 当队列不为空时,取出队头元素进行处理:对于每个空位,尝试移动其左侧相邻非零且非 7 的数字过来。如果得到的新状态未在字典中出现,将其放入字典、更新相关信息(空位位置、数字位置、移动次数),如果是目标状态则输出移动次数并返回 true ,否则将其放入队列。
  3. 在 main 函数中:

    • 构建目标状态字符串 sgoal 。
    • 读入测试用例数量 T 。
    • 对于每个测试用例,初始化棋盘状态,然后调用 bfs 函数进行搜索。如果无法达到目标状态,输出 -1 。
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

筱姌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值