C++語法衝刺(2)

mouyilai

目錄

  • for while loop
  • array
  • string

插播

建北電資自己的OJ上線啦!!!!!!!!

OJ

去註冊

感謝FN學長 && lemon學長

據說有學長想被追蹤(?

@_cs.g_94

@ckeisc41st_urmoron

@ckeisc41st_penghc

loops

迴圈

這是什麼

總之可以讓你方便很多的東西。

如果我想要宣揚丁瑞萱有多可愛

所以我想要輸出10次丁瑞萱好可愛......

#include <iostream>
using namespace std;
int main() {
	cout << "丁瑞萱好可愛<3" << endl;
	cout << "丁瑞萱好可愛<3" << endl;
	cout << "丁瑞萱好可愛<3" << endl;
	cout << "丁瑞萱好可愛<3" << endl;
	cout << "丁瑞萱好可愛<3" << endl;
	cout << "丁瑞萱好可愛<3" << endl;
	cout << "丁瑞萱好可愛<3" << endl;
	cout << "丁瑞萱好可愛<3" << endl;
	cout << "丁瑞萱好可愛<3" << endl;
	cout << "丁瑞萱好可愛<3" << endl;
	cout << "丁瑞萱好可愛<3" << endl;
}

寫一行輸出再複製9次 (?)

怪東西

來自lemon學長ㄉ補充 :

寫一行 再複製一行 

貼一行 再複製兩行

貼兩行 再複製四行

貼四行 再複製兩行

貼兩行 完成

 

這樣你假設你要輸出 \(n\) 行 你只要操作 \(log_2 \ n\) 次喔喔

但這樣如果要輸出100次的話

你會複製到想哭

快樂loop~~~~~~

#include <iostream>
using namespace std;
int main() {
	int i=0;
	while(i<10){
		cout << "丁瑞萱好可愛!" << endl;
		i++;
	}
}
#include <iostream>
using namespace std;
int main() {
	for(int i=0; i<10; ++i){
		cout << "丁瑞萱好可愛<3" << endl;
	}
}

while loop

while迴圈

while loop

while(/*條件*/){
  /*做事*/
}

條件跟if一樣要給它一個bool

如果條件符合(bool為true)才會執行後面的動作

栗子

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int i = 0;
  while(i < n){
    cout << "丁瑞萱好可愛!" << endl;
    i++;
  }
}

輸出n次丁瑞萱好可愛

栗子

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  while(n--){
    cout << "丁瑞萱好可愛!" << endl;
  }
}

輸出n次丁瑞萱好可愛

btw 寫--n的話只會執行n-1次喔喔喔

another 栗子

#include <iostream>
using namespace std;
int main(){
  int n;
  cin >> n;
  int sum = 0, i = 1;
  while(i <= n){
      sum += i;
	  i++;
  }
  cout << sum << '\n';
}

從1加到n

another 栗子

#include <iostream>
using namespace std;
int main(){
  int n, k;
  cin >> n >> k;
  int i = 1;
  while(i <= n){
    if(i % k == 0) cout << i << endl;
    i++;
  }
}

輸出n以內所有k的倍數

another 栗子

#include <iostream>
using namespace std;
int main(){
  int n, k;
  cin >> n >> k;
  int i = 1;
  while(i <= n){
    if(i % k == 0) cout << i << endl;
    i++;
  }
}

輸出n以內所有k的倍數

無限迴圈

#include <iostream>
using namespace std;
int main(){
  while(true){
    cout << "學妹好可愛<3" << endl;
  }
}

來跑跑看

記得不要讓你ㄉ迴圈變成無限迴圈喔喔喔

EOF

阿什麼是EOF

先給你維基自己看

end of file的縮寫

簡單來說就是不告訴你有幾筆測資 讀就對ㄌ

怎麼寫~~~~~~~~~~~~~~~~~~~~~

while(/*cin >> */){
  /*做事*/
}

怎麼寫~~~~~~~~~~~~~~~~~~~~~

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

假如我今天要a+b 但請你讀到EOF

重要的東西(?

手動輸入測資的時候 可以一直輸下去

 

如果題目要你在輸入若干筆測資後再做事情

在手輸測資的時候按ctrl + Z代表EOF

不然你也可以寫檔(應該之後會教(?

for loop

for 迴圈

其實跟while 迴圈差不多啦哈哈

for(/*初始化*/; /*條件*/; /*跑完一次迴圈要做什麼*/){
  /*做事情*/
}

順序:

初始化(只會在進入迴圈第一次時跑)

判斷條件是否符合

執行迴圈裡的程式

對值進行調整

栗子

輸出n次丁瑞萱好可愛

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int i;
  for(i=0; i<n; ++i){
    cout << "丁瑞萱好可愛" << endl;
  }
}

another 栗子

從1加到n

#include <iostream>
using namespace std;
int main(){
  int n;
  cin >> n;
  int sum = 0, i;
  for(i=1; i<=n; ++i){
    sum += i;
  }
  cout << sum << '\n';
}

another 栗子

從1加到n

#include <iostream>
using namespace std;
int main(){
  int n;
  cin >> n;
  int sum = 0, i;
  for(i=1; i<=n; ++i){
    sum += i;
  }
  cout << i << ' ' << sum << '\n';
}

也可以把變數宣告在裡面

但在這個迴圈外面就不能用ㄌ

差不多是每個變數都只能活在離它最近ㄉ大括號裡面的概念(?)

#include <iostream>
using namespace std;
int main(){
  int n;
  cin >> n;
  int sum = 0;
  for(int i=1; i<=n; ++i){
    sum += i;
  }
  cout << i << ' ' << sum << '\n';
}

這可以

這不行

break & continue

break

while(/*條件1*/){
  if(/*條件2*/){
    /*做事1*/
    break;
  }
  /*做事2*/
}
/*做事3*/
for(/*初始化*/, /*條件1*/, /*更新*/){
  if(/*條件2*/){
    /*做事1*/
    break;
  }
  /*做事2*/  
}
/*做事3*/

如果跑到break的話,

就直接跳到迴圈外面ㄌ

continue

while(/*條件1*/){
  if(/*條件2*/){
    /*做事1*/
    continue;
  }
  /*做事2*/
}
/*做事3*/
for(/*初始化*/, /*條件1*/, /*更新*/){
  if(/*條件2*/){
    /*做事1*/
    continue;
  }
  /*做事2*/  
}
/*做事3*/

如果跑到continue的話,

會直接跳到下一次的迴圈

do-while

do-while

do{
  /*做事*/
}
while(/*條件*/);

先做do裡面寫的東西再來檢查條件

條件符合就繼續做

最後面要記得加分號喔喔喔

什酢

寫寫題目

小提醒 : 可以在迴圈裡面放迴圈喔喔喔

for(int i=0; i<n; ++i){
  for(int j=0; j<m; ++j){
  
  }
}

之類的

array

陣列

假如我要記錄10個學生的成績......

int main(){
  int student_1, student_2, student_3, student_4, student_5;
  int student_6, student_7, student_8, student_9, student_10; 
  cin >> student_1;
  cin >> student_2;
  cin >> student_3;
  cin >> student_4;
  cin >> student_5;
  cin >> student_6;
  cin >> student_7;
  cin >> student_8;
  cin >> student_9;
  cin >> student_10;
}

你可能會這樣:

那如果我要記錄100個學生ㄉ成績ㄋ

假如我要記錄100個學生的成績......

int main(){
  int student[10];
  for(int i=0; i<10; ++i)
  	cin >> student[i];
}

使用我們今天介紹ㄉ陣列(O

複製到手快斷掉(X

怎麼做ㄋ

btw如果只想讓for迴圈做一行的事情的話可以把大括號省略喔喔喔

陣列宣告 & 初始化

/*陣列宣告*/
/*陣列型態 陣列名字[幾格] = {初始化}*/
int arr[50];
int b[10] = {0}; 

如果變數像是盒子

那麼陣列就像是櫃子

by北資電神 丁瑞萱

array

陣列

C++語法衝刺的啦(2)

By mouyilai

C++語法衝刺的啦(2)

  • 208