冒泡排序的优化 折半查找法 循环打印三角形 耶稣13门徒找叛徒 有1000000个数,每个数取值范围是0~999999,找出其中重复的数,重复的次数。

来源:互联网 发布:改字体的软件 编辑:程序博客网 时间:2024/05/16 10:54

#import <Foundation/Foundation.h>

#import "Founction.h"

//#define NUM 10


int main(int argc,const char * argv[]) {

// 冒泡排序的优化

//    int array[10] = {};

//    int *p = array;

//    for (int i = 0; i < NUM; i++) {

//        *(p  + i) = arc4random()%51 + 50;

//        printf("%d\t",*(p + i));

//    }

//    printf("\n");

//    BOOL needNext = YES;

//    for (int i = 0; i < NUM - 1 && needNext; i++) {

//        needNext = NO;

//        for (int j = 0; j < NUM -1 - i; j++) {

//            if (*(p + j) > *(p + j + 1)) {

//                needNext = YES;

//                int temp = *(p + j);

//                *(p + j) = *(p + j +1);

//                *(p + j + 1) = temp;

//            }

//        }

//    }

//    for (int  i = 0; i < NUM; i ++) {

//        printf("%d\t",*(p + i));

//    }


    

//    折半查找法

//    int array[10] = {10,11,23,45,67,87,88,89,96,98};

//    int start = 0;//开始下标

//    int end = 10 - 1;//结束下标

//    int mid = (start + end)/2;

//    int number = 98;

//    while (array[mid] != number && start < end) {

//        if (array[mid] > number) {

//            end = mid -1;

//        }else

//        {

//            start = mid + 1;

//        }

//        mid = (start + end)/2;

//    }

//    if (array[mid] == number) {

//        printf("\nindex = %d",mid);

//    }else{

//        printf("Not Found!");

//    }

    

// 循环打印三角形

//    int n = 0;

//    printf("请输入一个数字:");

//    scanf("%d",&n);

//    for (int i = 1; i <= n; i++) {

//        for (int j = 1; j <= n - i; j++) {

//            printf("1");

//        }

//        for (int j = 1; j <= 2*i - 1; j++) {

//            printf("*");

//        }

//        printf("\n");

//    }

    

//创建一个point结构体,包含xy两个变量。

//    并写以下函数:

//    函数1,判断两点是否在一条水平线上

//    函数2,判断两点是否在一条垂直线上

//    函数3,判断两个点是否相等。

//    MP p1 = {100,300};

//    MP p2 = {100,300};

//    printf("请输入第一个点的坐标:");

//    scanf("%f %f",&p1.x,&p1.y);

//    printf("请输入第二个点的坐标:");

//    scanf("%f %f",&p2.x,&p2.y);

////    是否同一水平线

//    printf("%d",isHorizontal(p1,p2));

//// 是否同一垂直线

//    printf("%d",isVertical(p1,p2));

////    是否相等

//    printf("%d",isEqual(p1,p2));

    

    

    

//    耶稣13门徒找叛徒

//    int peopleCount = 13;

//    int array[13] = {0};

//    int step = 1;

//    int pos = 0;

//    for (int i = 0; i < 13; i++) {

//        array[i] = 1;

//        printf("%d ",array[i]);

//    }

//    while(peopleCount > 0){

//        if (array[pos] != 0) {

//            step ++;

//            printf("step = %d\n",step);

//        }

//        if (step == 3) {

//            array[pos] = 0;

//            peopleCount --;

//            step = 0;

//        }

//        pos ++;

//        if (pos == 13) {

//            pos = 0;

//        }

//    }

//    printf("pos = %d\n",pos);

    

//将一个字符串中的数字除去

//    char str[100] = "ans3w5er";

//    char *p = str;

//    printf("去除数字之后的字符串为:");

//    int i = 0;

//    char temp[100] = {0};

//    while (*(p + i) !='\0') {

//        if (*(p + i) >= '0' && *(p + i) <= '9') {

//            strcpy(temp, p + i + 1);

//            strcpy(p + i, temp);

//        }else{

//            i ++;

//        }

//    }

//    printf("%s",str);

//    

    

//1000000个数,每个数取值范围是0~999999,找出其中重复的数,重复的次数。

   int array[1000000] = {0};

   for (int  i =0; i <  1000000; i ++) {

       int number = arc4random() %1000000;//随机数作为数组元素的下标

        array[number] ++;

    }

   for (int i =0; i < 1000000; i ++) {

       if (array[i] > 1) {

           printf("%d重复了%d\n",i,array[i]);

        }

    }

    

   return 0;

}

function.h

#import <Foundation/Foundation.h>


@interface Founction :NSObject

typedef struct MyPoint{

   float x;

   float y;

}MP;

@end


function.m

#import "Founction.h"


@implementation Founction

//是否在同一水平线上

BOOL isHorizontal(MP point1,MP point2){

   BOOL is = 0;

   if (point1.y == point2.y) {

        is =1;

    }

   return is;

}

//    是否在同一垂直线上

BOOL isVertical(MP point1,MP point2){

       BOOL is = 0;

       if (point1.x == point2.x) {

            is =1;

        }

       return is;

    }

    

BOOL isEqual(MP point1,MP point2){

   BOOL is = 0;

   if (point1.x ==point2.x && point1.y == point2.y) {

        is =1;

    }

   return is;

}


@end



0 0
原创粉丝点击