声明了n个循环,却循环了未知次数

P1957 口算练习题

Dolkx @ 2022-10-03 21:21:57

代码如下

#include <stdio.h>
#include<string>
#include<iostream>
int main() {
  int n = 0;
  char d,c[2]; //临时变量判断符号
  scanf("%d",&n);
  for (int i = 1; i <= n; i++) {
    //printf("8888\n");
    scanf("%s",c);
    //printf("%c\n",c);
    int a = 0, b = 0;
    if (c[0] >= 'a' && c[0] <= 'z') {
      scanf(" %d %d", &a, &b);
      d = c[0];
    } else {
    //可将字符转化为整型
     std::sscanf(c,"%d",&a);
      scanf("%d",&b);
    }
    printf("%d %d\n",a,b);
  }

by Wrong_Time_Limit @ 2022-10-03 21:29:45

少打了个 “{” 吧,在末尾


by Dolkx @ 2022-10-05 13:09:32

@Wrong_time_long 并不行


by Wrong_Time_Limit @ 2022-10-05 13:35:38

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n = 0;
    char d; string c;
    cin >> n;
    while (n--) {
        //printf("8888\n");
        cin >> c;
        //printf("%c\n",c);
        int a = 0, b = 0;
        if (c[0] >= 'a' && c[0] <= 'z') {
            cin >> a >> b;
            d = c[0];
        }
        else {
            //可将字符转化为整型
            stringstream ss(c);
            ss >> a;
            cin >> b;
        }
        cout << a << b;
    }
    return 0;
}

这样解决了循环次数问题


by Wrong_Time_Limit @ 2022-10-05 13:36:28

不保证正确性


by Wrong_Time_Limit @ 2022-10-05 13:37:04

@Dolkx


by Dolkx @ 2022-10-05 14:58:20

@Wrong_time_long 改用fgets就没啥问题了


by Dolkx @ 2022-10-05 14:58:53

@Wrong_time_long 还有stringstream ss(c);这个函数是在哪里学到的呢?有没有什么参考书推荐


by Wrong_Time_Limit @ 2022-10-05 19:38:33

像这种比较偏的函数一般扒网比找书好


by Wrong_Time_Limit @ 2022-10-05 19:38:46


|