42: Draw rectangle 43: prime factor decomposition

42: draw rectangle

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    int h,w,k;char a; //k denotes whether it is hollow or not
    cin >> h>>w>>a>>k;
    for(int i=0;i<w;i++){ //Print the top layer
        cout << a;
    }
    cout <<endl;
    for(int l=0;l<h-2;l++){ //Printing Intermediate Part
        cout << a ;
        if (k==0){for(int l2=0;l2<w-2;l2++){cout << " ";}} //If hollow, fill in the middle with spaces
        else {for(int l2=0;l2<w-2;l2++){cout << a;}} //Fill in with characters instead
        cout << a <<endl;
    }
    for(int j=0;j<w;j++){ //Print the bottom layer
        cout << a;
    }
}

It's simple
43: prime factor decomposition
I have used three methods to solve this problem. The following is over. The other two are out of time.

#include <iostream>
using namespace std;
int main(){
    long n,num;
    cin >> n;
    for(int i=2;i<n;i++){
     if(n%i==0){num=n/i;break;}
    }
    cout << num;
}

First kind

#include <iostream>
using namespace std;
int main(){
    long n,s=0,s2=0;
    scanf("%ld",&n);
    for(long i=2;i<n;i++){
        if(n%i==0){s++;}
    }
    long num[s],max=0;
    for(long i=2;i<n;i++){
        if(n%i==0){num[s2]=i;s2++;}
    }
    for(long j=0;j<s;j++){
        if(max<num[j]){
            max=num[j];
        }
    }printf("%ld",max);
}

It takes a lot of time to start the cycle from 2.
Second kinds

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    long n,num;
    cin >> n;
    for(long i=n-1;i>1;i--){
    	if(n%i==0){num=i;break;}
    }
    cout << num;
}

Starting from n-1, it takes time.

Next question I have been over three hours, has been overtime, no way, to see other people, are I do not understand the algorithm, I should be so dish. For the first time, I felt such a big gap.

#include <iostream>
using namespace std;
int main(){
    long n,num=0,size;
    cin >>n;
    for(long i=3;true;i+=2){
     size=0;
     for(long j=2;j<i;j++){
            if(i%j==0) break;
      else size++;
      }if(size==i-2){num++;}
     if((num+1)==n){cout <<i; break;}
    }
}

This is my, no way, can not pass, the mentality really collapsed, there are 22 days, it is too late.

Posted by cruzbullit on Sat, 12 Oct 2019 08:55:34 -0700