編程序 = 給計算機設計好運行步驟ios
程序 = 人們用來告訴計算機應該作什麼的東西c++
問題➡️該告訴計算機什麼?用什麼形式告訴?編程
問題1: 是否是不管什麼」單詞「,計算機都能明白數組
問題2: 是否是咱們不管在程序裏寫什麼「數」和「計算符號」,計算機都能明白?ui
問題3: 咱們須要多少「句式」纔夠描述各類邏輯spa
答案1:NO。定義一些有特定含義的「關鍵字」,計算機只能明白這些字的意義。設計
能夠經過少許詞定義更多詞(30+關鍵字)code
答案2:NO。定義一些「數據的類型」和相應的「操做符號」,計算機只能明白這些類型。(10+基本數據類型,30+運算符號)排序
答案3:順序、分支、循環,只須要3種。ci
#include <iostream> using namespace std; int main() { //Type your code in here retuen 0; }
定義變量,輸出數據:
int a = 0; cout<<a<<endl;
定義變量,輸入數據:
int a = 0; cout<<"請輸入一個數"<<endl; cin>>a; cout<<"我剛剛輸入的a:"<<a<<endl;
實型變量,順序語句:
float a = 0,b = 0,temp = 0; cout<<"Input a and b:"<<endl; cin>>a>>b; cout<<"a = "<<a<<",b = "<<b<<endl; temp = a; a = b; b = temp; cout<<"a = "<<a<<",b = "<<b<<endl;
分支語句(if語句)
int x = 0,y = 0; cin>>x>>y; if(x>y) cout<<"Max number is:"<<x<<endl; else cout<<"Max number is:"<<y<<endl;
循環語句(for語句)
int i = 0; cout<<"20之內的奇數: "<<endl; for(i = 0;i < 20;i++) { if(i%2!=0) cout<<i<<endl; }
循環語句,數組
int i = 0; char a[10] = {'a','b','c','d','e','f','g','h','i','j'} cout<<"字母表中序號爲奇數的前五個字母:"<<endl; for(i = 0; i < 10; i=i+2) { cout<<a[i]<<endl; }
綜合程序,註釋和縮進增長可讀性
char a='';//用於存放用戶輸入的字母 cout<<"猜我是哪一個字母,最多5次:" <<endl; int i = 0;//用於記錄猜過多少次 for(i = 0;i < 5;i++) { cin>>a; if(a=='G')//若是猜中 { cout<<"Good Job!"<<endl; break;//終止循環 } else //若是沒有被猜中 cout<<"Wrong! Try it again!" }
實現冒泡排序
#include <iostream> using namespace std; int main() { int n, a[1000];//一共n個數,n不超過1000,a用來保存這些數 cin>>n;//輸入n個數 for (int i = 0; i < n; i++) { cin>>a[i]; } //冒泡,不斷比較相鄰的兩個數,若是順序錯了,那麼就交換 for (int i = 0; i < n; i++) { for (int j = 1; j < n-i; j++) { if(a[j-1]>a[j]) { int temp = a[j]; a[j] = a[j-1]; a[j-1] = temp; } } } // 依次輸出 for (int i = 0; i < n; i++) { cout<<a[i]<<endl; } return 0; }
奇偶排序(一)
#include "iostream" using namespace std; int main(){ int a[10]; for (int i = 0; i < 10; i++) { cin >> a[i]; } //首先,咱們把奇數放到數組左邊,偶數放到數組右邊 int l = 0, r = 9;//用左手和右手分別指向數組兩端 while (l <= r){ bool leftIsOdd = a[l] % 2 == 1; bool rightIsEven = a[r] % 2 == 0; if (leftIsOdd){ l++; } else if (rightIsEven){ r--; } else if (!leftIsOdd && !rightIsEven){ int temp = a[l]; a[l] = a[r]; a[r] = temp; } } //對l左邊(奇數部分)進行冒泡排序 int start = 0, end = l; for (int i = start; i < end-1; i++) { for (int j = start+1; j <start + end - i ; j++) { if(a[j-1] > a[j]){ int temp = a[j-1]; a[j-1] = a[j]; a[j] = temp; } } } //對l右邊(偶數部分)進行冒泡排序 start = l, end = 10; for (int i = start; i < end - 1; i++) { for(int j = start + 1; j < start + end - i;j++){ if(a[j-1] > a[j]){ int temp = a[j-1]; a[j-1] = a[j]; a[j] = temp; } } } for (int i = 0; i < 10; i++) { cout<<a[i]<<' '; } return 0; }
奇偶排序(二)
#include <iostream> using namespace std; int main(){ int a[10]; for (int i = 0; i < 10; i++) { cin>>a[i]; } //冒泡,不斷比較相鄰的兩個數,若是順序錯了,那麼就交換 for (int i = 0; i < 9; i++) { for (int j = 1; j < 10-i; j++) { //與剛纔的冒泡排序不一樣,咱們不僅是經過比較數字大小決定順序 //若是左邊的爲偶數,右邊的爲奇數,順序也是須要顛倒的 bool leftIsEven = a[j - 1] % 2 == 0; bool rightIsEven = a[j] % 2 == 0; if((leftIsEven && !rightIsEven) || (leftIsEven == rightIsEven && a[j-1]>a[j])){ int temp = a[j - 1]; a[j - 1] = a[j]; a[j] = temp; } } } for (int i = 0; i < 10; i++) { cout<<a[i]<<' '; } return 0; }