3 条题解

  • 1
    @ 2024-4-8 22:16:02
    #include<bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        ll n;
        ll flag;
        ll c;
        cin>>n;
        if(n==1)
        cout<<1;
        if(n==3)//n=3时循环读取不到,因为i从2开始,n至少为4,所以进行特判
        cout<<"YES";
        for(ll i=2;i<=sqrt(n);i++)
        {
            if(n%i==0)
            {
                flag=0;
                c=i;
                break;
            }
            else
            flag=1;
        }
        if(flag==1)
        cout<<"YES";
        if(flag==0)
        cout<<n/c;
        return 0;
    }
    
    • 0
      @ 2025-2-10 13:06:17
      // https://dashoj.com/p/98
      #include <bits/stdc++.h>
      
      using namespace std;
      typedef long long ll;
      
      ll n, ans = -1;
      
      int main() {
      	cin >> n;
      	for (ll i = 2; i * i <= n; i++) if (n % i == 0) ans = max(ans, max(i, n / i));
      	if (ans == -1) cout << "YES" << endl;
      	else cout << ans << endl;
      	return 0;
      }
      
      • 0
        @ 2024-4-9 21:37:23

        #include <iostream> #include <cmath>

        bool isPrime(int n) { if (n <= 1) return false; // 0 和 1 不是质数 if (n <= 3) return true; // 2 和 3 是质数 if (n % 2 == 0 || n % 3 == 0) return false; // 排除能被 2 和 3 整除的数

        // 只需检查到 sqrt(n),因为如果 n 不是质数,它必有一个因子小于或等于 sqrt(n)
        for (int i = 5; i * i <= n; i += 6) {
            if (n % i == 0 || n % (i + 2) == 0) return false;
        }
        return true;
        

        }

        int secondLargestFactor(int n) { int factor = 1; // 首先找到最小的因子(除了 1) for (int i = 2; i <= sqrt(n); ++i) { if (n % i == 0 && n / i > factor) { factor = n / i; } } return factor; }

        int main() { int n; std::cin >> n;

        if (isPrime(n)) {
            std::cout << "YES" << std::endl;
        } else {
            int secondLargest = secondLargestFactor(n);
            std::cout << secondLargest << std::endl;
        }
        
        return 0;
        

        }

        • 1

        信息

        ID
        98
        时间
        1000ms
        内存
        256MiB
        难度
        6
        标签
        递交数
        199
        已通过
        64
        上传者