您的位置:首页 > 编程语言 > C语言/C++

c语言中需要注意的一些地方

2015-02-12 21:55 399 查看
1. (a, b)

unsigned int random = arc4random() % (b - a + 1) + a;

无符号长整形数据

int a = 100;

printf("%lu\n", sizeof(a));

printf("%lu\n", sizeof(int));

2.break与continue的区别

int i1 = 1;

while(1){

if (i1 > 100){

break; //break可以跳出任意循环。向下跳

}

printf("%d\n", i1);

i1++;

}

int i2 = 1;

while(i2 <= 100){

if (i2 % 2 != 0){

i2++;

continue; //continue向上跳

}

printf("%d\n", i2);

i2++;

}

注意:贪婪法:a +++ b

3.辗转相除法

int a = 0, b = 0;

printf("Enter two number: ");

scanf("%d%d", &a, &b);

while(a % b != 0){

int temp = a % b;

a = b;

b = temp;

}

printf("maxGY = %d\n", b);

4.随机生成一个数,这个数的范围在[10,20]或[80,90],要求用四种不同方法;

//(1)

unsigned int random = 0;

switch (arc4random() % 2) {

case 0:

random = arc4random() % (20 - 10 + 1) + 10;

break;

case 1:

random = arc4random() % (90 - 80 + 1) + 80;

break;

default:

break;

}

//(2)

unsigned int random = 0;

random = arc4random() % (20 - 10 + 1) + 10 + (arc4random() % 2) * 70;

printf("random = %d\n", random);

//(3)

unsigned int random = 0;

random = arc4random() % 22;

if (random < 11) {

random += 10;

} else {

random += 69;

}

printf("random = %d\n", random);

//(4)

random = arc4random() % (30 - 10 + 1) + 10;

if (random > 20) {

andom += 59;

}

注意:int (*p)[7] = array; //二维数组指针

int *p1[7] = {NULL}; //指针数组
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: