C++ 一些编程题

求一个数组里面有一个数重复奇数次找出来,要求时间复杂度O(N),空间复杂度O(1)

    方法取巧,0  跟数组里的数异或

参考:https://blog.csdn.net/JULIUS_0/article/details/81603572

void Find(int arr[], int size) 
{
    int result = 0;
    for(int i=0; i < size; i++)
    {
      result = result ^ value;
    }    
    return result;
}

求斐波那契数列的第N个数的值

//循环方式求解
int Fibloop(int n)
{
	if (n == 1)
	{
	   return 0;
	}
	if (n == 2)
	{
	   return 1;
	}
	int f1 = 0;
	int f2 = 1;
	int c = 0;
    //因为斐波那契数列是从0和1开始,并在第三个数的时候才开始有规律
	for (int i = 3; i <= n; i++)
	{
	   c = f1 + f2;
           f1 = f2;
	   f2 = c;
	}
 
	return c;
} 
                  
//递归方式求解
int Fib(int n)
{
	if (n == 1)
	{
	   return 0;
	}
	if (n == 2)
	{
	   return 1;
	}
	return Fib(n - 1) + Fib(n - 2);
}

一共有20级楼梯,每次可以上1级或2级,登上第20级一共有多少种上法

上第1级: 1种上法
上第2级: 2种上法
上第3级: 3种上法  3 = 2 + 1
上第4级: 5种上法  5 = 3 + 2
上第5级: 8种上法  8 = 5 + 3
...

1,2,3,5,8, ...

从第3级楼梯开始,每级楼梯的上法等于之前两级楼梯上法的和。
由此构成斐波那契数列,登上第20级台阶种类数即为该数列第二十项的数值,经计算为10946种

#include<stdio.h>
#include<string.h>
 
int foo(int n)
{
    if (1 == n) {
        return 1;
    }
    if (2 == n) {
        return 2;
    }
    if (n > 2) {
        return foo(n-1) + foo(n-2);
    }
}
 
int main(void)
{
    int x = foo(20);
    printf("%d\n", x);
}

查找两个数组中的相同元素

假设,这两个数组已经排好序(升序),那么只需要遍历一次即可。
首先设两个下标,分别初始化为两个数组的起始地址,依次向前推进 。推进的规则是比较两个数组中的数字,小的那个数组的下标向前推进一步,直到任何一个数组的下标到达数组末尾时,如果这时还没碰到相同的数字,说明数组中没有相同的数字

#include <QCoreApplication>
#include <iostream>
#include <map>
#include <set>
using namespace std;
bool findcommon2(int a[], int size1, int b[], int size2, vector<int> &vec)
{
     int i=0,j=0;
     while(i<size1 && j<size2) 
         {
          if(a[i]==b[j]) {
            vec.push_back(a[i]);
            i++;
            j++;
          }

          if(a[i]>b[j])
               j++;
 
          if(a[i]<b[j])
               i++;
     }
     return false;
}
 
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
 
    int data[10] = {1,2,3,4,5,6,7,8,9,10};
    int rt[15] = {5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
    vector<int> vec;
    findcommon2(data, 10, rt, 15, vec);
 
    vector<int>::iterator it;
    it = vec.begin();
 
    for (;it != vec.end(); ++it) {
        cout << *it << endl;
    }
    return a.exec();
}

查找数组中连续相同值的算法

#include<iostream>
#include<string>
using namespace std; 
int main()
{
    std::string str = "987600000000000000000789111111111111111666666666666";
    int num = 1;
    char word;
    int len = str.length()-1;
    for(int i=0; i<len; i++)
    {
        if (str[i] == str[i+1]) 
          {
             word = str[i];
             ++num;
          } 
        else if(num > 1) //num>1 表明元素重复
             { 
                 //这里的输出是:从0开始计数,连续值起始位置
                 cout << "start point is :" << i-num+1 << endl;
                 // num 表示连续相同值有多少个
                 cout << "count is : " << num << endl;
             }
             else           
              {
                  num = 1;
              }
    }
 
    return 0;
}
 

数组反转:

void reversech(char ch[])
{
	int len = strlen(ch);
	for (int i=0; i< len/2; ++i)
	{
		char temp = ch[i];
		ch[i] = ch[len-1-i];
		ch[len-1-i] = temp;
	}
}

链表反转:

PNode reverse(PNode head)
{
	PNode prev = NULL;
	PNode node = head;
	PNode child = node->next;
	while (node)
	{
		child = node->next;
		node->next = prev;
		prev = node;
		node = child;
 
	}
	return prev;
}

猜你喜欢

转载自blog.csdn.net/ytusdc/article/details/85850456