Acwing语法基础课题目练习

发布时间:2024年01月03日

变量、输入输出、表达式与顺序语句

616. 两点间的距离

#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    double a,b,c,d;
    cin>>a>>b>>c>>d;
    double s;
    s=sqrt((c-a)*(c-a)+(d-b)*(d-b));
    printf("%.4f",s);
    return 0;
}

653. 钞票

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int N;
    cin >> N;
    cout<<N<<"\n";
    printf("%d nota(s) de R$ 100,00\n", N/100);
    N = N % 100;
    printf("%d nota(s) de R$ 50,00\n", N / 50);
    N = N % 50;
    printf("%d nota(s) de R$ 20,00\n", N / 20);
    N=N%20;
    printf("%d nota(s) de R$ 10,00\n", N / 10);
    N=N%10;
    printf("%d nota(s) de R$ 5,00\n", N / 5);
    N=N%5;
    printf("%d nota(s) de R$ 2,00\n", N / 2);
    N=N%2;
    printf("%d nota(s) de R$ 1,00\n", N / 1);
    N=N%1;
}

654. 时间转换

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin >> n;
    printf("%d:%d:%d",n/3600,n/60%60,n%60);
    return 0;
}

605. 简单乘积

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    int n,m,s;
    cin >> n>>m;
    cout<<"PROD = "<<m*n;
    return 0;
}

AcWing 611. 简单计算

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    int a, b;
    double d, c;
    cin >> a >> a >> c;
    cin >> b >> b >> d;
    printf("VALOR A PAGAR: R$ %.2f",a*c+b*d);
    return 0;
}

657. 选择练习1

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    if((b>c)&&(d>a)&&((c+d)>(a+b))&&(c>0)&&(d>0)&&(a%2==0))
        cout<<"Valores aceitos";
    else
        cout<<"Valores nao aceitos";
}

AcWing 662. 点的坐标

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    double a,b;
    cin>>a>>b;
    if((a>0)&&(b>0))
        cout<<"Q1"<<endl;
    else if((a<0)&&(b>0))
        cout<<"Q2";
    else if((a<0)&&(b<0))
        cout<<"Q3";
    else if((a>0)&&(b<0))
        cout<<"Q4";
    else if((a==0)&&(b==0))
        cout<<"Origem";
    else if(b==0)
        cout<<"Eixo X";
    else if(a==0)
        cout<<"Eixo Y";
    return 0;
}

717. 简单斐波那契

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
int a[50];
int main()
{
    int n;
    cin>>n;
    if(n == 1)
    {
        cout << 0;
        return 0;
    }
    a[0]=0,a[1]=1;
    cout<<0<<" "<<1<<" ";
    for (int i = 2; i < n; i ++ )
    {
        a[i]=a[i-1]+a[i-2];
        cout<<a[i]<<" ";
    }
    return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;


int main()
{
    int n;
    cin>>n;

    int a, b, c;
    a = 0;
    b = 1;
    c = 1;
    for (int i = 0; i < n; i ++ )
    {
        cout << a << " ";
        c = b + c;
        int t = b;
        b = a + b;
        a = t;
    }

    return 0;
}

AcWing 726. 质数

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int check(int n)
{
    if(n<=1)
        return 0;
    else 
    {
        for(int i=2;i<=sqrt(n);i++)
        {
            if( n%i == 0)
                return 0;
        }
        return 1;
    }
}

int main()
{
    int T;
    cin >> T;
    while(T -- )
    {
        int n;
        cin >> n;
        if(check(n) == 0)
            cout << n << " is not prime\n";
        else 
            cout << n << " is prime\n";
    }
    return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
    int n;
    cin>>n;
    for (int i = 0; i < n; i ++ )
    {
        int x;
        cin>>x;
        int len = 0;
        
        for(int j=2;j<=sqrt(x);j++)
        {
            if(x%j==0)
            {
                cout<<x<<" is not prime \n";
                len = 1;
                break;
            }
        }
        if (len == 0)
            cout<<x<<" is prime\n";
    }
    
    return 0;
}
文章来源:https://blog.csdn.net/qq_52358084/article/details/135370193
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。