约瑟夫环:

#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. Stockbroker Grapevine

    http://poj.org/problem?id=1125 #include<cstdio> #include<cstring> #include<cmath> ...

  2. 通过示波器分析TypeB卡通讯数据

    这几天,使用NFC芯片模拟了一张TypeB的cpu卡,在调试过程中,因为要检查射频性能,所以用示波器抓取了RFID读卡器和TypeB CPU卡之间的通讯数据.READER发送的数据位106K ASK调 ...

  3. 使用Qt编写模块化插件式应用程序

    动态链接库技术使软件工程师们兽血沸腾,它使得应用系统(程序)可以以二进制模块的形式灵活地组建起来.比起源码级别的模块化,二进制级别的模块划分使得各模块更加独立,各模块可以分别编译和链接,模块的升级不会 ...

  4. VitamioBundle-master

    1. 下载资源 (1) 核心插件 VitamioBundle 下载地址:https://github.com/yixia/VitamioBundle (2) 官方示例 VitamioDemo 下载地址 ...

  5. 【HDOJ】1619 Unidirectional TSP

    题目本身一点儿都不难,但是就是被字典序搞死了.写的挺麻烦,但是过了,逆向做好做一点儿. /* 1619 */ #include <cstdio> #include <cstring& ...

  6. Silverlight 中 TreeView 的数据绑定

    方法一:Silverlight使用XAML标记语言来编写,如果不使用XAML强大的绑定功能,实在是罪过.通过使用绑定,可以将UI与视图模型层分离,有利于系统的维护.作为Silverlight中比较有代 ...

  7. delphi调用java编写的webservice

    delphi调用java编写的webservice JAVApojo: public class GroupInfo implements Serializable{    private stati ...

  8. DLL——SDL_PingGe

    这篇随笔专门做SDL的DLL开发. 下面这个版本暂且称为Beta版本吧. /* typedef void (*FUNCTION)(void); HMODULE HDll; HDll = LoadLib ...

  9. F - True Liars - poj1417(背包+并查集)

    题意:有这么一群人,一群好人,和一群坏人,好人永远会说实话,坏人永远说假话,现在给你一组对话和好人与坏人的数目P1, P2. 数据里面的no是A说B是坏人, yes代表A说B是好人,就是这样,问题能不 ...

  10. JSP学习笔记(二):Tomcat服务器的安装及配置

    一.Tomcat的下载及安装. 前往Tomcat官网下载安装包或者免安装压缩包.链接http://tomcat.apache.org/ 这里,我选择的是Tomcat8.0,而不是最新的Tomcat9. ...