最大访客数(c/python)

说明:确定某一时间的最大容客量,用X[i],y[i]分别表示第i个访客来访和离开的时间;

c代码

#include
#include

#define N 100
void sort(int *, int);
int main()
{
    int i, j;
    int number = 0;
    int x[N] = { 0 };
    int y[N] = { 0 };
    printf("输入访客来访与离开的时间(0~24):");
    for (i = 0; i < N; i++)
    {
        printf("\n");
        scanf_s("%d%d", &x[i], &y[i]);
        printf("\n");
        //输入(-1,-1)终止
        if (x[i] < 0 || y[i] < 0)
            break;
    }
    sort(x, i);
    sort(y, i);
    for (int time = 1; time <= 24; time++)
    {   
        number = 0;
        for (j = 1; j <= i; j++)  //如果不对x[i],y[i]进行排序改成for(j=0;jif (time > x[j])
                number++;
            if (time > y[j])
                number--;
        }
        printf("%d时的最大访客数为%d\n", time, number);
    }
    system("pause");
}

void sort(int *arr, int n)
{
    //int min = 0;
    int temp;
    for (int i = 0; i < n; i++)
    {
        int min = i;
        //temp = arr[min];
        for (int j = i + 1; j <= n; j++)
        {
            if (arr[j] < arr[min])
            {
                temp = arr[min];
                arr[min] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

python

# -*- coding: utf-8 -*-
"""
Created on Sat Nov  4 20:22:59 2017

@author: yangwenbin
"""
#import numpy as np

def sort(arr,n):
    for i in range(n):
        Min=i
        for j in range(i+1,n+1):
            if arr[Min]>arr[j]:
                temp=arr[Min]
                arr[Min]=arr[j]
                arr[j]=temp
                pass
            pass
        pass
    pass
if __name__=="__main__":

    x=[0]*100
    y=[0]*100
    print("请输入来访离开时间:")
    for i in range(100):
        x[i]=int(input())
        y[i]=int(input())
        if x[i]<0 or y[i]<0:
            break
        pass
    sort(x,i)
    sort(y,i)
    for time in range(1,25):
        num=0
        for j in range(1,i+1):
            if time>x[j]:
                num+=1
                pass
            if time>y[j]:
                num-=1
                pass
            pass
        print("%d时最大访客数%d"%(time,num))
        pass
    pass        

你可能感兴趣的:(python,c++,算法)