你要不试试改成+a+b这种形式,然后判断+1的加号不输出
加一个判断,就是后面的数之和不等于前面那个数时才输出加号
#include<iostream>
using namespace std;
int main(){
int m;
int s = 0;
for(m = 2; m <= 1000; m++){
for (int i = 1; i <= m/2; i++) // 求所有因数的和
if (m % i == 0)
s += i;
if (s == m){
cout << m << "=";
for(int i = 1; i <= m/2; i++){
if (m % i == 0){
cout << i;
if (i != m / 2)
cout << "+";
}
}
cout << endl;
}
s = 0;
}
}
换个思路,输出第1个数的时候cout<<i
输出第2个到最后 一个数的时候cout<<"+"<<i