最大质因子

#include <iostream>
#include <cstdio>
using namespace std;
int s=0;
int f(int x){
    int i=2,res=1;//因为x可能为1.。。。
    while (x>1)
    {
        if(x%i==0){
            //这里实际上是使劲除,除干净的方式,因为i没有++
            x=x/i;
            res=i;
            s++;
        }else {
            i++;
            s=0;
        }
    }
    return res;
}
int main(){
    int x;
    while (cin>>x)
    {
        int res=f(x);//这里是一个坑
        //因为printf会先确定变量然后在调用函数,所以不能printf("%d ,%d ,%d
",x,f(x),s);,这样s会没有修改
        printf("%d ,%d ,%d
",x,res,s);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/MorrowWind/p/13056593.html