编辑代码

#include <iostream>
using namespace std;

int divideToSquare(int length, int width) {
    int nextWidth = 0;
    if (length > width) {
        nextWidth = length % width;
        length = width;
        width = nextWidth;
    }
    else {
        nextWidth = width % length;
        width = nextWidth;
    }

    if (width == 0) {
        return length;
    }
    else {
        return divideToSquare(length, width);
    }
}
int main() {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
	cout << "The width of square for rectangle(1680, 640) is " << divideToSquare(1680, 640) << endl;
    cout << "The width of square for rectangle(640, 1680) is " << divideToSquare(640, 1680) << endl;
    cout << "The width of square for rectangle(640, 1680) is " << divideToSquare(80, 40) << endl;
    cout << "The width of square for rectangle(640, 1680) is " << divideToSquare(18, 3) << endl;
	return 0;
}