蓝欧--lesson8

#import <Foundation/Foundation.h>
//       宏名 替换的内容[千万别加分号]
#define VALUE 10

//命名规范,纯大写或者是k开头的驼峰命名法
#define AS 52.02

#define SUM(A, B) A + B

#define SQUARE(A) ((A) + (A))//安全

#define MAXXX(A, B) (((A) > (B)) ? (A) : (B))

#define MAX_NUMBER(A, B, C) ((((A) > (B)) ? (A) : (B)) > (C) ? (((A) > (B)) ? (A) : (B)) : (C))

#define MAX_ABC(A, B, C) MAXXX(A, B) > (C) ? MAXXX(A, B) : (C)

#define MAX_ABCD(A,B,C,D) MAX_ABC(A,B,C) > (D) ? MAX_ABC(A,B,C) : (D)

//宏定义 冒泡排序
#define kBubbleSort(array, number)\
for (int i = 0; i < number - 1; i++){\
for (int j = 0; j < number - 1 - i; j++){\
if (array[j] > array[j + 1]){\
int team = array[j];\
array[j] = array[j + 1];\
array[j + 1] = team;\
}\
}\
}\
//宏定义 选择排序
#define kChooseSort(A,N)\
for (int i = 0; i < n - 1; i++){\
int num = i;\
for (int j = i + 1; j < n; j++){\
if (A[mum] > A[j]){\
num = j;\
}\
}\
if (num != i){\
int team = A[min];\
A[min] = A[i];\
A[i] = team;\
}\
}\

int main(int argc, const char * argv[]) {
 
    //宏定义[预编译指令]
    //本质就是替换
//    int a = VALUE ;
//    float b = AS;
//    printf("%d %f", a, b);
    
//    int array[VALUE] = {0};
//    for (int i = 0; i < VALUE; i++){
//        array[i] = arc4random() % 101;
//    printf("%d\t",array[i]);
//    }
    
//    int a = SUM(5, 8);
//    printf("%d",a);
    
//    int a = SQUARE(5);
//    printf("%d",a);
//    
//    int a = MAXXX(15, 9);
//    printf("%d",a);
    
//    int a = MAX_NUMBER(20, 11, 19);
//    printf("%d",a);
    
//    int a = MAX_ABC(25,19,20);
//    printf("%d",a);
//    int a = MAX_ABCD(15,111,18,20);
//    printf("%d",a);
    
    //在switch case里面定义变量要加{ };
//    int array[VALUE] = {0};
//    for (int i = 0; i < VALUE; i++){
//            array[i] = arc4random() % 101;
//        printf("%d\t",array[i]);
//        }
//    printf("\n");
//    for (int i = 0; i < VALUE; i++) {
//        kBubbleSort(array, VALUE);
//        printf("%d\t",array[i]);
//    }
    
    //冒泡排序[算法优化]
//    int a[10] = {0};
//    for (int i = 0;i < 10; i++){
//        a[i] = arc4random() % 101;
//        printf("%d\t",a[i]);
//    }
    int a[10] ={15, 64, 56 , 65, 48 , 56, 49, 25, 69,34};
    //定义BOOL值假定是否需要下一趟比较
    BOOL needNeat = YES;
    for (int i = 0;i < 10 - 1 && needNeat == YES; i++){
        
        needNeat = NO;//没趟开始假定不需要下一趟比较
        for (int j = 0; j <10 - 1 -i ; j++) {
            if (a[j] > a[j + 1]) {
                //如果这一趟中有过一次交换说明还是乱序
                needNeat = YES;
                int tem = a[j];
                a[j] = a[j + 1];
                a[j + 1] = tem;
            }
        }
    }
    printf("\n");
    for (int i = 0; i < 10; i++) {
        printf("%d\t",a[i]);
    }
   
//    //选择排序
//    int a[10] = {0};
//    for (int i = 0;i < 10; i++){
//        a[i] = arc4random() % 101;
//        printf("%d\t",a[i]);
//    }
//    for (int i = 0; i < 10- 1; i++) {
//        int min = i;
//        for (int j = i + 1; j < 10; j++) {
//            if (a[min] > a[j]) {
//                min = j;
//            }
//        }
//        if (min != i) {
//            int tem = a[min];
//            a[min] = a[i];
//            a[i] = tem;
//        }
//    }printf("\n");
//    for (int i = 0; i < 10; i++) {
//        printf("%d\t",a[i]);
//    }
    
    //折半查找[二分法查找](仅适用于排好序的数组)
    int start = 0;//开始下标
    int end = 10 - 1;//结束下标
    int mid = (start + end) / 2;//中见下标
    int number = 64;//要从数组中检索的值
    //如果中间的数不是我们想要找的数,并且开始下标小于结束,循环继续
    while (a[mid] > number && start < end) {
        //如果中间数比要找的数大,说明在左半部范围内
        if (a[mid] > number) {
            end = mid -1;//调整结束下标
        }else {//否则在右半部范围内
            start = mid + 1;//调整开始下标
        }
        //再次确定中间值
        mid = (start + end) / 2;
    }
    if (a[mid] == number) {//确定要找的数,并输出
        printf("%d\n",mid);
    }
    return 0;
}

时间: 2024-12-25 16:07:37

蓝欧--lesson8的相关文章

蓝欧h5课程笔记

1.获取css外部样式表的属性值 getCss(obj,'属性名称');如:getCss(oSpan[0],'height'); getComputedStyle(obj).属性名称;如:getComputedStyle(oDiv[0]).width; 2响应式布局 <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0

蓝欧证件照

蓝欧--lessonc12[函数指针]

#import <Foundation/Foundation.h> //int(*)(int ,int) 原类型//PFUN 新类型typedef int(*PFUN)(int ,int); typedef struct student{    char name[20];    char sex;    int age;    float score;}STUDENT; BOOL sortByAge(STUDENT stu, STUDENT stu1);BOOL sortByAge(STUD

蓝欧--lesson2

#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {                //变量定义三要素    //类型修饰符 变量名 =初始值            //变量命名规范:    //1.变量名必须以字母,数字,下划线组成,数字不能打头    //2.变量名不能够占用系统关键字    //3.见名知意    //4.驼峰命名法        //    int a = 5;//   

蓝欧--lessonc11[动态内存分配]

#import <Foundation/Foundation.h>#import "header.h" typedef struct student{    char name[20];    char sex;    int  age;    float score;    }STUDENT; int maxValue(int a, int b);int square(int n); char * getString(); int maxValue(int a, int

蓝欧培训 c7

header.m#import "header.h" void printStudent(STUDENT boy){    printf("%-11s %d %d-%d-%d %c %.2f\n",boy.name,boy.age,boy.birthday.year,boy.birthday.month,boy.birthday.day, boy.sex,boy.score); } void printAllStudents(STUDENT stus[],int c

蓝欧--lesson5&lt;有难度&gt;

#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { //输出数组中4个数的最大数//    int i = 0, max = 0,a[4] = {0};//    printf("请输入四个数:");//    for (i = 0; i<4; i++) {//        scanf("%d", &a[i]);//    }//    m

蓝欧--lesson3

#import <Foundation/Foundation.h>////int main(int argc, const char * argv[]) { //    //    //溢出//    char a = 128;//    //    printf("%d", a);//        //无符号类型,值存正数    //%u 打印无符号整型数    //%lu 打印无符号长整型数    //%ld 打印有符号长整型数//    unsigned char

蓝欧--lessonc10[指针高级]

hearder.m #import "hearder.h" void changeValue(int *number) {    *number = 100; } void swap(int * a, int * b){    printf("a = %d,b = %d\n", *a, *b);    int tem = *a;    *a = *b;    *b = tem; } void maxThreeNumber(int a, int b, int c, i