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 ...
随机推荐
- temp gbk2utf8
__author__ = 'root' # -*- coding: utf-8 -*- ps = '/data/poitestdata/行政地名.csv' pt = '/data/poitestdat ...
- 使用Windows 系统性能监控来报警磁盘空间不足
http://blog.csdn.net/jiangxinyu/article/details/4370288
- @action 注解
================================================= 下载 注解配置 private String fileName; private String co ...
- POJ2996 Help Me with the Game(模拟)
题目链接. 分析: 简单模拟. #include <iostream> #include <queue> #include <cstdio> #include &l ...
- appendGrid的使用
新的项目中需要使用类似appendGrid的控件,当然开始时是不知道这个控件的,开始是想做成如下这样 这其实是Bugfree的搜索栏,并且我也说不好我要做的这个东西应该叫什么,大体可以说是动态的添加或 ...
- 输入一个单向链表,输出该链表中倒数第K个结点
输入一个单向链表,输出该链表中倒数第K个结点,具体实现如下: #include <iostream> using namespace std; struct LinkNode { publ ...
- SQL:将查询结果插入到另一个表的三种情况
一:如果要插入目标表不存在: select * into 目标表 from 表 where ... 二:如果要插入目标表已经存在: insert into 目的表 select * from 表 wh ...
- 《Ruby语言入门教程v1.0》学习笔记-02
9.18 第四章 一切都是对象 这个章节的例子都举得很浅显易懂,而且作者的语言= =噗,委实生动有趣啊是~~ 4.1 两种思维方式 初期的编程思想是:以“如何做”为指导来编写代码.这时期的编程语言叫 ...
- ArcGis : unable to save as template this document is already based on another template
原文:http://forums.esri.com/Thread.asp?c=93&f=989&t=289930 ----------------- I ran into this p ...
- 【转】SqlLite .Net 4.0 System.IO.FileLoadException”类型的未经处理的异常出现在XXX
原文地址:http://www.csharpcity.com/2010/sqlite-ado-net-c-4-0/ ---------------------- 解决方法: Paste the fol ...