您的位置:首页 > 科技 > 能源 > 网站建设网站建设教程_网页图片提取工具_合肥网络推广公司_网站免费推广软件

网站建设网站建设教程_网页图片提取工具_合肥网络推广公司_网站免费推广软件

2025/4/2 17:42:21 来源:https://blog.csdn.net/2401_88724790/article/details/146404693  浏览:    关键词:网站建设网站建设教程_网页图片提取工具_合肥网络推广公司_网站免费推广软件
网站建设网站建设教程_网页图片提取工具_合肥网络推广公司_网站免费推广软件

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

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

#include<cstdio>
#include<iostream>using namespace std;int main()
{cout << "Hello world" << endl;return 0;
}

C++源文件结构:

头文件
#include <cstido> 包含printf、scanf
#include <iostream> 包含cout输出、cin读入

使用命名空间
using namespace std; 表示使用了std这个命名空间,用于解决不同文件间的变量名的冲突,只需在某个命名空间里变量名不冲突即可,不同的命名空间间有变量名冲突无所谓,绝大多数库函数,比如cin与cout,都在这个std命名空间里

程序执行入口

int main()
{return 0;
}

变量的定义

变量必须先定义才可以使用,不能重名

常用变量类型及范围:

int:−2^{31} ~ 2^{31}-1,4 byte

bool:值为false(=0) 或 true(=1),1 byte(8 bit)

char:字符,用单引号 ' ' 来表示,如 'c','\n',1 byte

float:单精度浮点数,6-7位有效数字,可以用科学计数法,比如1.235e2,4 byte

double:双精度浮点数,15-16位有效数字,8 byte

long long:更长的整数型,−2^{63} ~ 2^{63}-1,8 byte

long double:更长的double,18-19位有效数字,16 byte

定义格式:

整型:

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

浮点型(整数是可以赋给浮点数的):

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

 布尔型:

bool g = true , h = false;

字符型:

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

另外:

long long l = 100000000000000LL;
long double m = 123.184;

cin,cout输入输出

题库1 A+B

#include<iostream>	//头文件using namespace std;int main()
{int a,b;    //定义两个变量cin >> a >> b;  //输入cout << a + b << endl;   //输出return 0;
}

每个语句要以分号 ; 结尾

cin和cout后的 >> 和 << 方向可理解为一个传送带:cin后跟 >> 类似于把值从cin中传送出来到变量里去;而cout后的 << 类似于把变量的值传送进入cout中

endl 表示回车

若要输出多个整数变量的值

#include<iostream>	//头文件using namespace std;int main()
{int a,b;    //定义两个变量cin >> a >> b;  //输入cout << a + b << a * b << endl;   //输出多变量//若写为多行:cin >> a;cin >> b;cout << a + b;cout << a * b;cout << 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;
}

 scanf,printf输入输出

整数输入输出

#include<cstdio>using namespace std;int main()
{int a, b;scanf("%d %d",&a,&b);printf("%d %d\n",a + b, a * b);return 0;
}

与C中用法相同,相比cin,cout可以做到格式化输出

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

单精度浮点数输入,保留位数输出

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

易错:scanf中 %c 会读入空格,因为空格也算一个字符;不过 &d 不会读入空格;

cin,cout 也不会读入空格

双精度浮点数输入输出

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

bool:&d

表达式

整数的加减乘除四则运算:

#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
*/

只有整数可以进行取余运算,且C++里的取余运算和数学里的取余运算不同:

cout << 5 % 2 << endl;    //结果为1
cout << -5 % 2 << endl;   //结果为-1,负数也可以做取余运算
cout << -5 % -2 << endl;  //结果为-1
cout << 5 % -2 << endl;   //结果为1,可见取余运算的结果正负只取决于前面的被除数的正负

整型变量的自增和自减

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;

变量的类型转换

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

显式转换

整数到浮点:相当于直接变过去

int a = 5;
float b = (float)a;
printf("%f",b); 	// 5.000000

浮点到整数:下取整

float a = 5.23;
int b = (int)a;
printf("%d",b); 	// 5

int 和 char 之间:根据ASCII码表转换

int a = 97;
char b = (char)a;
printf("%c",b);	 // a

char类型也可以做运算(char类型实质是整数)

char c = 'A';
cout << (char)(c+32) << endl; // a
cout << (char)(c+33) << endl; // b

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

int 和 float/double → float/double
char 和 int → int
int 和 long → long
float 和 double → double

int a = 6;
float b = 1.3;
cout << a * b << endl;    //7.8,整数和浮点数做运算,结果为浮点数

顺序语句

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

#include <iostream>using namespace std;int main()
{int a;cin >> a;int b = a + 2;int c = b * 3;int d = c * 2;cout << d << endl;return 0;
}

输入三个整数,输出第二个:

#include <iostream>using namespace std;int main()
{int a, b, c;cin >> a >> b >> c;cout << b << endl;return 0;
}

计算 (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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com