C++第一课 输出Hello World

#include<bits/stdc++.h>
using namespace std;
int main(){
 
}

这是一个固定的格式,记住就行了。

#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"Hello, World!"<<endl;
    return 0;
}

这是一个简单的输出Hello, World!

 

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    a = 8;
    cout<<a<<endl;
    return 0;
}

定义一个整型变量,并赋值输出

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c;
    cin>>a>>b>>c;
    cout<<b<<endl;
    return 0;
}

定义3个整型变量输入,输出第二个变量

条件判断

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    if(a>0)
    {
        cout<<"positive"<<endl;
    }
    else if(a == 0)
    {
        cout<<"zero"<<endl;
    }
    else
    {
        cout<<"negative"<<endl;    
    }

    return 0;
}


/*双分支
if(条件)
{
    语句
}
else
{
    语句
}

逻辑运算符
&& 与
|| 或
! 非
*/

 输入三角形的三条边,判断是否可以构成三角形

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c;
    cin>>a>>b>>c;
    if(a+b>c && a+c>b && b+c>a)
    {
        cout<<"yes"<<endl;    
    }
    else
    {
        cout<<"no"<<endl;
    }
    return 0;
}

posted on 2024-03-03 08:30  lilyxiaoyy  阅读(35)  评论(0编辑  收藏  举报

返回
顶部