ACwing(基础)--- 质数和分解质因数

时间:2020-07-06 16:08:53   收藏:0   阅读:97

质数

质数的判定

bool isprime(int x){
	if(x < 2) return 0;
	for(int i=2;i <= x/i;i++){
		if(x%i==0)
		return false;
	}
	return true;
}

分解质因数

void divide(int x)
{
    for (int i = 2; i <= x / i; i ++ )
        if (x % i == 0)
        {
            int s = 0;
            while (x % i == 0) x /= i, s ++ ;
            cout << i << ‘ ‘ << s << endl;
        }
    if (x > 1) cout << x << ‘ ‘ << 1 << endl;
    cout << endl;
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!