约瑟夫环:

#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:\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\n", 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:\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\n", 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];
}
}
}

Interview Algorithm的更多相关文章

  1. 实现带有getMin的栈

    题目 实现一个特殊的栈,在实现栈的基础上,再实现返回栈中最小的元素的操作. 要求 pop.push.getMin的时间复杂度是O(1) 可以使用现成的栈类型 思路 如下图所示,在栈结构中,每次pop的 ...

  2. Web Best Practices

    Web Best Practices General Google WebFundamentals - Github JavaScript Style Guide - Github Google In ...

  3. 每个JavaScript开发人员应该知道的33个概念

    每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...

  4. [Algorithm] Coding Interview Question and Answer: Longest Consecutive Characters

    Given a string, find the longest subsequence consisting of a single character. Example: longest(&quo ...

  5. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  6. Pramp - mock interview experience

    Pramp - mock interview experience   February 23, 2016 Read the article today from hackerRank blog on ...

  7. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

  8. [转]Design Pattern Interview Questions - Part 3

    State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...

  9. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

随机推荐

  1. ImportError: No module named matlab

    这个matlab已经改名为pylab了,你可以运行: from pylab import *

  2. Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)

    Node应用,使用formidable处理文件上传,本地测试没有问题,部署到服务器上之后上传大文件浏览器收到以下错误信息: Failed to load resource: the server re ...

  3. 深入研究 Win32 结构化异常处理(好多相关文章)

    摘要 就像人们常说的那样,Win32 结构化异常处理(SEH)是一个操作系统提供的服务.你能找到的所有关于 SEH 的文档讲的都是针对某个特定编译器的.建立在操作系统层之上的封装库.我将从 SEH 的 ...

  4. 【HDOJ】2150 Pipe

    计算几何的基础题目.是时候刷刷计算几何了. /* 2150 */ #include <cstdio> #include <cstring> #include <cstdl ...

  5. Best Time to Buy and Sell Stock——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  6. linux补包

    1.挂载文件export LANG=Cmkdir -p /media/cdrommount /dev/cdrom /media/cdrommount /dev/hdc /media/cdrommoun ...

  7. Drupal 实战

    <Drupal 实战> 基本信息 作者: 葛红儒    丛书名: 实战系列 出版社:机械工业出版社 ISBN:9787111429999 上架时间:2013-6-28 出版日期:2013 ...

  8. ffmpeg错误隐藏框架分析

    本文主要是分析ffmpeg的错误隐藏框架,故解码流程此处不会特地进行讨论,网上其他地方其实也有不少介绍相关流程的了,但发现基本没有介绍错误隐藏流程的,故本文希望能填补这个空白. 我们直接从decode ...

  9. Android ProgressDialog 加载进度

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  10. mongodb的write concern

    mongodb有一个write concern的设置,作用是保障write operation的可靠性.一般是在client driver里设置的,和db.getLastError()方法关系很大 一 ...