C,C++語法基礎 | 變數,表示式與順序語句
變數,表示式與順序語句
#include<iostream>
using namespace std;
int main(){
cout << "Hello World" << endl;
return 0;
}
首先是最簡單的一個C++
程式, 執行結果列印輸出Hello World
.
標頭檔案
這裡先介紹兩個有關於輸入輸出的標頭檔案
#include<cstdio> #include<iostream> #include<bits/stdc++.h> #include<cmath> #include<string>
#include<cstdio>
包含了printf
和scanf
#include<iostream>
包含了cin
和cout
,endl
#include<bits/stdc++.h>
萬能標頭檔案,基本包含了全部的標頭檔案
#include<cmath>
基本所有和數學標頭檔案都在#include<cmath>
#include<string>
使用字串
名稱空間
using namespace std;
std
是個名稱空間,類似於cout
這些都是定義在std
中的.
名稱空間的主要用途就是用來防止命名衝突的.
如果沒有書寫上using namespace std;
cout
的話,就會爆出錯誤.
解決方法:
一個就是加上名稱空間using namespace std;
,一般都是推薦這麼做
還有就是使用std::cout
, 一般是不會這麼寫的
主函式
int main(){
// 邏輯程式碼
return 0;
}
變數的定義
變數必須先定義才能使用,不能重名.
變數的定義方式:
#include<iostream> #include<cstdio> using namespace std; int main(){ int a = 5; int b,c = a,d = 10 / 2; // 這裡定義按照 型別 __,__,__ cout << a << " " << b << " " << c << " " << d << endl; // 逗號表示式是輸出逗號的最後一個 cout << (5,4,3,2) << endl; return 0; }
常用變數型別
變數的定義方式
型別 __ , __ , __ ;
下劃線部分可以填入變數,或者變數直接賦值
#include<iostrream>
using namespace std;
int main(){
int a;
int a,b=2,c=4; // 注意按照逗號分開看
float d=1.5,e=1,f=1.235e4; // int也是特殊的float
bool g=false,h=false;
char j='k',k='b'; // 注意要有單引號
long long l = 12321LL;
long double m = 123.154;
return 0;
}
注意: long long
在存常數的時候結尾要加上LL
,否則就還是按照int
來存放
整數的輸入輸出
首先展示cin/cout
的輸入輸出
// 下面都是一樣的
cin >> a >> b;
cin >> a;
cin >> b;
cout << a+b << " " << a*b << endl
cout << a+b;
cout << " ";
cout << endl;
#include<iostream>
using namespace std;
int main(){
int a,b; // 定義兩個變數
cin >> a >> b; // 輸入
cout << a+b << endl; // 輸出
return 0;
}
展示scanf/printf
的輸入輸出
#include<iostream>
#include<cstdio>
int main(){
int a,b;
scanf("%d%d",&a,&b); // 輸入a,b 整數
print("a+b=%d\na*b=%d",a+b,a*b);
// 浮點數的輸入輸出
// 注意怎麼輸出保留的小數點
float c,d;
scanf("%f%f",&c,&d); // 輸入a,b 整數
print("c+d=%.2f\nc*d=%.3f",c+d,c*d);
char e,f;
scanf("%c %c",&e,&f); // 輸入a,b 整數
print("%c %c\n",e,f);
return 0;
double a,b;
scanf("%lf %lf",&a,&b);
printf("a+b=%.2lf\na*b=%.2lf",a+b,a*b);
long long a,b;
scanf("%lld %lld",&a,&b);
printf("a+b=%lld\na*b=%lld",a+b,a*b);
}
注意: %c
是會讀入空格的,所以需要把空格過濾一下,但是%d
是不會讀入空格的
在c++
中,bool
被當作int
來處理
cout
保留幾位小數這個是比較難寫的,不建議大家去看. 需要使用到格式化輸出就是使用printf
cin/cout
和printf/scanf
的不同在於前者是不需要判斷變數的型別的,而後者是需要判斷變數的型別的.
還有一個就是效率的問題,cin/cout
就是使用起來比較方便,但是效率的話scanf/printf
會高很多, 特別是大資料讀取的時候,推薦使用scanf/printf
取模運算
在C++
中的取模運算和數學上的取模還不太一樣,數學上要求兩個數大於等於0,並且被除數嚴格小於除數. 但是在c++
中,可以支援負數的模運算,取決於除數的符號.
5 % 2 ==> 1
-5 % 2 ==> -1
-5 % -2 ==> -1
自增和自減
#include<iostream>
using namespace std;
int main(){
int a=6;
int b=6;
cout << a++ << endl; // 6
cout << ++a << endl; // 8
cout << ++b << endl; // 7
return 0;
}
a++
是先用a
的值,然後再進行+1
,++a
是先進行+1
操作.
變數的強制轉換
不同的變數型別之間是可以相互賦值的.
float
單精度向double
雙精度變化是沒有任何問題的,如果是double
向float
轉的話會損失一些精度.
int
變成(float,double)
是直接就行變化的,如果是(float,double)
轉為int
,則是進行下取整
int
和char
的轉換就依賴於ASCII
碼錶, 其實char
本質上就是一個整數
#include<iostream>
using namespace std;
int main(){
int a = 5;
float b = (float)a; // 強制轉換為浮點型別
printf("%f",b) // 5.000000
float a = 5.23;
float b = (int)a; // 強制轉換為int型別
printf("%d",b) // 輸出5
int a = 97;
char c = (char)a;
print("%c",c) // 輸出a
char a = 'a';
char c = (int)a;
print("%c",c) // 輸出97
return 0;
}
char
是可以直接和int
進行運算的,運算結果就是int
.
char a = 'A';
cout << a + 32 << endl; // 97
cout << (char)(a+32) << endl; // 'a'
習題一
A+B
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int a,b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
差
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int a,b,c,d;
cin >> a >> b >> c >> d;
cout << "DIFERENCA = " << (a*b - c*d) << endl; // 這裡還輸出了字串型別
return 0;
}
這種輸入的話, 如果是整數,c++
輸入的時候會過濾空格和回車,這個比Java
方便很多
圓的面積
#include<bits/stdc++.h>
using namespace std;
int main(){
double pi = 3.14159,r;
cin >> r;
printf("A=%.4lf", r * r * pi);
return 0;
}
在做演算法題的時候,能用double
儘量使用double
,不要使用float
平均數1
#include<bits/stdc++.h>
using namespace std;
int main(){
double a,b;
scanf("%lf %lf",&a,&b);
printf("MEDIA = %.5lf",(3.5*a + 7.5*b)/11);
return 0;
}
工資
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int n,t;
double m;
cin >> n >> t >> m; // cin是可以不區分資料型別直接輸入的
printf("NUMBER = %d\nSALARY = U$ %.2lf",n,m*t);
return 0;
}
油耗
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
double X,Y;
cin >> X >> Y;
printf("%.3lf km/l",X/Y);
return 0;
}
兩點的距離
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main(){
double x1,x2,y1,y2;
cin >> x1 >> y1;
cin >> x2 >> y2;
printf("%.4lf",sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
return 0;
}
鈔票
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int a[7] = {100,50,20,10,5,2,1},n;
cin >> n;
cout << n << endl;
for(int i=0;i<7;i++){
printf("%d nota(s) de R$ %d,00\n",n/a[i],a[i]);
n %= a[i]; // %相當於把這個數剔除,不能再和這個數進行整除
}
return 0;
}
時間轉換
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int n;
cin >> n;
printf("%d:%d:%d",n/3600,n%3600/60,n%3600%60);
return 0;
}
注意: %
相當於把可以被整除得數給剔除了,而/
相當於求出可以被整除的個數
簡單乘積
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int a,b;
cin >> a >> b;
printf("PROD = %d",a*b);
return 0;
}
簡單計算
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int a0,a1,b0,b1;
double a2,b2;
cin >> a0 >> a1 >> a2;
cin >> b0 >> b1 >> b2;
printf("VALOR A PAGAR: R$ %.2lf",a1*a2+b1*b2);
return 0;
}
球的體積
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int r;
cin >> r;
double pi = 3.14159;
printf("VOLUME = %.3lf",4/3. * pi * r * r * r);
return 0 ;
}
面積
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
double pi = 3.14159;
double a,b,c;
cin >> a >> b >> c;
printf("TRIANGULO: %.3lf\n",a*c/2);
printf("CIRCULO: %.3lf\n",c*c*pi);
printf("TRAPEZIO: %.3lf\n",(a+b)*c/2);
printf("QUADRADO: %.3lf\n",b*b);
printf("RETANGULO: %.3lf\n",a*b);
return 0;
}
平均數2
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
double a,b,c;
cin >> a >> b >> c;
printf("MEDIA = %.1lf",(2*a+3*b+5*c)/10);
return 0;
}
工資和獎金
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main(){
string name;
cin >> name;
double a,b;
cin >> a >> b;
printf("TOTAL = R$ %.2lf\n",a+b*0.15);
return 0;
}
最大值
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int a,b,c,max;
cin >> a >> b >> c;
a > b ? max = a : max = b;
max > c ? max = max : max = c;
printf("%d eh o maior",max);
return 0;
}
距離
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int L;
cin >> L;
printf("%d minutos",L*2);
return 0;
}
燃料消耗
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
double s,t;
cin >> s >> t;
printf("%.3lf",s*t/12);
return 0;
}
鈔票和硬幣
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
cout << "NOTAS:" << endl;
// 擴大100倍數處理
double n;
cin >> n;
n *= 100;
int m = (int)n;
int a[12]={10000,5000,2000,1000,500,200,100,50,25,10,5,1};
int ans[12]; // 存放答案
for(int i=0;i<12;i++){
ans[i] = m/a[i];
m %= a[i];
}
for(int i=0;i<12;i++){
if(i>5){
if(i==6)cout << "MOEDAS:" << endl;
printf("%d moeda(s) de R$ %.2lf\n",ans[i],a[i]/100.);
}else{
printf("%d nota(s) de R$ %.2lf\n",ans[i],a[i]/100.);
}
}
return 0;
}
這個可以把整數和小數分開處理.
強制轉int
就可以實現向下取整,還有就是需要注意的是浮點數是沒有取餘數的運算的.
所以最好的做法就是先把這個數擴大100倍.
天數轉換
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
printf("%d ano(s)\n",n/365);
printf("%d mes(es)\n",n%365/30);
printf("%d dia(s)\n",n%365%30/1);
return 0;
}