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 ...
随机推荐
- 构建高可用web站点学习--前言
前言:本人对于提高web站点的访问量等的有很浓厚的兴趣,也学习了将近一年的时间,希望能总结点东西,虽然很多东西都是从书籍和资料中学习的,而不是原创,但是这是我总结的一点感悟和进行的分类吧.而且可能思路 ...
- SQLSERVER收缩数据库日志
数据库名为:EKECMS DUMP TRANSACTION EKECMS WITH NO_LOG;BACKUP LOG EKECMS WITH NO_LOG;DBCC SHRINKDATABASE(E ...
- Fetcher类的工作流程
Fetcher类工作流程: FileInputFormat.addInputPath(job, new Path(segment, CrawlDatum.GENERATE_DIR_NAME)); jo ...
- Android Service 简介
Service是Android系统中的一种组件,它跟Activity的级别差不多,但是它不能自己运行,只能后台运行,并且可以和其他组件进行交互.Service是没有界面的长生命周期的代码.Servic ...
- vmware压缩磁盘空间的方法
家里笔记本40G,可用空间怎么挤都只剩7G,从单位copy回来的linux虚拟机要10G,经检查实际使用空间5.7,也就是其他都是空余空间,可以释放掉.(只适合independent.Persiste ...
- 调用test case集,并生成测试报告
结构是 test_all.py 进行配置,执行所有测试用例集,并合并测试报告到同一个文件 #test_all.py 进行配置,执行所有测试用例集 # coding = utf-8 from time ...
- 什么是C#,.NET,ASP.NET?
C#,.NET,ASP.NET这三个词,对于大家并不陌生,最近花了点时间去了解了下.最终才发现我以前的理解有些偏差.现在分享给大家. 什么是C#? C#是微软公司发布的一种面向对象的.运行于 ...
- STL——heap的4大操作
STL的堆操作 STL里面的堆操作一般用到的只有4个:make_heap();.pop_heap();.push_heap();.sort_heap(); 他们的头文件函数是#include < ...
- selenium webdriver python 元素定位
总结 定位查找时,返回查找到的第一个match的元素.如果找不到,则 raise NoSuchElementException 单个元素定位: find_element_by_idfind_e ...
- OpenStack入门之初步认识
一.OpenStack 入门 之 基础知识 二.OpenStack 入门 之 基本组件 三.OpenStack 入门 之 各组件解析(基础) 四.OpenStack 入门 之 各组件解析(进阶) 五. ...