3 条题解

  • 3
    @ 2023-5-21 15:55:41

    说明: 考查输出格式控制。

    思路分析: 根据题意,按顺序写代码即可,关键是输出格式的控制。

    易错点: C++的setw和C语言的printf,对输出结果默认是右对齐。刚好适合本题,直接使用即可。

    题目对输出结果要求相邻两个变量之间要用一个空格可开,所以在按8个字符的宽度进行输出时, 不要忘了变量之间的空格。

    参考代码 - C++:

    #include <iostream> 
    #include <iomanip> 
    using namespace std;
    int main()
    {
        int a, b, c;
        cin >> a >> b >> c;
        // 可以三行合并为一行写完,不过太长了 
        cout << setw(8) << a << ' ';
        cout << setw(8) << b << ' ';
        cout << setw(8) << c;
        return 0;
    }
    

    参考代码 - C语言:

    #include <stdio.h> 
    int main()
    {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        printf("%8d %8d %8d", a, b, c);
        return 0;
    }
    
    
    
    • @ 2023-8-29 11:25:35

      建议用long long,因为题目数据限制 -2^31 ≤a,b,c< -2^31

    • @ 2023-10-14 16:52:28

      @ 题目限制的数值刚好在int的数值范围内

    • @ 2024-2-3 22:50:26

      @ int 范围-2^32(包括-2^32) --- 2^32 - 1

  • 1
    @ 2023-5-20 14:36:14

    解析

    相信大家在使用C++\text{C++}时一定被输出如何设置宽度、如何用特殊符号占位困扰过,以至于在遇见类似问题时不得不用#include< cstdio >这个开头文件,去使用printf来解决问题。但其实C++中有这样一个函数可以解决问题的。

    setw(n)函数是C++中关于在输出操作中使用的字段宽度设置函数,其中nn表示字段宽度,而它的开头文件一定要记住哦!!

    #include<iomanip>

    注意aabbcc的数据范围,这个非常重要!!!

    参考代码:

    #include<bits/stdc++.h>
    #include<iomanip>
    using namespace std;
    int main()
    {
        long long a,b,c;
        cin>>a>>b>>c;
        cout<<setw(8)<<a<<" ";
        cout<<setw(8)<<b<<" ";
        cout<<setw(8)<<c;
        return 0;
    }
    
    • -1
      @ 2024-5-2 18:47:41
      #include <iostream>
      #include <iomanip>
      using namespace std;
      int main()
      {
      long long a,b,c;
      cin >> a >> b >> c;
      cout <<setw(8)<<a<<" ";
      cout << setw(8)<<b<<" ";
      cout <<setw(8)<<c;
      return 0;
      }
      

      #include<iomanip这是它的头文件名

      setw(n)函数是C++中关于在输出操作中使用的字段宽度设置函数,其中n表示字段宽度

      • 1

      信息

      ID
      102
      时间
      1000ms
      内存
      128MiB
      难度
      5
      标签
      递交数
      328
      已通过
      131
      上传者