PAT 天梯赛 L1-054. 福到了 【字符串】

本文介绍了一种通过字符串数组输入并进行反转判断的方法。利用C++实现字符串的反转及比较,若所有行均相同则输出特定信息,并最终按指定字符打印反转后的字符串。

题目链接

https://www.patest.cn/contests/gplt/L1-054

思路
可以先将字符串用字符串数组 输入
然后用另一个字符串数组 从 n - 1 -> 0 保存 其反转的字符串

然后每一行比较一下 这两个字符串数组有没有什么不同 如果没有 就要输出 bu yong dao le

然后最后 输出“到了” 的字符串 注意 字符替换

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const double PI  = 3.14159265358979323846264338327;
const double E   = exp(1);
const double eps = 1e-6;

const int INF  = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
const int MOD  = 1e9 + 7;

string in[maxn], tran[maxn];

string Reverse(string s)
{
    string ans = "";
    int len = s.size();
    for (int i = len - 1; i >= 0; i--)
        ans += s[i];
    return ans;
}

int main()
{
    char c;
    int n;
    scanf(" %c%d", &c, &n);
    getchar();
    for (int i = 0; i < n; i++)
    {
        getline(cin, in[i]);
        tran[n - 1 - i] = Reverse(in[i]);
    }
    int flag = 1;
    for (int i = 0; i < n; i++)
    {
        if (in[i] != tran[i])
        {
            flag = 0;
            break;
        }
    }
    if (flag)
        printf("bu yong dao le\n");
    for (int i = 0; i < n; i++)
    {
        int len = tran[i].size();
        for (int j = 0; j < len; j++)
        {
            if (tran[i][j] != ' ')
                printf("%c", c);
            else
                printf(" ");
        }
        printf("\n");
    }
}
### 关于 PAT 天梯赛 L1-006 连续因子的解题思路 此问题的核心在于找到给定正整数 \( N \) 的最长连续因子序列,并按照题目要求输出其长度以及具体的连续因子表达形式。以下是详细的解析: #### 问题分析 对于任意正整数 \( N \),我们需要找出一组连续的因子,使得它们的乘积等于 \( N \) 并且该组因子的数量尽可能多。如果存在多个满足条件的连续因子组合,则应返回起始数值最小的那一组。 为了高效解决这一问题,可以采用如下方法: - **枚举起点**:从可能的小因数开始逐一尝试作为连续因子区间的左端点。 - **逐步扩展区间右边界**:计算当前范围内所有数相乘的结果是否能被原数\( N \) 整除。一旦发现无法继续延伸或者超出目标值范围就停止对该特定起点进一步探索。 - **记录最佳方案**:在整个过程中保持跟踪所遇到的最大合法连续因子集合及其对应的信息以便最终输出。 #### 示例代码实现 (Python 版本) 下面提供了一个基于上述逻辑编写的 Python 实现版本: ```python def find_longest_consecutive_factors(N): max_len = 0 best_seq = [] # 枚举可能成为连续因子序列的第一个成员的位置i for i in range(2, int(N**(1/2)) + 2): product = 1 seq = [] j = i while True: if j > N or product * j > N: break product *= j seq.append(j) if N % product == 0 and len(seq) >= max_len: max_len = len(seq) best_seq = seq[:] j += 1 return max_len, "*".join(map(str,best_seq)) if __name__ == "__main__": import sys try: input_line = sys.stdin.read().strip() number = int(input_line) length, factors_str = find_longest_consecutive_factors(number) print(length) print(factors_str) except Exception as e: pass ``` 以上程序通过双重循环结构实现了对每一个潜在初始位置的有效探测,并利用列表存储临时结果,在每次更新更优解答时同步刷新全局最优状态变量 `max_len` 和 `best_seq`. --- #### 注意事项与特殊情形处理 需要注意的是当输入为质数时,由于不存在任何大于一且小于自身的其他自然数能够将其完全分解成两个以上的连贯部分之积的形式,因此在这种情况下应当单独考虑并直接返回单一元素构成的答案即该质本身[^1]。 另外还需留意某些极端情况下的性能表现优化措施比如提前终止不必要的迭代操作等策略的应用价值所在之处[^3]. ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值