IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C/C++常用语法复习(输入、输出、判断、循环) -> 正文阅读

[C++知识库]C/C++常用语法复习(输入、输出、判断、循环)

内容借鉴acwing,仅供复习。

入门程序

#include<iostream>

using namespace std;

int main()
{
    cout << "hello world" << endl;
    return 0;
}

1、 变量的定义
先定义后使用,不可以重名

#include<iostream>

using namespace std;

int main()
{
 	int a = 5;
 	int b, c = a,d = 10 / 2;
 	cout << a << b << c << d << endl;
	return 0;
}

2、 输入输出

#include<iostream>

using namespace std;

int main()
{
    int a, b;
    cin >> a >> b;
    cout << a + b << endl;
    return 0;
}

输入输出多个不同类型的变量;

#include<iostream>
#include<string>

using namespace std;

int main()
{
    int a, b;
    string str;
    
    cin >> a;
    cin >> b >> str;
    
    cout << str << "!!" << a + b << endl;
    
    return 0;
}

3、表达式
/:地板除

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a = 6 + 3 * 4 /2 -2;
    
    cout << a << endl;
    
    int b = a * 10 + 5 / 2;
    
    cout << b << endl;
    
    cout << 23 * 56 - 78 / 3 << endl;
    
    return 0;
}

4、浮点数运算:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    float x = 1.5, y = 3.2;
    
    cout << x * y << ' ' << x + y << endl;
    
    cout << x - y << ' ' << x / y << endl;
    
    return 0;
}

5、 整型变量的自增、自减

#include<iostream>
#include<string>

using namespace std;

int main()
{
    int a = 1;
    int b = a ++;
    
    cout << a << ' ' << b << endl;
    
    int c = ++ a;
    
    cout << a << ' ' << c << endl;
    
    return 0;
}

6、类型转换:

#include<iostream>
#include<string>

using namespace std;

int main()
{
    float x = 123.12;
    
    int y = int(x);
    
    cout << x << ' ' << y << endl;
    
    return 0;
}

7、顺序语句
输出第二个整数:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    cout << b << endl;
    return 0;
}

求反三位数:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n ;
    cin >> n;
    
    int a = n % 10;
    n = n / 10;
    int b = n % 10;
    n = n / 10;
    int c = n % 10;
    
    cout << a << b << c << endl;
    return 0;
}

printf语句与判断结构

1、printf输出格式,注意:使用printf时最好添加头文件 #include 。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    printf("hello world");
    return 0;
}

Int、float、double、char等类型的输出格式:
(1) int:%d
(2) float: %f, 默认保留6位小数
(3) double: %lf, 默认保留6位小数
(4) char: %c, 回车也是一个字符,用’\n’表示

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a = 3;
    float b = 3.14564;
    double c = 3.14564;
    char d = 'y';
    
    printf("%d\n",a);
    printf("%f,%lf,%c",b,c,d);
    
    return 0;
}

练习:输入一个整数,表示时间,单位是秒。输出一个字符串,用”时:分:秒”的形式表示这个时间。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int t;
    
    cin >> t;
    
    int hours = t / 3600;
    int minutes = t % 3600 / 60;
    int seconds = t % 60;
    
    printf("%d:%d:%d",hours,minutes,seconds);
    
    return 0;
}

float, double等输出保留若干位小数时用:%.4f, %.3lf

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    float b = 3.141565464;
    double c= 3.1415231321;
    
    printf("%.4f\n", b);
    printf("%.3lf\n",c);
    
    return 0;
}

%8.3f, 表示这个浮点数的最小宽度为8,保留3位小数,当宽度不足时在前面补空格。 %-8.3f,表示最小宽度为8,保留3位小数,当宽度不足时在后面补上空格。
%08.3f, 表示最小宽度为8,保留3位小数,当宽度不足时在前面补上0
2、 if else语句

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a, b;
    cin >> a >> b;
    
    if (a > b)
        cout << a << endl;
    else
        cout << b << endl;
        
    return 0;
}

3、if-else连写
输入一个0到100之间的分数,
如果大于等于85,输出A;
如果大于等于70并且小于85,输出B;
如果大于等于60并且小于70,输出C;
如果小于60,输出 D;

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int s;
    cin >> s;
    
    if (s >= 85)
        printf("A");
    else if (s >= 70)
        printf("B");
    else if (s >= 60)
        printf("C");
    else
        printf("D");
    
    return 0;
}

判断闰年。闰年有两种情况:
(1) 能被100整除时,必须能被400整除;
(2) 不能被100整除时,被4整除即可。
输入一个年份,如果是闰年输出yes,否则输出no。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int year;
    cin >> year;
    
    if (year % 400 == 0 || year % 4 ==0 && year % 100 != 0)
        printf("Yes");
    else
        printf("NO");
    return 0;
}

3、 条件表达式
(1) 与 &&
(2) 或 ||
(3) 非 !
输入三个数,输出三个数中的最大值。

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    
    if (a >= b && a >= c) cout << a << endl;
    else if (b >= a && b >= c) cout << b << endl;
    else cout << c << endl;
    
    return 0;
}

循环语句

1、while循环
求1~100中所有数的立方和。

#include <iostream>

using namespace std;

int main()
{
    int i = 1,sum = 0;
    while (i <= 100)
    {
        sum += i * i * i;
        i ++;
    }
    cout << sum << endl;
    return 0;
}

求斐波那契数列的第n项。f(1) = 1, f(2) = 1, f(3) = 2, f(n) = f(n-1) + f(n-2)。

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    
    int a = 1,b = 1, i = 1;
    
    while (i < n)
    {
        int c = a + b;
        a = b;
        b = c;
        i ++;
    }
    
    cout << a << endl;
    
    return 0;
}

2、for循环

#include <iostream>

using namespace std;

int main()
{
    for (int i = 0; i < 10; i ++ )
    {
        cout << i << endl;
    }

    return 0;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-13 10:54:41  更:2022-09-13 10:57:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 15:17:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码