Acwing语法基础课 1.变量、输入输出、表达式和顺序语句(笔记)

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

编程是一种控制计算机的方式,和我们平时双击打开文件、关机、重启没有任何区别。·

4个Level:

语法基础课:C++在算法竞赛、算法考试中常见语法(非工程) 20h
算法基础课:各种常用算法、代码模板【足以应付找工作】 45h
算法提高课:应用技巧 106h
算法进阶课:特别难的题


00 概述

编写一个简单的C++程序——手速练习

Hello world.cpp

#include<cstdio>
#include<iostream>

using namespace std;

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

C++源文件结构(暂时):

  • 头文件
    <cstido>:printf、scanf
    <iostream>:cout输出、cin读入
  • 使用命名空间
    using namespace std;
  • 程序执行入口
int main()
{

	return 0;
}

作业和提交地址


01 变量的定义

  • 变量必须先定义才可以使用,不能重名
  • 常用变量类型及范围:
    int:−231 ~ 231-1,4 byte
    bool:false 0 和 true 1,1 byte(8 bit)
    char:字符,1 byte
    float:单精度浮点数,6-7位有效数字,4 byte
    double:双精度浮点数,15-16位有效数字,8 byte
    long long:更长的整数型,−263 ~ 263-1,8 byte
    long double:更长的double,18-19位有效数字,16 byte
  • 定义格式:

整型 int:

int a , b = 2, c = b;
// 等价于
int a;
int b = 2;
int c = b;

浮点型 float
双浮点型 double

float d = 1.5 , e = 1 , f = 1.235e2;
//整数是特殊的浮点数

布尔型 bool

bool g = true , h = false;

字符型 char

char j = 'a' , k = 'b';

long long

long long l = 100000000000000LL;

long double

long double m = 123.184;

02 输入输出

AC:通过
WA:答案错误
TLE:超时
MLE:超内存
SF:数组越界
CE:编译错误
RE:运行时间出错——没出来结果

例题Acwing 1. A+B

#include<iostream>	//头文件

using namespace std;

int main()
{
    int a,b;    //定义两个变量
    cin >> a >> b;  //输入
    cout << a + b << endl;   //输出
    
    return 0;
}

printfscanf输入输出:

  • 整数的输入输出
	int a, b;
	scanf("%d %d",&a,&b);
	printf("%d %d\n",a + b, a * b);

这个在格式化输入输出时很方便,但是会判断类型
cout 和 cin 不会判断类型
!所有能用 cin 和 cout 的地方,一定可以用 printf 、scanf 替换,但是反过来不一定可以
→效率问题,printf、scanf 要快一点

  • 单精度浮点数保留位数
float a, b;
scanf("%f %f", &a, &b);
printf("a + b = %.1f\na * b = %.2f\n",a + b, a * b);
  • 字符
scanf("%c%c", &a, &b); 
//这种方式会读入空格,但是cin不会
printf("%c %c\n",a, b);
  • 双精度浮点数输入输出
double a,b;
scanf("%lf%lf", &a, &b);
printf("%lf %lf", a, b);
  • long long类型
long long a,b;
scanf("%lld%lld", &a, &b);
printf("%lld %lld", a, b);

总结:不同变量的输出方式

  • int%d
  • float%f
  • double%lf
  • char%c
  • long long%lld

cincout输入输出

  • 整数的输入输出:
#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()
{
    string str;
    cin >> str;
    cout << str;
    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;
}

03 表达式

整数的加减乘除四则运算

整数的加减乘除四则运算
只有整数可以进行取余运算

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

输出结果:
10
102
1262

浮点数的运算

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

输出结果:
4.8 4.7
-1.7 0.46875

整型变量的自增和自减

a++a--:先用再加减
++a--a:先加减再用

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

输出结果:
2 1
3 3

简写运算

a = a + b;a += b;
a = a - b;a -= b;
a = a * b;a *= b;
a = a / b;a /= b;
a = a % b;a %= b;
…………

变量的类型转换

不同的变量类型之间,可以相互赋值

显式转换
  1. 整数到浮点:相当于直接变过去
int a = 5;
float b = (float)a;
printf("%f",b); 	// 5.000000
  1. 浮点到整数:下取整
float a = 5.23;
int b = (int)a;
printf("%d",b); 	// 5
  1. int 和 char 之间:根据ASCII码表转换
int a = 97;
char b = (char)a;
printf("%c",b);	 // a

一个大胆的想法,char类型可以做运算

char c = 'A';
cout << (char)(c+32) << endl; // a
cout << (char)(c+33) << endl; // b
#include <iostream>
#include <string>
 
using namespace std;

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

    return 0;
}

输出结果:
123.12 123

隐式转换(往精度高的转)

intfloat/doublefloat/double
charintint
intlonglong
floatdoubledouble

04 顺序语句

每一句代码从前往后按编写顺序执行

例题(1) 输入三个整数,输出第二个:

#include <iostream>
#include <string>

using namespace std;

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

例题(2) 计算 (a + b) * c的值

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a, b, c;

    cin >> a >> b >> c;

    cout << (a + b) * c << endl;

    return 0;
}

例题(3) 带余除法

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a, b;

    cin >> a >> b;

    int c = a / b, d = a % b;

    cout << c << ' ' << d << endl;

    return 0;
}

例题(4) 求反三位数:

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

    cout << a << b << c << endl;

    return 0;
}

例题(5) 交换两个整数

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a = 3, b = 4;

    int t = a;
    a = b;
    b = t;

    cout << a << ' ' << b << endl;

    return 0;
}

例题(6) 输出菱形

#include <iostream>
#include <string>

using namespace std;

int main()
{
    char c;

    cin >> c;

    cout << "  " << c << endl;
    cout << " " << c << c << c << endl;
    cout << c << c << c << c << c << endl;
    cout << " " << c << c << c << endl;
    cout << "  " << c << endl;

    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值