pta天梯赛

一系列C++编程实例,涵盖了从基础操作到复杂计算的多个方面,包括温度转换、输出拼音、计算和、比较大小、指数计算、阶乘和、字符串处理、判断素数、体重判断、日期格式化、打招呼、新世界场景模拟、情侣身高匹配、打折计算、心理阴影面积、新胖子公式、吃鱼还是吃肉、调和平均、人与神的关系等。这些代码展示了C++在实际问题解决中的应用。
摘要由CSDN通过智能技术生成

··

#include<iostream>
using namespace std;
int main()
{
    cout<<"Hello World!"<<endl;//输出
}

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a;
    b=5*(a-32)/9;//计算温度公式
    cout<<"Celsius = "<<b;
}

//输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出fu字。十个数字对应的拼音
#include<iostream>
using namespace std;
int zs(int n)//定义一个函数得该熟的第一位
{
    int cnt=1;
    while(n>9)
    {
        n/=10;
        cnt*=10;
    }
    return cnt;
}
int main()
{
    long int n,m,c;
    cin>>n;
    if(n<0)
    {
        cout<<"fu ";//小于零时先输出fu
        n=-n;
    }
    c=zs(n);
    for(int i=1;c;i++)
    {
        m=n/c;
        n=n%c;
        switch(m)//输出第一位
        {
            case 0:cout<<"ling";break;
            case 1:cout<<"yi";break;
            case 2:cout<<"er";break;
            case 3:cout<<"san";break;
            case 4:cout<<"si";break;
            case 5:cout<<"wu";break;
            case 6:cout<<"liu";break;
            case 7:cout<<"qi";break;
            case 8:cout<<"ba";break;
            case 9:cout<<"jiu";break;
        }
        c=c/10;
        if(c)
        cout<<" "; //最后一个不输出空格
    }
     
}

求整数段和

#include<iostream>
using namespace std;
#include<iomanip>
int main()
{
    int A,B,sum=0,count=0,c;
    cin>>A>>B;
    if(A>B)
    {
        A=A+B;
        B=A-B;
        A=A-B;
    }
    c=A;
    for(A;A<=B;A++)
    {
        cout<<setw(5)<<A;
        count++;
        if(count%5==0)
        cout<<endl;
        sum=sum+A;
    }
    if((B-c+1)%5==0)
        cout<<"Sum = "<<sum;
    else
        cout<<endl<<"Sum = "<<sum;
}

三个数比大小

#include<iostream>
using namespace std;
int main()
{
    int a,b,c,t;
    cin>>a>>b>>c;
    if(a>b)//两数比较,小数放在前边
    {
        t=a;
        a=b;
        b=t;
    }
    if(b>c)
    {
        t=b;
        b=c;
        c=t;
    }
    if(a>b)
    {
        t=a;
        a=b;
        b=t;
    }
    cout<<a<<"->"<<b<<"->"<<c;//最后按格式输出
}

计算指数

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    long int n,m;
    cin>>m;
    cout<<"2^"<<m<<" = "<<pow(2,m);利用cmath函数输出
}

计算阶乘和

#include<iostream>
using namespace std;
int main()
{
    long int n,j=1,i=1,sum=0;
    cin>>n;
    for(i;i<=n;i++)
    {
        j=j*i;
        sum=sum+j;
    }
    cout<<sum;
}

直接输出

#include<iostream>
using namespace std;
int main()
{
    cout<<"This is a simple problem.";
}

大笨钟

#include<stdio.h>
main()
{
    int hh,mm,i,j;
    scanf("%d:%d",&hh,&mm);
    if(hh<12||hh==12&&mm==0)//判断时间是否在敲钟时间内
        printf("Only %02d:%02d.  Too early to Dang.",hh,mm);
    else
    {
        i=hh-12;
        while(i--)//循环判断敲几下
            printf("Dang");
        if(mm>0)
            printf("Dang");
    }
}

重要的事情说三遍

#include<iostream>
using namespace std;
int main()
{
    cout<<"I'm gonna WIN!"<<endl; cout<<"I'm gonna WIN!"<<endl; cout<<"I'm gonna WIN!";
 

奇偶分家

#include<iostream>
using namespace std;
int main()
{
    int j=0,o=0,n,shu;
    cin>>n;
    for(n;n>0;n--)
    {
        cin>>shu;
        if(shu%2)//判断奇数还是偶数
            j++;
        else
            o++;
    }
    cout<<j<<" "<<o;
}

后天

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    switch(n)//输入一个数
    {
        case 1:cout<<"3";break;//输出后天
        case 2:cout<<"4";break;
        case 3:cout<<"5";break;
        case 4:cout<<"6";break;
        case 5:cout<<"7";break;
        case 6:cout<<"1";break;
        case 7:cout<<"2";break;
    }
}

//L1-026 I Love GPLT (5 分)
#include<stdio.h>
main()
{
    char ch[12]={"I Love GPLT"};
    for(int i=0;i<12;i++)
        printf("%c\n",ch[i]);
    
}

判断素数

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    bool e=true;
    long long int n,k;
    cin>>n;
   
    while(n--)
    {
        e = true;
        cin>>k;
         if(k ==1){
printf("No\n");
        continue;
        }
        for(long long i=2;i<=sqrt(k);i++)//从2开始小于他的平方根为素数范围

{
               if(k%i==0){
                   e = false;
                   break;
}
                
}
         
        if(e)//若e不成立证明不是素数
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
        
    }
}

到底是不是太胖了

#include<iomanip>
#include<iostream>
using namespace std;
int main()
{
    int h;
    float a;
    cout<<fixed;
    
    cin>>h;
    h=h-100;
    a=h*0.9;
    cout<<setprecision(1)<<a*2;//该函数限制小数点
}

到底是不是太胖了升级版

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,h,w;
    float x;
    cin>>n;
    while(n--) 
    {
        cin>>h>>w;
        x=(h-100)*0.9*2;//标准体重
        if(fabs(w-x)<0.1*x)//利用公式作为判断条件
            cout<<"You are wan mei!";
        else
            if((w-x)>=0.1*x)
                cout<<"You are tai pang le!";//输出个判断条件的结果
        else
            cout<<"You are tai shou le!";
        cout<<endl;
    }
}

A*B

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    cout<<a*b;//输出乘积
}

/B#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int a,b;
    double y;
    cout<<fixed;
    cin>>a>>b;
    y=a*1.0/b;
    if(b==0)//判断分数是不是0
        cout<<a<<"/"<<b<<"=Error";
    else
        if(b>0)
            cout<<a<<"/"<<b<<"="<<setprecision(2)<<y;//保留两位小数点
    else
        cout<<a<<"/("<<b<<")="<<setprecision(2)<<y;
}

新世界

#include<iostream>
using namespace std;
#include<cstring> 
main()
{
    string str1="Hello World",str2="Hello New World";//输出该语句
    cout<<str1<<endl<<str2;
}

最佳情侣身高

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int n;
    cout<<fixed;
    char ch;
    float x;
    cin>>n;
    while(n--)
    {
        cin>>ch>>x;
        if(ch=='F')//判断男女
        {
            x=x*1.09;
        }
        if(ch=='M')
        {
            x=x/1.09;
        }
        cout<<setprecision(2)<<x<<endl;//保留两位小数
    }
    
}

寻找250

#include<iostream>
using namespace std;
int main()
{
    int x,count=1;
    cin>>x;
    while(x!=250)//输入一个数判断是否=250
    {
        count++;
        cin>>x;
    }
    cout<<count;
    
}

日期格式化

#include<stdio.h>
main()
{
    int y,m,d;
    scanf("%d-%d-%d",&m,&d,&y);
    printf("%d-%02d-%02d",y,m,d);//按照格式化输出
}

宇宙无敌打招呼

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    string str;
    cin>>str;
    cout<<"Hello "<<str;//加上hello输出
}

装睡

#include<iostream>
using namespace std;
#include<cstring>
int main()
{
    string str;
    int x,y,n;
    cin>>n;
    while(n--)
    {
        cin>>str>>x>>y;
        if(!(x>=15&&x<=20&&y>=50&&y<=70))//判断是否符合装睡标准
            cout<<str<<endl;
            
    }
    
}

打折

#include<iostream>
using namespace std;
#include<iomanip>
int main()
{
    int x;float n;
    float y;
    cout<<fixed;
    cin>>x>>n;
    y=x*n/10;
    cout<<setprecision(2)<<y;
}

2018我们要赢

#include<iostream>
using namespace std;
int main()
{
    cout<<"2018"<<endl<<"wo3 men2 yao4 ying2 !";//按照格式输出
}

电子汪

#include<iostream>
using namespace std;
int main()
{
    int x,y,n;
    cin>>x>>y;
    n=x+y;
    while(n--)//循环判断wang几下
    {
        cout<<"Wang!";
    }
}

谁是赢家

#include<iostream>
using namespace std;
int main()
{
    int pa,pb,a,b,c,count1=0,count2=0;
    cin>>pa>>pb;
    cin>>a>>b>>c;//比较判断的条件
    if(a==0)
        count1++;
    else
        count2++;
    if(b==0)
        count1++;
    else
        count2++;
    if(c==0)
        count1++;
    else
        count2++;
    if(pa>pb)
        if(count1)
            cout<<"The winner is a: "<<pa<<" + "<<count1;//按照条件输出
    else
        cout<<"The winner is b: "<<pb<<" + "<<count2;
    else
        if(count1==3)
            cout<<"The winner is a: "<<pa<<" + "<<count1;
    else
        cout<<"The winner is b: "<<pb<<" + "<<count2;
        
        
        
    
}

pta使我精神焕发

#include<iostream>
using namespace std;
int main()
{
    cout<<"PTA shi3 wo3 jing1 shen2 huan4 fa1 !";//按条件输出
    
}

心理阴影面积

#include<iostream>
using namespace std;
int main()
{
    int x,y,s;
    cin>>x>>y;
    s=100*100-(100-x)*y-0.5*x*y-0.5*(100-y)*(100-x)-0.5*100*100;//扣掉没有阴影的面积
    cout<<s;//输出结果
    
}

新胖子公式

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    float x,y;
    cout<<fixed;
    cin>>x>>y;
    cout<<setprecision(1)<<x/y/y<<endl;//一位小数输出
    if(x/y/y>25)
        cout<<"PANG";
    else
        cout<<"Hai Xing";
        
}

吃鱼还是吃肉

#include<iostream>
using namespace std;
int main()
{
    int x,h,w,n;
    cin>>n;
    while(n--)
    {
        cin>>x>>h>>w;
        if(x==1)//先判断男女
        {
            if(h>130)
                cout<<"ni li hai! ";
            else
                if(h==130)//在判断身形
                    cout<<"wan mei! ";
            else
                cout<<"duo chi yu! ";
            if(w==27)
            {
                cout<<"wan mei!";
            }
            else
                if(w<27)
            cout<<"duo chi rou!";
            else
                cout<<"shao chi rou!";
        }
        if(x==0)
        {
                        if(h>129)
                cout<<"ni li hai! ";
            else
                if(h==129)
                    cout<<"wan mei! ";
            else
                cout<<"duo chi yu! ";
            if(w==25)
            {
                cout<<"wan mei!";
            }
            else
                if(w<25)
            cout<<"duo chi rou!";
            else
                cout<<"shao chi rou!";
        }
        cout<<endl;
        
    }
}

不要废话上代码

#include<stdio.h>
main()
{
    printf("Talk is cheap. Show me the code.");//按要求输出
}

猫是液体

#include<iostream>
using namespace std;
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    cout<<a*b*c;//根据立方公式输出
    
}

洛希极限

#include<iostream>
using namespace std;
#include<iomanip>
int main()
{
    float a,b,c,s;
    cin>>a>>b>>c;
    cout<<fixed;
    if(b)
        s=1.26*a;
    else
        s=2.455*a;
    cout<<setprecision(2)<<s;/保留两位小数输出
    if(s>=c)
        cout<<" "<<"T_T";
    else
        cout<<" "<<"^_^";

}
 

调和平均

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
    int n,i;
    float x,y,s=0;
    cin>>n;
    cout<<fixed;
    i=n;
    while(i--)
    {
        cin>>x;
        s+=pow(x,-1);//按照调和公式判断
        
    }
    s/=n;
    cout<<setprecision(2)<<pow(s,-1);//输出保留一位小数
}

人与神

#include<iostream>
using namespace std;
int main()
{
    cout<<"To iterate is human, to recurse divine.";
}

两小时学完c语言

#include<iostream>
using namespace std;
int main()
{
    int n,k,m;
    cin>>n>>k>>m;
    cout<<n-k*m;//输出时间
}

强迫症

#include<iostream>
#include<cstring>
using namespace std;
string str1;
main()
{
    cin>>str1;
    if(str1.length()==4)
    {
        if((str1[0]-'0')*10+str1[1]-'0'<22)//判断4还是6位数值
        {
            printf("20");
            cout<<str1[0]<<str1[1]<<"-"<<str1[2]<<str1[3];
        }
        else
        {
            printf("19");
            cout<<str1[0]<<str1[1]<<"-"<<str1[2]<<str1[3];
        }
    }
    else
        cout<<str1[0]<<str1[1]<<str1[2]<<str1[3]<<"-"<<str1[4]<<str1[5];//按照要求输出
}

提醒降价机器人

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    float x,y,i;
    cout<<fixed;
    cin>>x>>y;
    while(x--)
    {
        cin>>i;
        if(i<y)
            cout<<"On Sale! "<<setprecision(1)<<i<<endl;//保留一位小数输出
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值