求最大因数
链接:http://1.14.136.201/problem.php?cid=1166&pid=8
时间限制:1.000s 内存限制:16MB
输入一个整数n(0<n<1000000),求除1和它本身以外最大的因数,如果没有输出"n"。
输入格式
一个整数
输出格式
最大因数或n
样例输入
21
样例输出
7
只用main函数就可以。
我的错误代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long x;
cin>>x;
long long i = x/2;
for(i; i >1; i--)
{
if(x%i == 0)break;
}
if ((i==0)) {
cout<<x;
}
else if((i==1)) {
cout<<x;
}
else if((i==x)) {
cout<<x;
}
else {
cout<<i;
}
return 0;
}