Chapter 2 program design of cyclic structure

Keywords: Windows

 

Example 2-1: 7744: how to judge whether n is a complete square number (P20)

(1) The integer m is used to store the integer after sqrt(n) rounding (considering floating-point error, so it is generally rounded). Judge m*m==n?

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
    int n,m;
    for(int i=1;i<=9;i++){
        for(int j=0;j<=9;j++){
            n=i*1100+j*11;
            m=floor(sqrt(n)+0.5);  //Given the floating-point error, it is rounded. floor(x) returns the maximum integer no more than x
            if(n==m*m)
                printf("%d\n",n);
        }
    }
    return 0;
}

(2) Enumerate the square root x to avoid the square operation.

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
    int n;
    for(int x=30;;x++){
       n=x*x;
       if(n<1000) continue;
       if(n>=10000) break;
 
       if(n/1000==n/100%10&&n%100/10==n%10)  // %And / have the same priority
           printf("%d\n",n);
    }
    return 0;
}

Example 2-2:3n+1 problem -- pay attention not to overflow, int range 2e9 + (P22)

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;

int main(){
    ll n;
    scanf("%lld",&n);
    ll cnt=0;
    while(n!=1){
        if(n&1) n=3*n+1;
        else n/=2;
        cnt++;
        //cout<<cnt<<" "<<n<<endl;
    }
    printf("%lld\n",cnt);
    return 0;
}

Example 2-3: approximate calculation (P24)

Made a year ago

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-6;

int main(){
    double sum=0,tmp;
    int i=0,t=1;
    do{
        tmp=1.0/(2*i+1);
        sum+=t*tmp;
        t*=-1;
        i++;
    }while(tmp>=eps);
    printf("%.6f\n",sum);
    return 0;
}
//Output: 0.785399
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-6;

int main(){
    double sum=0;
    for(int i=0;;i++){
        double term=1.0/(i*2+1);
        if(i%2==0) sum+=term;
        else sum-=term;
        if(term<eps) break;
    }
    printf("%.6f\n",sum);
    return 0;
}

Example 2-4: sum of factorials (P25)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<time.h>
using namespace std;
const int mod=1e6;
typedef long long ll;

int main(){
    int n;
    scanf("%d",&n);
    ll sum=0,tmp=1;
    for(int i=1;i<=n;i++){
        tmp*=i;
        tmp%=mod;
        sum+=tmp;
        sum%=mod;

    }
    printf("%lld\n",sum);
   // Printf ("time used =%. 3F \ n", (double) clock() / locks_per_sec); / / timing function in seconds
    return 0;
}

From 40 onwards, the answer remains the same. Because 25! There are 6 zeros at the end, so starting from the fifth item, all subsequent items will not affect the last 6 digits of the sum.

So we can add a judgment before the program. If n exceeds 25, then n will be assigned 25.  

#include<iostream>
#include<cstdio>
#include<cmath>
#include<time.h>
using namespace std;
const int mod=1e6;
typedef long long ll;

int main(){
    int n;
    scanf("%d",&n);
    if(n>25) n=25;
    ll sum=0;
    for(int i=1;i<=n;i++){
        int factorial=1;
        for(int j=1;j<=i;j++)
            factorial=(factorial*j%mod);
        sum=(sum+factorial)%mod;
    }
    printf("%lld\n",sum);
   // Printf ("time used =%. 3F \ n", (double) clock() / locks_per_sec); / / timing function in seconds
    return 0;
}

Example 2-5 data statistics (P27)

1. Under Windows, press Enter, Ctrl+Z, and Enter to finish the input.

#include<iostream>
#include<cstdio>
#include<cmath>
#include<time.h>
using namespace std;
const int mod=1e6;
typedef long long ll;

int main(){
    double sum;
    int n,mx,mn,cnt=0;
    while(cin>>n){
        if(cnt==0){
            mx=n,mn=n;
        }else{
            mx=max(mx,n);
            mn=min(mn,n);
        }
        sum+=n;
        cnt++;
    }

    printf("max=%d,min=%d,avr=%.3f\n",mx,mn,sum/cnt);

    return 0;
}

2. Redirected version Written a year ago

#define LOCAL
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int INF=1e9;

int main(){
#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // LOCAL
    double sum;
    int n,mx,mn,cnt=0;
    while(cin>>n){
        if(cnt==0){
            mx=n,mn=n;
        }else{
            mx=max(mx,n);
            mn=min(mn,n);
        }
        sum+=n;
        cnt++;
    }

    printf("max=%d,min=%d,avr=%.3f\n",mx,mn,sum/cnt);

    return 0;
}

Note: in.txt and out.txt files and programs should be in the same file directory. Input and output are all in text.  

If the competition requires standard input and output, just delete "define LOCAL" before submitting.

3.fopen version

Failed to achieve TT

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int INF=1e9;

int main(){
    FILE *fin,*fout;
    fin=fopen("in.txt","rb");
    fout=fopen("out.txt","rw");
    double sum;
    int n,mx,mn,cnt=0;
    while(fscanf(fin,"%d",&n)==1){
        if(cnt==0){
            mx=n,mn=n;
        }else{
            mx=max(mx,n);
            mn=min(mn,n);
        }
        sum+=n;
        cnt++;
    }

    fprintf(fout,"max=%d,min=%d,avr=%.3f\n",mx,mn,sum/cnt);
    fclose(fin);
    fclose(fout);

    return 0;
}

 

 

Posted by work_it_work on Thu, 02 Jan 2020 12:51:43 -0800