像素个数 = 长 * 宽
所以:长 , 宽 为像素个数 n 的约数
约数是成对出现的
比如 8 = 1 * 8 , 8 = 2 * 4
分析可知 约数对的重复出现在 <= 一个数的平方根
#include<iostream> #include<cmath> using namespace std; int main(){ int n;cin>>n; int cnt = floor(sqrt(n));//获得最中间的约数 //从最中间开始寻找宽 while(1){ if(n % cnt == 0) break; cnt--; } //长可根据宽求出 cout<<cnt<<" "<<n/cnt; return 0; }