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:\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的更多相关文章
- 实现带有getMin的栈
题目 实现一个特殊的栈,在实现栈的基础上,再实现返回栈中最小的元素的操作. 要求 pop.push.getMin的时间复杂度是O(1) 可以使用现成的栈类型 思路 如下图所示,在栈结构中,每次pop的 ...
- Web Best Practices
Web Best Practices General Google WebFundamentals - Github JavaScript Style Guide - Github Google In ...
- 每个JavaScript开发人员应该知道的33个概念
每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...
- [Algorithm] Coding Interview Question and Answer: Longest Consecutive Characters
Given a string, find the longest subsequence consisting of a single character. Example: longest(&quo ...
- 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 ...
- Pramp - mock interview experience
Pramp - mock interview experience February 23, 2016 Read the article today from hackerRank blog on ...
- WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】
WCF Interview Questions – Part 4 This WCF service tutorial is part-4 in series of WCF Interview Qu ...
- [转]Design Pattern Interview Questions - Part 3
State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
随机推荐
- 转:PHP include()和require()方法的区别
文章来自于:http://developer.51cto.com/art/200909/153687.htm 本文总结了PHP的include()和require()两种包含外部文件的方法的不同之处. ...
- 转:cookie和session(二)——php应用
文章来自于:http://blog.csdn.net/half1/article/details/21650211 本文将介绍cookie在session在php中的基本用法. 1.cookie ...
- 【Maven实战】仓库介绍和Nexus的安装
在Maven中我们之前进行配置一个项目的依赖时,引用一下jar包,这些jar包第一次都会从一个地方进行下载,这个地方称为仓库,而对于仓库一般有本地仓库和中心仓库之分,但是我们一般在做项目时会在自己的服 ...
- CONTEXT MENU简介
安卓中的上下文菜单是通过长按控件元素触发的,要注意的是每次都会触发onCreateContextMenu方法: main.xml <?xml version="1.0" en ...
- keil c编译器错误与解决方法
1. Warning 280:’i’:unreferenced local variable 说明局部变量i 在函数中未作任何的存取操作解决方法消除函数中i 变量的宣告 2 Warning 206:’ ...
- windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器(用轮询的办法保持高精度)
windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器可以有 100 纳秒也就是 1/10 微秒, 1/10000 毫秒的精度. 呵呵. SetWa ...
- 【HDOJ】4986 Little Pony and Alohomora Part I
递推.设n个盒子的Spell次数为S(n),期望为E(n).当有n个盒子时,可能第n把钥匙在第n个盒子中,此时的Spell次数应该为(n-1)!+S(n-1):当第n把钥匙不在第n个盒子中,混合排列, ...
- 【转】Android源代码查看途径
原文网址:http://www.it165.net/pro/html/201501/32967.html 作为一个android coder,多阅读android源码对提高android开发水平是很有 ...
- Hbase 计数器
Hbase计数器可以用于统计用户数,点击量等信息 基本操作 可以使用incr操作计数器,incr语法格式如下: incr '<table>', '<row>', '<co ...
- delphi对ini文件的操作(转载 万一)
ini 文件操作记要(1): 使用 TIniFileunit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Gr ...