2020年 1月26号OJ习题【很low】

捐款

简单的等差数列求和

#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;
}

军训

简单的排列计算

#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;
}

求导

简单的函数

#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;
}

统计

简单的单层循环

#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;
}

整数–求和

依然是等差数列,只不过范围变大了
正好用一下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;
}

组合-亢龙无悔

简单的组合数,范围很小,用公式即可

#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;
}

这让我想起了几天前做的选优秀这道题,同样是组合数,但是因为这个范围过大,因此用公式计算的话需要高精度,因此此题利用的是杨辉三角性质先打表再直接找就好了!!!,详细地代码见22号的博客22号习题

云之遥–素数

简单的判断素数,1特判

#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;
}

做了一些水题,毕竟春节,不给自己找麻烦。。。。。。

发布了10 篇原创文章 · 获赞 1 · 访问量 114

猜你喜欢

转载自blog.csdn.net/shiroi2333/article/details/104088121