一些算法的代码练习(c++实现,编译环境xcode)
2014-06-14 21:56
796 查看
1. 将excel表格的列转换成整数,例如a=1,b=2,。。。z=26,aa=27,。。。等等。
2.字符串压缩,例如把xxxyyz变成3x2yz,返回压缩后新字符串string strzip(const string &str);//这个代码是胡总写的,真好
3.已排序整数数组,设计O(n)时间算法确定数组中是否存在两个数的和正好为x。
4.求斐波那契数列第n项,规定n不小于2,不能用循环,用函数递归,怎么样递归次数最少
5.http://www.cnblogs.com/bennettwang00/p/3598909.html
#include <iostream> #include <string> #include <cctype> using namespace std; int main(int argc, const char * argv[]) { if (argc != 2) { cout<<"Input error!"<<endl; return -1; } string s(argv[1]); int sum = toupper(s[0]) - 'A' + 1; for (int i = 1; i < s.size(); i++) { sum = sum * 26 + (toupper(s[i]) - 'A' + 1); } cout<<sum<<endl; return 0; }
2.字符串压缩,例如把xxxyyz变成3x2yz,返回压缩后新字符串string strzip(const string &str);//这个代码是胡总写的,真好
#include <iostream> #include <string> using namespace std; string strzip(const string &str){ string ret; for(auto it=str.begin();it!=str.end();){ char c=*it; int count=0; while(it!=str.end()&&*it==c){ count++; it++; } if(count!=1){ ret.append(to_string(count)); } ret.push_back(c); } return ret; } int main(int argc, const char * argv[]) { string str = "az"; cout<<strzip(str)<<endl; return 0; }
3.已排序整数数组,设计O(n)时间算法确定数组中是否存在两个数的和正好为x。
#include <iostream> #include <string> #include <vector> using namespace std; void sumup(const vector<int> &iv, const int x){ for (auto itl = iv.begin(), itr = iv.end()-1; itl!=itr; ) { if (*itl+*itr>x) { --itr; }else if (*itl+*itr<x){ ++itl; }else{ cout<<x<<"="<<*itl<<"+"<<*itr<<endl; return; } } cout<<"Not found!"<<endl; } int main(int argc, const char * argv[]) { vector<int> iv; iv.push_back(1); iv.push_back(3); iv.push_back(6); iv.push_back(7); iv.push_back(10); sumup(iv, 10); return 0; }
4.求斐波那契数列第n项,规定n不小于2,不能用循环,用函数递归,怎么样递归次数最少
#include <iostream> #include <string> #include <vector> using namespace std; //找出fibonacci第n个数,n不小于2 const int fibonacci(const int t,const int i, const int j){ if (t==2) { return j; } return fibonacci(t-1,j,i+j); } int main(int argc, const char * argv[]) { cout<<fibonacci(8,1,1)<<endl; return 0; }
5.http://www.cnblogs.com/bennettwang00/p/3598909.html
#include <iostream> #include <cmath> using namespace std; void formatout(const int &n){ if (n<1) { return; } int p = floor(log2(n)); int i = 1; for (int j = 0; j<p; ++j) { for(int k = 0; k<pow(2, j); ++k){ cout<<i++<<" "; } cout<<endl; } while (i<=n) { cout<<i++<<" "; } cout<<endl; } int main(int argc, const char * argv[]) { formatout(17); return 0; }
相关文章推荐
- 编译原理(九) LR(0)文法分析法(算法描述和C++代码实现)
- 编译原理(九) LR(0)文法分析法(算法描述和C++代码实现)
- android NDK开发——Android.mk\Application.mk实现与C\C++代码编译
- 数据结构—中缀表达式转后缀表达式算法及实现—栈的应用—计算表达式(C++代码实现)(1)
- C++ 一些基本算法练习
- 练习写C++代码(11)--实现简单的时钟类3
- 双链表的定义、初始化、插入、删除,C++代码实现的算法
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- Boyer Moore 算法的C++实现以及一些独特的感受
- 卡尔曼滤波简介及其算法实现代码(C++/C/MATLAB)
- 练习写C++代码(10)--实现简单的时钟类2
- 算法竞赛入门经典 第二章 上机练习(C++代码)
- 练习写C++代码(1)--实现简单的时钟类1
- C++基本算法收集及代码实现
- 算法练习之二叉查找树 C++实现
- 快速傅里叶变换(FFT)算法C++实现代码
- 当你在VC++6.0编译上面的代码时会提示一些opencv的函数没有被定义,原因是环境没设置好
- 练习写C++代码(100)--实现简单的时钟类4
- 对于解决一些VS 2010/2012 C++ 生成的exe在非编译环境电脑中缺失dll的方法
- Xcode 5 中引入 C++ 模板代码无法编译通过的问题解决