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 ...
随机推荐
- poi简单案例
//poi api 操作 public static void main(String[] args) { // TODO Auto-generated method stub // 创建一个工作 ...
- 重新定义malloc和free 防止内存泄漏
1, 定义供应用程序使用的头文件//libmem.h#ifndef _LIBMEM_H_#define _LIBMEM_H_ //声明自定义malloc及free函数extern void *my_m ...
- -_-#Android版QQ浏览器广告过滤
省流加速 - 广告过滤 默认开启 设别广告是添加的标签最外层标签带有adv download
- 【转】Android 带checkbox的listView 实现多选,全选,反选 -- 不错
原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811 Demo地址(0分资源):http://download.csdn.net ...
- sql存储过程的创建
一:没有参数的存储过程 CREATE PROCEDURE select_all AS BEGIN SELECT * from T_login1 END GO 二:带参数的存储过程 CREATE PRO ...
- HBase Java API入门
概括 1. 创建.删除及启用禁用表.添加列等都需用到HBaseAdmin,另外需要注意删除,添加列等操作都需要禁用表 2. 表中添加数据,查询等都是和HTable相关,如果是多线程的情况下注意用HTa ...
- <a>标签的href属性
<a> 标签的 href 属性用于指定超链接目标的 URL. 语法 <a href="value"> 属性值 值 描述 URL 超链接的 URL.可能的值: ...
- ANT打包 小实例
ANT打包 package src; public class Hello { /** * @param args */ public static void main(String[] a ...
- 用otl写的oracle取数工具,执行传入在查询语句,把结果输出到文件
项目中经常需要用到此类型的工具 #pragma warning (disable:4786) #include <iostream> #include <map> #inclu ...
- Spring MVC之LocaleResolver(解析用户区域)
为了让web应用程序支持国际化,必须识别每个用户的首选区域,并根据这个区域显示内容. 在Spring MVC应用程序中,用户的区域是通过区域解析器来识别的,它必须实现LocaleResolver接口. ...