C++進修小結之語句。本站提示廣大學習愛好者:(C++進修小結之語句)文章只能為提供參考,不一定能成為您想要的結果。以下是C++進修小結之語句正文
1、次序語句
2、前提,分支語句
1、if語句
症結是可以或許闇練應用 if的嵌套。要斟酌好一切的情形。
假如說 前提是兩種情形互相對應的,那末便可以只用 if 與else 。但必需要想好 每一個else 跟哪一個if是一對。
假如情形是互相自力的三種情形以上,那末可以選擇應用if ... else if ...else。
1.if語句
if(前提)
{
知足前提的時刻履行;
}
2. if(前提)
{
知足前提履行;
}
else
{
不知足前提時履行;
}
3 if(前提1)
{
知足前提1的時刻履行;
}
else if(前提2)
{
不知足前提1的情形下知足前提2;
}
4.
if(前提1)
{
if(前提2)
{
既知足前提1又知足前提2的時刻履行;
}
}
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);
}
3、輪回語句
for輪回
四要素:
初始前提,輪回前提,狀況轉變,輪回體。 履行進程:
初始前提--輪回前提--輪回體--狀況轉變--輪回前提....
留意:for的小括號外面分號離隔,for的小括號後不要加分號。
應用 加斷點的方法,可以更好的明確for的任務道理。
1.for輪回空操作完成的實例, 輸入100之內的數字
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("請輸出次數");
//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.窮舉法: 用輪回把各類能夠的情形都給走一遍,然後用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.迭代法:有必定紀律。 每次輪回都是從前次運算成果中取得數據,本次運算的成果都是要為下次運舉動當作預備。
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("請輸出月數:");
int month = Convert.ToInt32(Console.ReadLine());
int sum;
for (int i = 1; i <= month; i++)
{
//賦值次序不克不及變,必需依照兔子發展紀律來,先有的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}個月後,幼兔個數為{1}對,小兔個數為{2}對,成兔個數為{3}對,總共有{4}對。", month.ToString(), byt, bxt, bct,sum);
}
//辦法二
static void Maink4(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int tu = 0;//請求誰人月的總數
int tu1=1, tu2=1;//倒數第一個為 tu1,倒數第二個為 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("請輸出月數:");
int m = int.Parse(Console.ReadLine());
int ct = 0;//成兔的對數
int xt = 0;//小兔的對數
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() + "個月後成兔的對數是:" + ct.ToString());
Console.WriteLine(i.ToString() + "個月後小兔的對數是:" + xt.ToString());
Console.WriteLine(i.ToString() + "個月後幼兔的對數是:" + yt.ToString());
Console.WriteLine(i.ToString() + "個月後兔子的總對數是:" + zt.ToString());
}
Console.ReadLine();
}
eg 2 100之內的一切數的和。
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的高度,問第五次彈起後的高度?
4、while 輪回。普通用在一些逝世輪回傍邊。
5、try catch。掩護法式,防止法式失足時沒法運轉。
格局:
try//快捷方法:雙擊 tab鍵
{
}
catch (Exception)
{
throw;
}
finally
{
}
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。