2 solutions

  • -1
    @ 2024-6-11 14:10:18

    模拟(60pts)

    • 枚举2到n−1n-1的数ii,如果ii是nn的因子,那么就更新答案,最后从小到大去枚举,最后一次遇到的ii就是最大的质数。
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        cin>>n;
        int res=0;
        for(int i=2;i<n;i++) //从2到n枚举因子
        {
            if(n%i==0)
            {
                res=i;  //更新答案
            }
        }
        cout<<res;
        return 0;
    }
    

    模拟优化(100pts)

    • 我们知道因子一定是成对出现的,那么最后出现的一个前一定在前面就已经出现过更小的,且我们可以通过的小的因子算出大的因子。
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        cin>>n;
        for(int i=2;i<=n/i;i++) //枚举小因子
        {
            if(n%i==0) //找到小因子
            {
                cout<<n/i; //找到大因子
                return 0;
            }
        }
        return 0;
    }
    
    • -2
      @ 2024-7-19 13:52:11
      #include<bits/stdc++.h>
      using namespace std;
      int main(){
      	int n;
      	cin>>n;
      	for(int i=2;i<=sqrt(n);i++){
      		if(n%i==0){
      			cout<<n/i<<endl;
      			break;
      		}
      	} 
      	
      	return 0;
      }
      
      • 1

      Information

      ID
      437
      Time
      1000ms
      Memory
      128MiB
      Difficulty
      1
      Tags
      # Submissions
      24
      Accepted
      14
      Uploaded By