2020 January 26 OJ exercises [very low]

Donation

Simple arithmetic sequence summation

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,sum;
    ios::sync_with_stdio(false);
    while(cin>>n)
    {
        sum=(1+n)*n/2;
        cout<<sum<<endl;
    }
    return 0;
}

Military training

Simple calculation arrangement

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int sum=1;
        for(int i=1;i<=n;i++)
        {
            sum*=i;
        }
        printf("%d\n",sum);
    }
    return 0;
}

Derivation

Simple function

#include <bits/stdc++.h>
using namespace std;

double f(double x)
{
    return 3*x*x+2*x+4;
}
int main()
{
    double x,h,y;
    while(cin>>x>>h)
    {
        y=(f(x+h)-f(x))/h;
        printf("%.2lf\n",y);
    }
    return 0;
}

statistics

Simple single-cycle

#include <bits/stdc++.h>
using namespace std;
int x[1001];
int main()
{
    int n;
    while(cin>>n)
    {
        int count=0;
        for(int i=0;i<n;i++)
        {
            cin>>x[i];
            if(x[i]<60)
                count++;
        }
        cout<<count<<endl;
    }
    return 0;
}

Integer - sum

Still the arithmetic sequence, but a larger range
just use what typedet. . . . . .

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n,sum;
    ios::sync_with_stdio(false);
    while(cin>>n)
    {
        sum=(1+n)*n/2;
        cout<<sum<<endl;
    }
    return 0;
}

Combination - Kang Long regret

The number of combinations of simple, very small, you can use the formula

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(n<m)
            cout<<"0"<<endl;
        else if(n==m||m==0)
            cout<<"1"<<endl;
        else
        {
            int t=1,k=1;//t,k分别记录分子与分母//
            int x=n,y=m;
            for(int i=m;i>0;i--)
            {
                t*=x;
                x--;
                k*=y;
                y--;
            }
            cout<<t/k<<endl;
        }
    }
    return 0;
}

This reminds me of a few days ago to do the election excellent this question, the same number of combinations, but because it is too large, so if calculated using the formula requires precision, so this problem is to use Pascal's Triangle properties hit the table again directly to like! ! ! See the code in detail blog 22 22 Exercises

Un'noHaruka - prime number

Simple prime number is determined, a determination Laid

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        if(n==1) cout<<"NO"<<endl;
        else
        {
            int flag=1;
            for(int i=n-1;i>1;i--)
            {
                if(n%i==0)
                {
                    flag=0;
                    break;
                }
            }
            if(flag==1) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    return 0;
}

Did some water problems, after all, Chinese New Year, do not trouble yourself. . . . . .

Published 10 original articles · won praise 1 · views 114

Guess you like

Origin blog.csdn.net/shiroi2333/article/details/104088121