|
/* STL map应用 * Greedy 部分背包问题 * newplan 开发时间:08.5.13 */ /*--------INCLUDES----------*/ #include <cstdlib> #include <iostream> #include <map> #include <fstream> #include <iomanip> /*--------INCLUDES----------*/
/*---------MACROS-----------*/ #define INPUTFILE "bag.txt" /*---------MACROS-----------*/
/*----------STD-------------*/ using std::ifstream; using std::cout; using std::endl; using std::map; using std::greater; using std::ios; using std::setw; /*----------STD-------------*/
/*-------GLOBAL VAL---------*/ ifstream Fin; int n; int W; int totalValue; /*-------GLOBAL VAL---------*/
/*---------MAIN-------------*/ int main(int argc, char *argv[]) { map<int,int,greater<int> > goods; Fin.open(INPUTFILE); int value; int weight; Fin>>W; Fin>>n; int i; for(i=0;i<n;i++) { Fin>>value; Fin>>weight; goods[value]=weight; }
for(map<int,int>::iterator it = goods.begin();it!=goods.end();it++) { cout<<setiosflags(ios::left)<<"value:"<<setw(4)<<it->first <<" weight:"<<setw(4)<<it->second<<endl; } for(map<int,int>::iterator it = goods.begin();it!=goods.end();it++) { if(W-it->second>=0) { W-=it->second; totalValue+=it->first*it->second; cout<<"w="<<W<<" "; } else { totalValue+=W*it->first; cout<<"totalValue:"<<totalValue<<endl; break; } } system("PAUSE"); return EXIT_SUCCESS; } /*---------MAIN-------------*/ BAG.TXT 100 10 3 43 5 22 6 4 4 67 2 3 45 2 4 2 42 24 41 4 34 55
vc : CMD: 1. vcvars.bat 2. cl /FAs ***.cpp 注意要将VC98/bin下面的文件VCVARS.BAT带到 所要编译的CPP文件所在的文件夹之下
borlandc: 1.设置环境变量右击我的电脑进入高级环境进行系统:path的设置:加入*\Borland\BCC55\BIN; 2.在*\BIN文件夹下添加两个文件bcc32.cfg 文件内容为: -I "*\Borland\BCC55\INCLUDES" -L "*\BORLAND\BCC55\LIBS" 另外的一个文件名为:ilink32.cfg: -L "*\BORLAND\BCC55\LIBS" 3.CMD: BCC32 -S *CPP/C
gcc/g++:gcc for c file and g++ for cpp 只要安装了DEVCPP就可以使用g++/gcc gcc -S *c g++ -S *cpp
#include <functional> #include <vector> #include <algorithm> #include <iostream>
using namespace std;
class out_times_x { private: int multiplier; public: out_times_x(const int& k) : multiplier(k) { } void operator()(const int& x) { cout << x * multiplier << " " << endl; } };
int main () { int sequence[5] = {1,2,3,4,5}; vector<int> v(sequence+0, sequence+5); out_times_x f2(2); for_each(v.begin(),v.end(),f2); // Apply function
system("pause"); return 0; }
win32:DEV C++ 格式: #include <iostream> using namespace std;
int a=1;/*a,b 应当都是全局的*/ int b=10;
int main(int argc,char **argv) { cout<<sizeof(int)<<endl; /*AT&T 规范 Not Intel*/ __asm("mov _b,%eax");//mov 的左边是源,右边是目标 __asm("mov %eax,_a");
cout<<a<<endl; cout<<b<<endl; cout<<"a+b="<<a+b<<endl; getchar(); return(a-b); } win32:VC6.0格式: #include <iostream> using namespace std; int main(int argc,char **argv) { /*int的长度和eax的长度一样都是4个字节*/ cout<<sizeof(int)<<endl; int a=100; int b=10; /*Intel 规范*/ __asm{ mov eax,a;//eax 是目标,a 是源 add eax,b; mov a,eax; } cout<<a<<endl; cout<<"a+b="<<a<<endl; return(a-b); }
#include <iostream> #include <queue>//有priority_queue using namespace std; class cl { public: int i; }; bool operator>(const cl&a, const cl & b) { return a.i < b.i; } bool operator<(const cl&a,const cl &b) { return a.i>b.i; } int main() { priority_queue<cl,vector<cl>,greater<vector<cl>::value_type> > q; cl a; while(cin>>a.i) { q.push(a); } while(!q.empty()) { cout<<q.top().i<<endl; q.pop(); } system("PAUSE"); return 1; }
/* Romberg Algorithm 开发者:newplan 开发日期:08.05.07 */
/*=======================================*/ /*INCLUDES*/ #include <cstdlib> #include <iostream> #include <cmath> /*=======================================*/ /*MACROS USED IN THIS FILE*/ #define MAX 20 #define PRECISION 0.000008 /*=======================================*/ /*DECLARE NAMES IN STL NAMESAPCE */ using std::cout; using std::endl;
/*=======================================*/ /*CLASS FUNC (FUNCTION OBJECT): THE ORIGINAL FUNCTION WE WANT TO INTEGRAL*/ class func{ public: func(double x=1.0):exp(x){} double operator()(const double& dnum)const{return pow(dnum,exp);} private: double exp; }; /*=======================================*/ /*CLASS ECHELONFUNC (FUNCTION OBJECT)梯形法的递推公式*/ class echelonFunc{ public: echelonFunc(double begining,double ending,func & myfunc); double operator()(); private: double h; int n; double T; func myfunc; double begining; double ending ; }; /*=======================================*/ echelonFunc::echelonFunc(double begining,double ending,func & myfunc) { this->begining=begining; this->ending=ending; this->h=ending-begining; this->n=0; this->T=0; this->myfunc=myfunc;//FUCNTION } /*------------------------------*/ /* INCREASE FUNCTION 递推函数*/ double echelonFunc::operator()() { if(this->n==0) { this->T=h*0.5*(myfunc(this->begining)+myfunc(this->ending)); this->n=1; return this->T; } double len=0.5*h; double sum=0; int k=0; for(k=0;k<this->n;k++) { sum+=myfunc(len); len=len+h; } this->T=0.5*this->T+0.5*h*sum; this->h/=2; this->n*=2; return this->T; } /*=======================================*/ /*THE MAIN CLASS IN THIS PROGRAM*/ class Romberg{ public: Romberg(double begining,double ending,double exp); ~Romberg(); private: void RombergCPU();/*THE MOST IMPORTANT FUNCTION IN THIS PROGRAM*/ echelonFunc *echol; double T[MAX][MAX];/*STO THE ROMBERG TABLE*/ }; /*------------------------------*/ Romberg::Romberg(double begining ,double ending ,double paraexp) { func myfunc(paraexp); echol = new echelonFunc(begining,ending,myfunc); RombergCPU(); } /*------------------------------*/ Romberg::~Romberg() { delete echol; } /*------------------------------*/ void Romberg::RombergCPU() { clock_t Start; //TIME STARAT clock_t End; //TIME END double *p[MAX];//WE USE THIS POINTER ARRAY TO ACCELERATE ALGOTITHM double **q; int i,j; Start = clock();//TIME START FROM HERE for(i = 0,q = p; q < p+MAX; q++,i++) *q= &T[i][0]; double a,b,pows; cout<<"-----------------------Romberg Algorithm---------------------"<<endl; *p[0]=(*echol)(); cout<<" "<<*p[0]<<endl; p[0]++; do{ *p[0]=(*echol)(); cout<<" "<<*p[0]; p[0]++; for(i=1;;i++) { pows=pow(4.0,double(i)); a=pows/(pows-1); b=1/(pows-1); *p[i]=a*(*(p[i-1]-1))-b*(*(p[i-1]-2));//ROMBERG ALGORITHM cout<<" "<<*p[i]; if(p[i]==&T[i][0]) { p[i]++; break; } p[i]++; } cout<<endl;//fabs(T[i][0]-T[i-1][0]) }while(fabs(T[i][0]-T[i-1][0])>PRECISION); End = clock();//TIME END HERE cout<<"-------------------------------------------------------------"<< endl<<" TIME SPEND:"<<(double)(End-Start)/CLOCKS_PER_SEC<<endl; } /*=======================================*/ /*MAIN FUNCTION*/ int main(int argc, char *argv[]) { Romberg romberg(0,1,1.5);//ROMBERG API :BEGIN(0) END(1) EXP(1.5) system("PAUSE"); return EXIT_SUCCESS; }
/* *用来测试STL hash_map *简单例子2008.5.5 */ #include <cstdlib> #include <iostream> #include <string> #include <hash_map.h>/*因为hash_map暂不为CPP标准所以没办法写为<hash_map>*/ /*-------------------------------------------*/ using std::cout; using std::endl; using std::string; /*-------------------------------------------*/ /*函数类 *作为hash_map的hash函数 *string没有默认的hash函数 */ class str_hash{ public: size_t operator()(const string& str) const { unsigned long __h = 0; for (size_t i = 0 ; i < str.size() ; i ++) __h = 5*__h + str[i]; return size_t(__h); } }; /*-------------------------------------------*/ /*函数类 *作为hash_map的比较函数 ) *(查找的时候不同的key往往可能对用到相同的hash值 */ class str_compare { public: bool operator()(const string& str1,const string& str2)const {return str1==str2;} }; /*-------------------------------------------*/ int main(int argc, char *argv[]) { hash_map<string,string,str_hash,str_compare> myhash; myhash["google"]="newplan"; myhash["baidu"]="zhaoziming"; if(myhash.find("google")!=myhash.end()) cout<<myhash["google"]<<endl; system("PAUSE"); return EXIT_SUCCESS; } /*-------------------------------------------*/
#include <stdio.h> #include <ctype.h>
int lookahead; void error() { printf("synatax error\n"); exit(1); } void match(int t) { if(lookahead==t) lookahead=getchar(); else error(); } void term() { if(isdigit(lookahead)) { putchar(lookahead); match(lookahead); } else error(); } void exptr() { term(); while(1) { if(lookahead=='+') { match('+');term();putchar('+'); } else if(lookahead=='-') { match('-');term();putchar('-'); } else break; } } int main(int argc, char *argv[]) { lookahead=getchar(); exptr(); putchar('\n'); system("PAUSE"); return 0; }
#include "apue.h" #include "semaphore.h" #include "pthread.h"
#define N 5 static int necs; static sem_t *forks;
void * takeFork(int i) { if(i==N-1) { sem_wait(&forks[0]); sem_wait(&forks[i]); } else { sem_wait(&forks[i]); sem_wait(&forks[i+1]); } } void * putFork(int i) { if(i==N-1) { sem_post(&forks[0]); sem_post(&forks[i]); } else { sem_post(&forks[i]); sem_post(&forks[i+1]); } } void thinking(int i,int necs) { printf("pholosopher %d is thinking\n",i); sleep(necs);
} void eating(int i,int necs) { printf("pholosopher %d is eating\n",i); sleep(necs); } void * philosopher(void *n) { int i=(int )n; while(1) { thinking(i,necs); takeFork(i); eating(i,necs); putFork(i); } } //============================main function int main(int argc,char *argv[]) { pthread_t tid; if(argc==1) necs=2; else if(argc ==2) necs=atoi(argv[1]); else return 1; forks=(sem_t*)malloc(N*sizeof(sem_t)); int i; for(i=0; i<N; i++) sem_init(forks+i,0,1);
int status; for(i=0;i<N;i++) { status=pthread_create(&tid,NULL,philosopher,(void*)i);
if(status<0) err_sys("create error!"); } pthread_join(tid ,NULL); return 0; }
#include "apue.h" #include "lock.h" #include "lock.c"
/*define some important variable*/ /*=======================================*/ pid_t pid;
static char *forks[5]={ "fork0", "fork1", "fork2" ,"fork3" ,"fork4" };
static int necs;
#define N 5
/*======================================*/ void takeFork(int i) { if(i==N-1) { lock(forks[0]); lock(forks[i]); } else { lock(forks[i]); lock(forks[i+1]); } }
void putFork(int i) { if(i==N-1) { unlock(forks[0]); unlock(forks[i]); } else { unlock(forks[i]); unlock(forks[i+1]);
} } int thinking(int i,int necs){ printf("pholosopher %d is thinking\n",i); return sleep(necs); } int eating(int i,int necs) {printf("pholosopher %d is eating\n",i); return sleep(necs); } void philosopher(int i) { while(1) { thinking(i,necs); takeFork(i); eating(i,necs); putFork(i);
} } int main(int argc ,char *argv[]) { if(argc=1) necs=2; else if(argc==2) necs=atoi(argv[1]); else return 0; int i; for(i=0;i<N;i++) { pid=fork(); if(pid ==0) philosopher(i); } return 0; }
|