zoukankan      html  css  js  c++  java
  • Interview Algorithm

    约瑟夫环:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int find(int *arr, int m, int n);
    
    int main(int argc, char* argv[])
    {
    	int m, n;
    	int index;
    
    	printf("Please input the value of m and n:
    ");
    	fflush(stdout);
    	scanf("%d %d", &m, &n);
    
    	int *arr;
    	arr = (int*)calloc(n, sizeof(int));
    	for (index = 0; index<n; index++)
    	{
    		arr[index] = index+1;
    	}
    	printf("The winner is: %d
    ", find(arr, m, n));
    
    	free(arr);
    	arr = NULL;
    
    	system("pause");
    	return 0;
    }
    
    int find(int *arr, int m, int n)
    {
    	int len = n;
    	int index, step;
    	index = step = 0;
    
    	while(len>1)
    	{
    		if (arr[index] != 0)
    		{
    			++step;
    			if (step == m)
    			{
    				arr[index] = 0;
    				step = 0;
    				len--;
    			}
    		}
    		index = (index+1)%n;
    	}
    
    	for (index = 0; index<n; index++)
    	{
    		if (arr[index] != 0)
    		{
    			return arr[index];
    		}
    	}
    }
    

    将所有的整数放在负数的前面:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int find(int *arr, int m, int n);
    
    int main(int argc, char* argv[])
    {
    	int m, n;
    	int index;
    
    	printf("Please input the value of m and n:
    ");
    	fflush(stdout);
    	scanf("%d %d", &m, &n);
    
    	int *arr;
    	arr = (int*)calloc(n, sizeof(int));
    	for (index = 0; index<n; index++)
    	{
    		arr[index] = index+1;
    	}
    	printf("The winner is: %d
    ", find(arr, m, n));
    
    	free(arr);
    	arr = NULL;
    
    	system("pause");
    	return 0;
    }
    
    int find(int *arr, int m, int n)
    {
    	int len = n;
    	int index, step;
    	index = step = 0;
    
    	while(len>1)
    	{
    		if (arr[index] != 0)
    		{
    			++step;
    			if (step == m)
    			{
    				arr[index] = 0;
    				step = 0;
    				len--;
    			}
    		}
    		index = (index+1)%n;
    	}
    
    	for (index = 0; index<n; index++)
    	{
    		if (arr[index] != 0)
    		{
    			return arr[index];
    		}
    	}
    }
    
  • 相关阅读:
    Celery ---- 分布式队列神器 ---- 入门
    如何使用Python快速制作可视化报表----pyecharts
    django 使用 可视化包-Pyechart
    git & github 快速入门
    开发效率进阶
    windows编译 obs-studio
    python 控制vbox虚拟机
    pyqt实践——从裸机到打包安装
    测试darwin calendar 服务器
    centos 搭建 darwin calendar 服务器
  • 原文地址:https://www.cnblogs.com/stardujie89/p/4795128.html
Copyright © 2011-2022 走看看