C++學習小結之語句
一、順序語句
二、條件,分支語句
1、if語句
關鍵是能夠熟練運用 if的嵌套。要考慮好所有的情況。
如果說 條件是兩種情況相互對應的,那么就可以只用 if 與else 。但必須要想好 每個else 跟哪個if是一對。
如果情況是相互獨立的三種情況以上,那么可以選擇運用if ... else if ...else。
1.if語句
if(條件)
{
滿足條件的時候執(zhí)行;
}
2. if(條件)
{
滿足條件執(zhí)行;
}
else
{
不滿足條件時執(zhí)行;
}
3 if(條件1)
{
滿足條件1的時候執(zhí)行;
}
else if(條件2)
{
不滿足條件1的情況下滿足條件2;
}
4.
if(條件1)
{
if(條件2)
{
既滿足條件1又滿足條件2的時候執(zhí)行;
}
}
2、switch 語句
如果說可選的條件比較多時,選擇switch語句,要比if語句效率要高。特別注意的是 case 后跟的break。
eg:
//eg.6 swtich語句 作用域
static void Maine(string[] args)
{
//Console.WriteLine("你本次選擇出場的英雄是:");
Random r = new Random();
int n = r.Next(10);
string a;
switch (n)
{
case 1:
a = "趙信"; break;
case 2:
a = "寒冰射手";break;
case 3:
a = "無極劍圣";break;
case 4:
a = "機器人"; break;
default:
a = "齊天大圣";break;
}
Console.WriteLine("本次選擇的英雄是:"+a);
}
三、循環(huán)語句
for循環(huán)
四要素:
初始條件,循環(huán)條件,狀態(tài)改變,循環(huán)體。 執(zhí)行過程:
初始條件--循環(huán)條件--循環(huán)體--狀態(tài)改變--循環(huán)條件....
注意:for的小括號里面分號隔開,for的小括號后不要加分號。
利用 加斷點的方式,可以更好的明白for的工作原理。
1.for循環(huán)空操作完成的實例, 輸出100以內的數(shù)字
static void Main(string[] args) { int i = 1; for (; ; ) { if (i > 100) { break; } Console.Write(i + "\t"); i++; } Console.ReadKey(); }
當然你也可以用 while,if() break;的嵌套完成上述操作。
.正序和逆序的推斷問題。 (折紙問題)
//eg.5 折紙問題
static void Maine(string[] args) { //Console.WriteLine("請輸入次數(shù)"); //int n = Convert.ToInt32(Console.ReadLine()); //int i = 0; //for (double sum = 0.0001; sum <= 8848.0; sum = sum * 2) //{ // i++; //} //Console.WriteLine(i); double sum = 0.0001; int z = 0; for (int i = 0; ; i++) { z++; sum = sum * 2; if (sum >= 8848.0) { Console.WriteLine(z); break; } } }
.應用:a.窮舉法: 用循環(huán)把各種可能的情況都給走一遍,然后用if條件把滿足要求的結果給篩選出來。
//eg.6 百馬百石 大馬馱2石,中馬馱1石 小馬馱0.5石
static void Main6a(string[] args) { for (int i = 0; i <= 50; i++) { for (int j = 0; j <= 100; j++) { for (int k = 0; k <= 200; k++) { if ( (i * 2 + j * 1 + k * 0.5 == 100) && (i + j + k == 100) ) { Thread.Sleep(50); Console.WriteLine("大馬需要" + i + "頭,中馬需要" + j + "頭,小馬需要" + k + "頭。"); } } } } }
//eg.7
static void Maing(string[] args) { for (int i = 1; i < 10; i++) { for (int j = 1; j < 5; j++) { for (int k = 1; k < 25; k++) { if (i * 5 + j * 10 + k * 25 == 50) { Console.WriteLine("50元用來買" + i.ToString() + "個牙刷," + j.ToString() + "個牙膏," + k.ToString() + "塊肥皂,正好能用完。"); } } } } }
//eg.8 有1塊,2塊,5塊的錢若干,湊出20塊錢,有幾種湊法
static void Mainh(string[] args) { int m = 0; for (int i = 0; i <= 20; i++) { for (int j = 0; j <= 10; j++) { for (int k = 0; k < 4; k++) { if (i * 1 + 2 * j + 5 * k == 20) { m++; Console.WriteLine("一共有" + m + "中方法。"); Console.WriteLine("需要1元的" + i + "張,2元的" + j + "張,5元的" + k + "張。"); } } } } }
//eg.9 1 () 2 () 3 ()4 = 4;問括號里我要填 (- 或 +)
static void Maini(string[] args) { for (int i = 1; i <= 1; i += 2) { for (int j = -1; j <= 1; j += 2) { for (int k = -1; k <= 1; k += 2) { for (int l = -1; l <= 1; l += 2) { if (1 * i + 2 * j + 3 * k + l * 4 == 4) { Console.WriteLine("i=" + i + ",j=" + j + ",k=" + k + ",l=" + l + "。"); } } } } } }
//eg.10 123()45()67()8()9=100;要求在()里面填寫+或-使等式成立。
static void Maini2(string[] args) { for (int a = -1; a <= 2; a += 2) { for (int b = -1; b <= 2; b += 2) { for (int c = -1; c <= 2; c += 2) { for (int d = -1; d <= 2; d += 2) { if (123 + a * 45 + b * 67 + c * 8 + d * 9 == 100) Console.WriteLine("a=" + a + ",b=" + b + ",c=" + c + ",d=" + d); } } } } Console.ReadKey(); }
//eg.11 某偵查隊接到一項緊急任務,要求在A.B.C,D,E,F六名隊員中盡可能多的挑選若干人。A和B兩人必須去一人。A和D不能同時去。A,E,F三人必須兩人去。B和C都
//去或都不去。C和D兩人中去一人。若D不去,E也不去。問應叫哪幾個人去?(靈活運用1與0)
static void Mainj(string[] args) { for (int a = 0; a <= 1; a++) { for (int b = 0; b <= 1; b++) { for (int c = 0; c <= 1; c++) { for (int d = 0; d <= 1; d++) { for (int e = 0; e <= 1; e++) { for (int f = 0; f <= 1; f++) { if ((a + b >= 1) && (a + d <= 1) && (a + e + f == 2) && (b + c != 1) && (c + d == 1) && (d - e >= 0)) { Console.WriteLine("A=" + a + "B=" + b + "C=" + c + "D=" + d + "E=" + e + "F=" + f); } } } } } } } } //老師版 static void Mainj1(string[] args) { int a, b, c, d, e, f; for (a = 0; a < 2; a++) { for (b = 0; b < 2; b++) { for (c = 0; c < 2; c++) { for (d = 0; d < 2; d++) { for (e = 0; e < 2; e++) { for (f = 0; f < 2; f++) { if ((a + b >= 1) && (a + d <= 1) && (a + e + f == 2) && (b + c != 1) && (c + d == 1) && ((d + e == 0) || d == 1)) { Console.WriteLine("A=" + a + "B=" + b + "C=" + c + "D=" + d + "E=" + e + "F=" + f); } } } } } } } Console.ReadKey(); }
b.迭代法:有一定規(guī)律。 每次循環(huán)都是從上次運算結果中獲得數(shù)據(jù),本次運算的結果都是要為下次運算做準備。
eg1 兔生兔問題
有一對幼兔,幼兔一個月后成長為小兔,小兔一個月后成長為成兔并生下一對幼兔,問幾年后有多少對兔子,其中幼兔,小兔,成兔分別是多少?
//eg.2 兔生兔問題
//方法一 static void Maink3(string[] args) { int syt = 1, byt = 0; int sxt = 0, bxt = 0; int sct = 0, bct = 0; Console.WriteLine("請輸入月數(shù):"); int month = Convert.ToInt32(Console.ReadLine()); int sum; for (int i = 1; i <= month; i++) { //賦值順序不能變,必須按照兔子生長規(guī)律來,先有的bct才會有byt bct = sxt + sct; bxt = syt; byt = sxt + sct; //bct = sxt + sct; 這樣寫,必須注意他的順序 //bxt = syt; //byt = bct; //byt = bct;//錯誤的 //bxt = syt; //bct = sxt + sct; syt = byt; sxt = bxt; sct = bct; //sum = byt + bxt + bct; } sum = byt + bxt + bct; Console.WriteLine("過了{0}個月后,幼兔個數(shù)為{1}對,小兔個數(shù)為{2}對,成兔個數(shù)為{3}對,總共有{4}對。", month.ToString(), byt, bxt, bct,sum); } //方法二 static void Maink4(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); int tu = 0;//要求那個月的總數(shù) int tu1=1, tu2=1;//倒數(shù)第一個為 tu1,倒數(shù)第二個為 tu2 for (int i = 3; i < n;i++ ) { tu = tu1 + tu2; tu2 = tu1; tu1 = tu; } Console.WriteLine(tu); } //方法三 static void Maink5(string[] args) { Console.Write("請輸入月數(shù):"); int m = int.Parse(Console.ReadLine()); int ct = 0;//成兔的對數(shù) int xt = 0;//小兔的對數(shù) int yt = 1;// int zt = 1;// for (int i = 1; i <= m; i++) { if (i == 1) { ct = 0; xt = 0; yt = 1; } else { ct = xt + ct; xt = yt; yt = ct; } zt = yt + xt + ct; Console.WriteLine(i.ToString() + "個月后成兔的對數(shù)是:" + ct.ToString()); Console.WriteLine(i.ToString() + "個月后小兔的對數(shù)是:" + xt.ToString()); Console.WriteLine(i.ToString() + "個月后幼兔的對數(shù)是:" + yt.ToString()); Console.WriteLine(i.ToString() + "個月后兔子的總對數(shù)是:" + zt.ToString()); } Console.ReadLine(); }
eg 2 100以內的所有數(shù)的和。
eg3. 求階乘。
eg4.求年齡。
eg5.折紙。
eg6.棋盤放糧食。
eg7.猴子吃桃子。
static void Maink(string[] args) { int sum = 1; for (int i = 0; i < 6; i++) { int t = (sum + 1) * 2; sum = t; } Console.WriteLine("桃子一共有:" + sum + "個。"); }
eg8.落球問題。一個球從10米高度落下,每次彈起2/3的高度,問第五次彈起后的高度?
四、while 循環(huán)。一般用在一些死循環(huán)當中。
五、try catch。保護程序,避免程序出錯時無法運行。
格式:
try//快捷方式:雙擊 tab鍵 { } catch (Exception) { throw; } finally { }
以上所述就是本文的全部內容了,希望大家能夠喜歡。
相關文章
探討:程序在內存中的分配(常量,局部變量,全局變量,程序代碼)問題
本篇文章是對程序在的內存中分配(常量,局部變量,全局變量,程序代碼)的問題進行了詳細的分析介紹,需要的朋友參考下2013-05-05c++中的內聯(lián)函數(shù)inline用法實例
在本篇文章里小編給大家整理的是關于c++中的內聯(lián)函數(shù)inline用法實例以及相關知識點,有需要的朋友們學習下。2019-09-09C語言數(shù)據(jù)結構中數(shù)制轉換實例代碼
這篇文章主要介紹了C語言數(shù)據(jù)結構中數(shù)制轉換實例代碼的相關資料,需要的朋友可以參考下2017-03-03