![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++-面试
Alcoholsong
这个作者很懒,什么都没留下…
展开
-
有这样一个数组A,大小为n,相邻元素差的绝对值都是1。如:A={4,5,6,5,6,7,8,9,10,9}。现在,给定A和目标整数t,请找到t在A中的位置。
在笔试中遇到过这样一个数组问题,只知道一般的解法: 数组第一个数为array[0], 要找的数为y,设t = abs(y - array[0])。由于每个相邻的数字之差的绝对值为1。故第t个位置之前的数肯定都比y小。因此直接定位到array[t],重新计算t,t = abs(y – array[t]),再重复上述步骤即可。这种算法主要利用了当前位置的数与查找数的差来实现跨越式搜索。算法效率要比遍历原创 2015-05-09 15:19:59 · 2626 阅读 · 0 评论 -
循环链表 ——解决约瑟夫环
已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。#include <iostream>using namespace std;struct Node { int data; Node* next;};Node* create(in原创 2015-05-10 21:37:40 · 650 阅读 · 1 评论 -
C++ 指针顺序
c++中while(*p++);与while(*p){p++;}有什么区别?区别在于退出循环后, p的值不一样while( *p++ ); //当*p=0时,退出循环,此时p++仍然执行了 while( *p ) p++; //当*p=0时,退出循环,此时p++不再被执行例如 char *p=”ABCD”; 执行完第一个while循环后,p指向的是’\0’后面的一个字节,*p的结果是未知的原创 2015-05-06 19:15:09 · 440 阅读 · 0 评论 -
C++ 实现strcpy
不使用任何字符串库函数是实现strcpychar* _strcpy(char* dest, const char* src) { assert(dest != NULL && src != NULL); char* temp = dest; while (*src != '\0') { *dest = *src; dest++;原创 2015-05-06 19:17:19 · 1077 阅读 · 0 评论 -
C++ 实现strcmp
实现strcmp函数,不使用任何的字符串库#include <iostream>#include <cassert>#include <cstring>using namespace std;int _strcmp(const char* s1, const char* s2) { assert(s1 != NULL && s2 != NULL); while (*s1 !=原创 2015-05-06 17:28:26 · 2993 阅读 · 0 评论 -
m进制转为n进制(0<m,n<=10)
首先,将m进制转为10进制,然后在将10进制转为n进制;(都用辗转相除法)#include <iostream>using namespace std;void mToN(char* src, int m, char* dest, int n);void reverseString(char *s);int main() { char A[100] = { 0 }; mToN原创 2015-05-31 20:51:22 · 667 阅读 · 0 评论 -
将正整数N转换成m(2-16)进制数
给出一个正整数N和目标进制,将其转换并且输出 代码如下:#include <iostream>using namespace std;void mTurntoN(int m, int n, char* buf);int main() { char buf[100] = { 0 }; cout << "输入要转化的进制:"; int n; cin >> n; c原创 2015-05-31 19:06:47 · 1191 阅读 · 0 评论