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 ...
随机推荐
- 无聊时,可以去HASKELL里找点感觉
可以和C,JAVA,PYTHON作任意的比较,感觉越来越晰一些计算机语言里深层的东东... doubleMe x = x + x doubleUs x y = x * + y * doubleSmal ...
- ARP劫持攻击
今天下午,莫名其妙的产生. 部分客户的网站产生乱码,但本机访问或是好好的. 外网访问,乱码的原文件是一个<IFRAME>网页. 听说,有时ARP攻击是导致网络中断或时断时续. 安全狗和36 ...
- java正则表达式一:基本使用
java正则表达式主要涉及三个类:java.util.regex.Matcher.java.util.regex.Pattern.java.util.regex.PatternSyntaxExcept ...
- Cracking the coding interview--Q2.2
Implement an algorithm to find the kth to last element of a singly linked list. 实现一个算法寻找链表中倒数第K个数.. ...
- 疯狂java实战演义 弹球游戏代码
package org.crazyit.ball; import java.awt.Image; import java.io.File; import javax.imageio.ImageIO; ...
- 【HDOJ】2424 Gary's Calculator
大数乘法加法,直接java A了. import java.util.Scanner; import java.math.BigInteger; public class Main { public ...
- POJ 2774 最长公共子串
一定好好学SAM...模板在此: #include<iostream> #include<cstdio> #include<cmath> #include<a ...
- List<T> 和DataTable的相互转换
我用的将集合类转换为DataTable 的方法 /// <summary> /// 将集合类转换成DataTable /// </summary> /// <param ...
- MFC自定义消息
本文地址:http://blog.163.com/strive_only/blog/static/893801682010101911467765/ 消息机制是windows的典型运行机制,在MFC中 ...
- Foundation: Binary Search
/* Binary search. * * Implementation history: * 2013-10-5, Mars Fu, first version. */ /* [Binary Sea ...