elysia_nice 2023-02-23 10:29 采纳率: 82.4%
浏览 10
已结题

Uva 536 关于输入的判定


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<sstream>
#include<istream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 50;
int pre[maxn], ino[maxn];
int lch[maxn], rch[maxn];
int n;
bool read(int* a) {
    n = 0;
    string line;
    **if (!(cin >> line))return false;**
    stringstream ss(line);
    char x = 0;
    while (ss >> x)a[n++] = (int)x-64;
    while (n > 0)return true;/*检测末尾*/
}
int build(int L1, int R1, int L2, int R2) {
    if (L2 > R2)return 0;
    int root = pre[L1];
    int p=L2;
    while (ino[p] != root)p++;/*cnt统计左子树的个数,此处少一*/
    int cnt = p - L2;
    lch[root] = build(L1 +1, L1 + cnt, L2, p - 1);/*范围按各个遍历的顺序来决定*/
    rch[root] = build(L1 + cnt+1, R1, p + 1, R2);
    return root;
}
void dfs(int root) {
    if (lch[root])dfs(lch[root]);
    if (rch[root])dfs(rch[root]);
    printf("%c", root + 64);
}
int main() {
    while (read(pre)) {
        read(ino);
        build(0, n - 1, 0, n - 1);
        dfs(pre[0]);
        printf("\n");
    }
    return 0;
}

如上有两个星号包围的语句,如果我不加以if进行判定的话,本来ac的代码会runtime error,这是怎么一个情况?

  • 写回答

1条回答 默认 最新

  • MarkHan_ 2023-02-23 12:41
    关注

    该回答引用GPTᴼᴾᴱᴺᴬᴵ
    这是因为如果输入流(cin)遇到文件结束符或输入错误,就会设置一个错误标志,之后对输入流的操作将失败,直到清除该错误标志。当发生这种错误时,cin 会将标志位置为 true,而输入操作返回 false。因此,当输入读取失败时,代码需要进行错误处理,以避免对后续操作产生影响。在这里,你使用了一个 stringstream 对象从输入流中读取了数据,所以要检查 stringstream 对象读取是否成功。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月10日
  • 已采纳回答 3月2日
  • 修改了问题 2月23日
  • 创建了问题 2月23日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测