格式控制符设为 %+d
即可
#include <bits/stdc++.h>
using namespace std;
int main() {
printf("%+d\n", -1);
printf("%+d\n", 0);
printf("%+d\n", 1);
cout << showpos << -1 << endl; // noshowpos 可以取消显示正号
cout << showpos << 0 << endl;
cout << showpos << 1 << endl;
return 0;
}
输出:
-1
+0
+1
-1
+0
+1