文章目錄
1 輸入輸出
1.1 注意事項
- 一個.cpp文件,最好不要 printf 和 cout 交雜,要麼全用C,要麼全用C++
- 除了string的輸入用cin外,其餘任何都用scanf、printf
- 涉及浮點數,都用double
1.2 輸入:c
#include<cstdio>
輸入 | 停止符(不能讀) | 可讀 | 輸出 | 舉例 |
---|---|---|---|---|
scanf | 空格、/n | 當讀入%c,可讀空白(空格、/n、/t) | printf | |
getchar | 讀一個字符【任何】 | putchar | char ch; ch = getchar(); putchar(ch); |
|
gets | \n | 讀一行char數組【可讀 空格,不讀 \n】 | puts | char chs[10]; gets(chs); puts(chs); |
// 用getchar 輸入字符串數組
char str[20];
for(int i=0;i<5;i++){
str[i] = getchar();
}
str[5] = '\0'; // 關鍵
1.3 輸入:c ++
#include<iostream>
輸入 | 停止符(不能讀) | 可讀 | 輸出 | 舉例 |
---|---|---|---|---|
cin>>xxx>>… | 空格、/n | cout<<xxx<<… | ||
getline | /n | 讀一行char數組 或 string【可讀 空格,不讀 \n】 | char chs[10]; string str; cin.getline(chs,10); getline(cin, str); |
int a;
char b[20];
double db;
string str;
cin>>a>>b>>db>>str;
cout<<a<<endl<<b<<endl<<setiosflags(ios::fixed)<<setprecision(2)<<db<<endl<<str<<endl;
//
cin.getline(b,5); //只能讀4個+\0
cout<<b<<endl;
getline(cin,str);
cout<<str<<endl;
1.4 scanf / printf
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const double PI = acos(-1.0);
const int INF = 0x3fffffff; //無窮大, 7個f
int main(){
int a;
scanf("%d",&a);
printf("%d",a);
printf("%6d",a); // 右對齊,不足6位的補空格
printf("%06d",a); // 右對齊,不足6位的補0
long long b;
scanf("%lld",&b);
printf("%lld",b); // 保留小數
char c;
scanf("%c",&c); // 可讀空白(空格、/n、/t)
printf("%c",c);
double d;
scanf("%lf",&d); // %lf
printf("%f",d);
printf("%.2f",d);
char s[20];
scanf("%s",s); // 沒有 &
printf("%s",s);
string str;
cin>>str;
printf("%s",str.c_str());
return 0;
}
2 條件、數組
2.1 if
if(n){ // n != 0
...
}
If(!n){ // n == 0
...
}
2.2 switch
int a = 1 , b=1;
int c = 5;
switch(c){
case 2:
case 3:
case 4:
printf("%d",a+b); //c=2 || c=3 || c=4
break;
case 5:
printf("%d",a-b);
break;
default:
printf("end");
}
2.3 數組
二維初始化:
int arr[3][6] = {{1,2,3},{},{4,5}};
當數組較大()時, 設置全局變量
3 指針
unsigned類型的整數
3.1 指針變量
搞懂以下代碼
int a;
int *b = &a; // b指向a,*b = a的值(內容),b = a的地址
a = 111;
printf("%d %d\n",*b,b);
//
*b = 222; // 改變a
printf("%d %d\n",*b,a);
//
b ++; // 跨越了一個int存儲單元(4B)
printf("%d\n",b);
3.2 指針數組
int a[5];
for(int i=0; i<5; i++){
scanf("%d",a+i); // 相當於 scanf("%d", &a[i]);
}
for(int i=0;i<5;i++){
printf("%d ",*(a+i)); // 相當於 printf("%d ",a[i]);
}
//
int *p;
p = a; //相當於 p = &a[0];
for(; p<a+5; p++){
scanf("%d", p);
}
for(p=a; p<a+5; p++){
printf("%d ",*p);
}
4 函數
4.1 傳遞參數:變量
一共有5種情形,對應如下5個swap函數
// 不改變main中變量(函數中的變量是main中的副本)
void swap1(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}
//引用:改變main中的變量(函數中的變量是main中的別名)
void swap2(int &a, int &b){
int temp;
temp = a;
a = b;
b = temp;
}
// 地址傳值/指針傳值:改變main中的變量
void swap3(int * a, int * b){
int temp; // int *temp;
temp = *a; // *temp = *a; 運行錯誤,理由:temp未初始化,存放隨機
*a = *b;
*b = temp;
}
// 不改變main中變量
void swap4(int * a, int * b){
int *temp;
temp = p;
p = q;
q = temp;
}
// 指針引用:不改變main中變量
void swap5(int * &p, int * &q){
int *temp;
temp = p;
p = q;
q = temp;
}
int main(){
int a=1, b=2;
swap1(a, b);
swap2(a, b); // 交換
swap3(&a, &b); // 交換
swap4(&a, &b);
int *p = &a, *q = &b;
swap5(p, q);
return 0;
}
4.2 傳遞參數:數組
void fun(int a[], int b[][3]){
...
}
int main(){
int a[10];
int b[5][3];
fun(a, b);
return 0;
}
5 其他
5.1 int / long long(空間大小)、OJ最大運算次數(時間大小)
一般比賽平臺,最大運算次數:
類型 | 位數 | 可表示範圍 |
---|---|---|
int | 32 | () ~~~ () |
long long | 64 | () ~~~ () |
總結:
- x <= 或 32位,用int
- x <= 或 64位,用long long
5.2 ASCII碼
小寫 = 大寫 + 32
字符 | ASCII碼 |
---|---|
‘0’ | 48 |
‘A’ | 65 |
‘a’ | 97 |
char c = 117;
printf("%c",c);
char c = '9';
printf("%d",c);
5.3 數據類型的轉換
-
char*(字符串數組)<——> int、double、 char*(字符串數組)
sscanf()和sprintf()
詳情見:此文章:3 cstring
-
char*(字符串數組) <——> string
詳情見:此文章:2 string
-
int <——> double
int a; double db; a = (int)db; db = (double)a;
5.4 邏輯運算符
類型 | 代碼 |
---|---|
與 | && |
或 | || |
非 | ! |
位與 | a & b |
位或 | a | b |
位異或 | a ^ b |
取反 | ~ a |
1左移8位:
1<<8 =
5. 5 條件運算符
int max(int a, int b){
return a>b?a:b ;
}
5.6 typedef
當複雜數據類型出現太多,起別名
typedef 數據類型 別名
如:typedef long long LL;
#include<cstdio>
using namespace std;
typedef long long LL;
int main(){
LL a = 23LL;
printf("%lld",a);
return 0;
}
5.7 浮點數的比較
#include<cstdio>
#include<cmath>
using namespace std;
const double eps = 1e-8; // 1*10^(-8)
// define中定義的函數:變量帶(),返回bool
#define Equ(a,b) ( (fabs((a)-(b))) < (eps)) // a==b: b-eps< a <b+eps
#define More(a,b) ( ((a)-(b)) > (eps)) // a>b: a > b+eps
#define Less(a,b) ( ((a)-(b)) < (-eps)) // a<b: a < b-eps
#define MoreEqu(a,b) ( ((a)-(b)) > (-eps)) // a>=b: a > b-eps
#define LessEqu(a,b) ( ((a)-(b)) > (eps)) // a<=b: a < b+eps
int main(){
double db1 = 4 * asin(sqrt(2.0)/2); // 4 * pi/4
double db2 = 3 * asin(sqrt(3.0)/2); // 3 * pi/3
// 浮點數的比較
printf("%d\n",db1==db2); // 不精確
printf("%d\n",Equ(db1,db2)); // 精確
// 修正
double res = -0.00; //某個結果
printf("%f\n",res+eps); // 修正
return 0;
}