PAT乙级 1103 缘分数

PAT乙级 1103 缘分数

题目

所谓缘分数是指这样一对正整数 a 和 b,其中 a 和它的小弟 a−1 的立方差正好是另一个整数 c 的平方,而 c 正好是 b 和它的小弟 b−1 的平方和。例如 83−73=169=132,而 13=32+22,于是 8 和 3 就是一对缘分数。

给定 a 所在的区间 [m,n],是否存在缘分数?

输入格式:

输入给出区间的两个端点 0<m<n≤25000,其间以空格分隔。

输出格式:

按照 a 从小到大的顺序,每行输出一对缘分数,数字间以空格分隔。如果无解,则输出 No Solution。

输入样例 1:

8 200

输出样例 1:

8 3
105 10

输入样例 2:

9 100

输出样例 2:

No Solution

题解

#include <bits/stdc++.h>

int main() {
    int m, n;
    std::cin >> m >> n;
    bool found = false;
    for(int a = m; a <= n; a++) {
        int num = std::pow(a, 3) - std::pow(a - 1, 3);
        int c = std::sqrt(num);
        if(a != c && c * c == num) {
            for(int b = 1; b <= c; b++) {
                if(std::pow(b, 2) + std::pow(b - 1, 2) == c) {
                    found = true;
                    std::cout << a << " " << b << std::endl;
                    break;
                }
            }
        }
    }
    if(!found) std::cout << "No Solution" << std::endl;

    return 0;
}
### 关于 PAT 乙级真题解析 以下是针对您提到的几道 PAT 乙级真题的具体解答和思路: --- #### 题目 1040:统计字符串中的 PAT 数量 此题的核心在于找到所有可能组成单词 "PAT" 的字符组合。通过遍历输入字符串,记录 P 和 A 出现的位置,并计算 T 后面有多少种合法的 PA 组合。 ```cpp #include <iostream> #include <vector> using namespace std; int main() { string s; cin >> s; long long count_P = 0, count_PA = 0, result = 0; // 使用 long long 避免溢出 for (char c : s) { if (c == 'P') { count_P++; } else if (c == 'A') { count_PA += count_P; } else if (c == 'T') { result += count_PA; } } cout << result % 1000000007; // 取模防止过大 return 0; } ``` 上述代码实现了动态规划的思想[^1],逐步累积符合条件的子序列数量。 --- #### 题目 1026:程序运行时间 该问题主要涉及浮点数处理以及四舍五入操作。可以通过简单的数学运算实现精确的结果转换。 ```cpp #include <cstdio> int main(){ double timeInSeconds; scanf("%lf", &timeInSeconds); printf("%.0f\n", (timeInSeconds / 3600.0 + 0.5)); // 加上偏移量完成四舍五入 return 0; } ``` 这里采用了 `(double + 0.5)` 技巧来简化逻辑[^2],从而达到标准输出的要求。 --- #### 题目 1032:挖掘机组队比赛成绩汇总 这是一道典型的分数累加与比较题目。需要读取多组数据并维护每所学校的成绩表,最后找出最高分对应的学校编号。 ```cpp #include <bits/stdc++.h> using namespace std; struct SchoolScore{ int id; int score; }; bool cmp(const SchoolScore& a,const SchoolScore& b){ return a.score > b.score; } int main(){ vector<SchoolScore> schools(101,{0,0}); // 假设最多有100所不同学校参与竞赛 int n,m,tempId,tempScore; cin>>n>>m; while(m--){ cin>>tempScore>>tempId; schools[tempId].id=tempId; schools[tempId].score+=tempScore; } sort(schools.begin(),schools.end(),cmp); cout<<schools[0].id<<" "<<schools[0].score; return 0; } ``` 这段代码利用结构体存储信息,并借助 STL 排序功能快速定位最优解[^3]。 --- #### 题目 1029:检测损坏按键 本题要求识别哪些字母无法正常打印出来。可以采用集合去重的方式收集异常字符集。 ```cpp #include<stdio.h> #define MAX_LEN 1000 void findBrokenKeys(char* typed,char* expected){ char brokenSet[MAX_LEN]={}; bool isUpper=false; for(int i=0;i<strlen(typed)||i<strlen(expected);i++){ if(i>=strlen(typed)){ addIfNotExists(brokenSet,toupper(expected[i])); } else if(i>=strlen(expected)){ addIfNotExists(brokenSet,toupper(typed[i])); } else if(typed[i]!=expected[i]){ addIfNotExists(brokenSet,toupper(typed[i])); addIfNotExists(brokenSet,toupper(expected[i])); } } printUniqueChars(brokenSet); } // 辅助函数定义省略... ``` 以上片段展示了如何逐字对比两串文本差异之处[^4],并通过辅助方法整理最终答案。 --- ### 总结 这些示例覆盖了多种基础算法和技术要点,包括但不限于字符串匹配、数值变换、数组管理及错误排查等领域知识点。希望它们能帮助理解相关概念并提升实战能力!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

polarours

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

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

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

打赏作者

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

抵扣说明:

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

余额充值