6 solutions

  • 1
    @ 2023-8-15 9:18:58

    先用上题的循环判断是不是能被除,这里能被除说明是因数。是因数之后判断是不是质数。如果是质数的话,则输出较大的那个数,也就是j。

    #include<bits/stdc++.h>
    using namespace std;
    bool zhishu(int x){
    	bool flag = true;
    	int i;
    	for(i=2;i<=sqrt(x);i++){
    		if(x % i == 0){
    			flag = false;
    			break;
    		}
    	}
    	return flag;
    }
    int main(){
    	int x, i, j;
    	cin >> x;
    	for(i = 2; i <= sqrt(x); i++){
    		if(x % i == 0 && zhishu(i)){
    			j = x / i;
    			break;
    		}
    	}
    	cout << j << endl;
    }
    
    • 1
      @ 2022-12-27 19:51:28

      相当炸裂啊

      #include<bits/stdc++.h>
      using namespace std;
      const int N=1e5+6;
      int prime[N];
      bool vis[N];
      int k=0;
      int seive()
      {
      	memset(vis,true,sizeof(vis));
      	vis[0]=vis[1]=false;
      	for(int i=2;i<N;i++)
      	{
      		if(vis[i])
      			prime[k++]=i;
      		for(int j=0;i<k;j++)
      		{
      			if(i*prime[j]>N) break;
      			vis[i*prime[j]]=false;
      			if(!i%prime[j])break;
      		}
      	}
      	return k;
      }
      int main()
      {
      	seive();
      	int n;
      	cin>>n;
      	for(int i=0;i<k;i++)
      	{
      		if(n%prime[i]==0)
      		{
      			printf("%d",n/prime[i]);
      			break;
      		}	
      			
      	}
      	return 0;
      }
      
      • 1
        @ 2022-4-5 0:45:10

        C++

        有一个小小的注意点,先从小的除,防止超时

        #include <iostream>
        #include <cmath>
        using namespace std;
        int isprime(int n)
        {
            int m = sqrt(n);
            for(int i = 2; i <= m; i++)
            {
                if(n % i == 0)  return 0;
            }
            return 1;
        }
        int main()
        {
            int n = 0;
            cin >> n;
            int m = sqrt(n);
            int i = 0;
            for(i = 2; i <= m; i++)
            {
                if(n % i == 0 && isprime(n/i))
                {
                    cout << n/i << endl;
                    break;
                }
            }
            return 0;
        }
        
        • 0
          @ 2022-11-26 16:23:11
          #include<cmath>
          using namespace std;
          int main(){
          	int x, i, j, max;
          	cin >> x;
          	for(i = 2; i < sqrt(x); i++){
          		if(x % i == 0){
          			j = x / i;
          			break;
          		}
          	}
          	if(i > j)
          		max = i;
          	else
          		max = j;
          	cout << max << endl;
          }
          
          • 0
            @ 2022-4-8 23:56:30
            import java.util.Scanner;
            
            public class Main {
            	public static void main(String[] args) {
            		Scanner sc = new Scanner(System.in);
            		int n = sc.nextInt();
            		int i = 2;// 用来除的数,检验n是否为质数
            		for(;i<=n;i++) {
            			if(n%i==0) break;
            		}
            		System.out.println(n/i);
            		sc.close();
            	}
            }
            
            • 0
              @ 2022-3-7 21:20:29
              #include <stdio.h>
              #include <algorithm>
              #include <math.h>
              #include <vector>
              #include <iostream>
              using namespace std;
              int n;
              int main() {
                  scanf("%d",&n);
                  for(int k=2; k*k<=n; k++) {
                      if(n%k==0) {
                          int tmp=n/k;
                          int q=0;
                          for(int i=2; i*i<=tmp; i++) {
                              if(tmp%i==0) {
                                  q=1;
                                  break;
                              }
                          }
                          if(!q) {
                              printf("%d\n",tmp);
                              break;
                          }
                      }
                  }
                  return 0;
              }
              
              • 1

              Information

              ID
              44
              Time
              1000ms
              Memory
              256MiB
              Difficulty
              7
              Tags
              # Submissions
              999
              Accepted
              238
              Uploaded By