锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng) 姹傚彲鑳芥儏鍐電殑鎬繪暟
瑙f硶涓:
聽(tīng)#include <stdio.h>
#include <stdlib.h>
int f(int n, int m)
{
聽(tīng)int fn=0,i;
聽(tīng)if(m<0||m>10*n) return 0;
聽(tīng)if(n==1) return 1;
聽(tīng)for(i=0;i<=10;i++)
聽(tīng)fn+=f(n-1,m-i);
聽(tīng)return fn;
}
int main()
{
聽(tīng)int n,m;
聽(tīng)printf("Please enter n and m:");
聽(tīng)scanf("%d%d",&n,&m);
聽(tīng)printf("\n%d鍙戞墦涓?d鐜湁%d縐嶅彲鑳?\n",n,m,f(n,m));
聽(tīng)system("PAUSE");
聽(tīng)return 0;
}聽(tīng)
瑙f硶浜?
聽(tīng)#include"stdio.h"
int main(){
int num =1,i;
for(i=1;i<=10;i++)
num = num *(9+i)/i;
printf("%d",num);
}
涓漢瑙傜偣:
聽(tīng) 璁$畻鏈烘槸涓闂ㄥ拰鏁板鐩稿叧鐨勫縐?闅忕潃榪欓棬瀛︾鐨勫彂灞?榪欑"鏈川"浼間箮琚急鍖栦簡(jiǎn).瑙f硶1鏄緢鏅氱殑鏂規(guī)硶,涓鑸漢閮芥槸榪欑鎬濊礬.鑰岃В娉曚簩鍒欐槸浠庢暟瀛﹁搴﹁冭檻.綆媧?蹇?
涓婃鍘葷鐮佺瑪璇?紕板埌涓棰?br />聽(tīng)聽(tīng) :緇欏畾涓涓暟,濡?899,鎶婂悇浣嶆暟鍊肩浉鍔?+8+9+9=33,3+3=6,鐢ㄨ繖縐嶆柟娉曡綆楁満涓嬪幓,姹傛渶鍚庡緱鍒扮殑涓綅鏁?br />
鎴戠粰鍑虹殑瑙f硶:
#include "stdio.h"
int main(){
聽(tīng)聽(tīng)聽(tīng)int i;
聽(tīng)聽(tīng)聽(tīng)scanf("%d",&i);
聽(tīng)聽(tīng)聽(tīng)printf("%d",i%9==0?9:i%9);
聽(tīng)聽(tīng) return 0
}//~end聽(tīng)
聽(tīng)
#include<iterator>
#include<list>
using namespace std;
template <class Type> class Ring{
聽(tīng)list<Type> lst;
public:
聽(tīng)class iterator;
聽(tīng)friend class iterator;
聽(tīng)class iterator :public std::iterator<std::bidirectional_iterator_tag,Type,ptrdiff_t>{
聽(tīng)聽(tīng)typename list<Type>::iterator it;
聽(tīng)聽(tīng)list<Type>* r;
聽(tīng)public:
聽(tīng)聽(tīng)iterator(list<Type>&lst,const typename list<Type>::iterator& i):it(i),r(&lst){}
聽(tīng)聽(tīng)bool operator==(const iterator& x)const{
聽(tīng)聽(tīng)聽(tīng)return it==x.it;
聽(tīng)聽(tīng)}
聽(tīng)聽(tīng)bool operator!=(const iterator& x)const{
聽(tīng)聽(tīng)聽(tīng)return!(*this==x);
聽(tīng)聽(tīng)}
聽(tīng)聽(tīng)typename list<Type>::reference operator*()const{
聽(tīng)聽(tīng)聽(tīng)return *it;
聽(tīng)聽(tīng)}
聽(tīng)聽(tīng)iterator& operator++(){
聽(tīng)聽(tīng)聽(tīng)++it;
聽(tīng)聽(tīng)聽(tīng)if(it==r->end())
聽(tīng)聽(tīng)聽(tīng)聽(tīng)it=r->begin();
聽(tīng)聽(tīng)聽(tīng)return *this;
聽(tīng)聽(tīng)}
聽(tīng)聽(tīng)iterator operator++(int){
聽(tīng)聽(tīng)聽(tīng)iterator tmp=*this;
聽(tīng)聽(tīng)聽(tīng)++*this;
聽(tīng)聽(tīng)聽(tīng)return tmp;
聽(tīng)聽(tīng)}
聽(tīng)聽(tīng)iterator &operator--(){
聽(tīng)聽(tīng)聽(tīng)if(it==r->begin())
聽(tīng)聽(tīng)聽(tīng)聽(tīng)it=r->end();
聽(tīng)聽(tīng)聽(tīng)--it;
聽(tīng)聽(tīng)聽(tīng)return *this;
聽(tīng)聽(tīng)}
聽(tīng)聽(tīng)iterator operator--(int){
聽(tīng)聽(tīng)聽(tīng)iterator tmp=*this;
聽(tīng)聽(tīng)聽(tīng)--*this;
聽(tīng)聽(tīng)聽(tīng)return tmp;
聽(tīng)聽(tīng)}
聽(tīng)聽(tīng)iterator insert(const Type& x){
聽(tīng)聽(tīng)聽(tīng)return iterator(*r,r->insert(it,x));
聽(tīng)聽(tīng)}
聽(tīng)聽(tīng)
聽(tīng)聽(tīng)//I have to recognize that the iterator is not so smart as
聽(tīng)聽(tīng)//we expected .If the element in the list is erased ,the
聽(tīng)聽(tīng)//iterator will be lost and point to a null.The shortage of
聽(tīng)聽(tīng)//the stupid iterator will been seen the the Josephus.cpp
聽(tīng)聽(tīng)//we have to use a template variable to storage the next iterator
聽(tīng)聽(tīng)//of the deleting iterator.If not ,the programe will be nightmare
聽(tīng)聽(tīng)iterator erase(){
聽(tīng)聽(tīng)聽(tīng)return iterator(*r,r->erase(it));
聽(tīng)聽(tīng)}
聽(tīng)};
聽(tīng)void push_back(const Type& x){lst.push_back(x);}
聽(tīng)iterator begin(){return iterator(lst,lst.begin());}
聽(tīng)int size(){return lst.size();}
};
#endif
瀹炵幇鏂囦歡:
//:Using circle list to solve Josephus problem
//:version 1.01
//:author Murainwood 12/3/2006
#include<iostream>
#include"Ring.h"
void聽(tīng) main(){
聽(tīng)//enter the number of people and the index
聽(tīng)int n,m;
聽(tīng)cout<<"Enter the Number of Contestants?";
聽(tīng)cin>>n>>m;
聽(tīng)聽(tīng)聽(tīng)
聽(tīng)
聽(tīng)聽(tīng)聽(tīng) Ring<int> ring;
聽(tīng)for(int i=1;i<=n;i++) ring.push_back(i);
聽(tīng)//determine the iterator
聽(tīng)//it is reasonable聽(tīng) to declare two iterator
聽(tīng)聽(tīng)聽(tīng) Ring<int>::iterator tmp=ring.begin();
聽(tīng)Ring<int>::iterator it=tmp;
聽(tīng)
聽(tīng)//without the iterator tmp ,if we erase the it
聽(tīng)//the it will lost ,so the loop can not聽(tīng) work
聽(tīng)for(int index=0;index<n-1;index++){
聽(tīng)聽(tīng)聽(tīng)it=tmp;
聽(tīng)聽(tīng)for(int j=0;j<m-1;j++){it++;tmp++;}
聽(tīng)聽(tīng)聽(tīng)tmp++;
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng) cout<<"Delete person"<<*it<<endl;
聽(tīng)聽(tīng)聽(tīng)it.erase();
聽(tīng)聽(tīng)聽(tīng)
聽(tīng)}
聽(tīng)it=ring.begin();
聽(tīng)cout<<"The result is person"<<*it<<endl;
}
//:finished
聽(tīng)聽(tīng)聽(tīng)聽(tīng) 榪欎釜Ring List鏈夊緢楂樼殑鏁堢巼鍜屽畨鍏ㄦ?騫朵笖閫氱敤鎬т篃姣旇緝濂?br />聽(tīng)聽(tīng)聽(tīng)聽(tīng) 榪欓噷涔熷彂鐜頒釜闂,灝辨槸STL閲岀殑Iterator 榪樹(shù)笉澶?鑱槑",榪欎篃鏄負(fù)浠涔堟垜浼?xì)鍦ńE嬪簭閲屽鍔犱釜榪唬鍣?tmp,
鍥犱負(fù)濡傛灉鎴戣皟鐢ㄤ簡(jiǎn)it.erase();閭d箞榪欎釜榪唬鍣ㄥ氨涓㈠け浜?紼嬪簭涔熷氨鏃犻潪榪愯涓嬪幓.
聽(tīng)聽(tīng)聽(tīng)聽(tīng) 榪欐牱鎴戜滑寰堝鏄撳湴鎯?濡傛灉璋冪敤浜?jiǎn)it.erase()鍚?鎸囬拡涓嶆槸涓㈠け,鑰屾槸鎸囧悜涓嬩竴涓厓绱犺澶氬ソ鍟?
聽(tīng)聽(tīng)聽(tīng)聽(tīng) 寰堥仐鎲?鐜伴樁孌墊爣鍑哠TL搴撻噷鐨凩ist娌℃湁榪欎釜鍔熻兘.鐚滄兂鏄洜涓篖ist鐨勭墿鐞嗗瓨鍌ㄧ粨鏋勭殑緙樻晠,濡傛灉璁╀粬鐨勮凱浠e櫒鍙樺緱濡傛垜浠兂鍍忓緱閭d箞"鑱槑",閭d箞鑺辮垂鍙兘浼?xì)鏈変簺澶?
聽(tīng)聽(tīng)聽(tīng) 榪樻病鏈夐獙璇佽繃boost搴?涓嶇煡閬撻偅閲岀殑榪唬鍣ㄦ槸鍚︿細(xì)鏇翠漢鎬у寲浜?br />聽(tīng)聽(tīng)聽(tīng)聽(tīng) 鏈夋椂闂寸爺絀朵竴涓嬬浉鍏崇殑STL婧愮爜,鍐欎竴涓?鑱槑浜?鐨勮凱浠e櫒.