[C #] 05 C # language learning the basic elements of an overview, first met types, variables and methods, Introduction to Algorithms

Courseware

Here Insert Picture DescriptionHere Insert Picture Description
Here Insert Picture DescriptionHere Insert Picture Description

notes

1. naming

(1) Pascal (pascal): When variable names and function names are connected by two or more words together and capitalize the first letter of each word. For naming methods, classes, namespaces.
(2) hump formula: When the variable names or function names are linked together by one or more words, the first word begins with a lowercase letter; after the first letter of each word starting from the second word are uppercase . For naming variables.

2. Text

(1) integer

Ordinary type int
long long integer

In long type suffix L, ensure the correct call type, for example:

SampleMethod(5);    // Calling the method with the int parameter
SampleMethod(5L);   // Calling the method with the long parameter

(2) Real

Single-precision float, 32 indicates floating point
double double, 64 bits represent floating-point

(3) Character string

Character char, a pair of single quotation marks ( 'g') enclosed
string string, a pair of double quotes ( "guoqinyu") enclose

(4) Boolean

bool b1 = true
bool b2 = false

(5) empty (null)

string str = null

3. Comment

(1) a single line comment

//定义一个整型变量,其值为3
int x = 3;

(2) Block comment

/*从明天起
做一个幸福的人
喂马,劈柴,周游世界*/

(3) Batch Notes shortcut keys, select the required comment Code: ctrl EC
solution annotation shortcut keys, select the code: ctrl KU

Learning problems encountered

1. Examples of the class, the class format method, and the variables are declared as follows:

Data Type Name
If not write data type, being given, the following code sample
Here Insert Picture Description
creates a Calculater classes, methods defined in the class Add, features are obtained of two integers.

(1) instance does not declare the class (that is, (2))
of Calculater instantiate, and assigned to the variable m, if you write "Calculater" not in front of m, will get an error: The name 'm' does not exist in the current context.

(2)不声明变量
a.提供Add参数并调用Add方法,将其值赋给变量x,如果不在x前写“int",会报错:The name ‘x’ does not exist in the current context.
b.定义Add方法中的参数,不声明参数的数据类型,不在a,b前写"int",会报错:identifier expected.
c.规定Add方法中两参数的和赋值给变量result,如果不在result前写"int",会报错:The name ‘result’ does not exist in the current context.

(3)不声明方法
如果不为方法Add声明其执行结果的数据类型(返回类型)为普通整型"int",会报错:method must have a return type.

在进行变量声明时会进行内存的分配,以保存该类型对应的值

2.void是什么?

方法具有一个参数 (parameter) 列表(可以为空),表示传递给该方法的值或变量引用;方法还具有一个返回类型 (return type),指定该方法计算和返回的值的类型。如果方法不返回值,则其返回类型为 void。

3.++是什么意思?

++i :前递增,即先自增后传值

int i = 3;
int j = ++i;

此时i值为4,j值为4

i++:后递增,即先传值后递增

int i = 3;
int j = i++;

此时I值为4,j值为3

i += 1, ++ i 就相当于 i = i + 1

4.用三种方法求解1——100的和

(1)循环
Here Insert Picture Description
(2)递归
Here Insert Picture Description
(3)数学计算式
Here Insert Picture Description

5.C#中的for循环语句

语法:

for (表达式1,表达式2,表达式3)
{
	表达式4
}

Expression 1: initial value for the loop variable.
Expression 2: a circulation loop condition.
Expression 3: to change the size of the loop variable.
Expression 4: Expression 4 when the execution of the loop condition.

6. Recursive thinking

The definition of recursive algorithm: if the object contains a description of itself, we say that the object is recursive, this algorithm is called recursively to describe the recursive algorithm.

How 7.visual studio conducted debug?

After the first line in the first block of code needs to be debugged set breakpoints, start commissioning (start debugging), click View Code step into running processes

The 8.python for loop

Here Insert Picture Description

9. jobs: a recursive algorithm to solve the Towers of Hanoi problem

Number of Discs Step count
1 1
2 3
3 7
4 15

So that the number of disks n, the number of steps as a function of the number of disk f (n), is easy to get: f (n) = 2f ( n -1) + 1
by mathematical induction may be obtained f (n) = 2 ** n - 1

Code:
Here Insert Picture Description

Published 29 original articles · won praise 3 · Views 960

Guess you like

Origin blog.csdn.net/weixin_44813932/article/details/103730272