3 条题解

  • 2
    @ 2022-7-26 13:42:53

    这就a+b嘛,主要还是在"场宽"的问题上。题解里有setw( )的解法了,我就写个printf的

    格式: printf("%nd",a); 其中n是你要控制的场宽长度,默认右对齐,要左对齐改成负的.

    还是演示下好了owo

    a=1,b=22,c=333

    printf("%4d",a);

    printf("%4d",b);

    printf("%4d",c);

    输出(-代表空格)


    1--22-333

    printf("%-4d",a);

    printf("%-4d",b);

    printf("%-4d",c);

    输出(-代表空格)

    1---22--333-

    就这么简单~

    最后附上代码: !不要直接复制粘贴! 这个菜狗写成这样

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	int a,b;
    	cin>>a>>b;
    	cout<<a<<'+'<<b<<'='<<a+b<<endl;
    	printf("%7d",a);
    	cout<<endl<<'+';
    	printf("%6d",b);
    	cout<<endl<<"-------"<<endl;
    	printf("%7d",a+b);
    	return 0;
    }
    
    • 2
      @ 2022-5-28 15:27:08
      using namespace std;
      int main()
      {
        int d,f;
        cin>>d>>f;
        cout<<d<<"+"<<f<<"="<<d+f<<endl;
        cout<<setw(7)<<d<<endl;
        cout<<"+"<<setw(6)<<f<<endl;
        cout<<"-------"<<endl;
        cout<<setw(7)<<d+f;
        return 0;
      }
      

      setw()用于控制输出之间的间隔

      博客:https://blog.csdn.net/MaNong125/article/details/121071712

      有一说一这个格式还是很恶心的,我开始都想到判断这个数有几位再分别判断减空格了(=。=)

      • 0
        @ 2022-7-20 19:07:13

        #include <iostream> #include <iomanip>

        using namespace std;

        int main() { int a,b; cin >> a >> b; cout << a <<"+"<< b <<"="<< a + b << endl; cout << setw(7) <<a<<endl; cout <<"+"<< setw(6)<<b<<endl; cout << "-------"<<endl; cout << setw(7) << a+b << endl; return 0; }

        • 1

        信息

        ID
        15
        时间
        1000ms
        内存
        256MiB
        难度
        5
        标签
        递交数
        195
        已通过
        70
        上传者