爱嘉牛LA

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

随笔分类 -  程序

摘要:View Code #include<iostream>using namespace std;int gcd(int x,int y){ return y==0?x:gcd(y,x%y);}int main(){ int x,y; cin>>x>>y; cout<<gcd(x,y)<<endl; return 0;} 阅读全文
posted @ 2012-05-20 10:22 爱嘉牛LA 阅读(232) 评论(0) 推荐(0)

摘要:View Code #include<iostream>using namespace std;string reverse(string &str){ if(str.length()>1){ string sub=str.substr(1,str.length()-2); return str.substr(str.length()-1,1)+reverse(sub)+str.substr(0,1); } else return str;}int main(){ string str; cout<<"输入一个字符串:"; cin... 阅读全文
posted @ 2012-05-14 23:23 爱嘉牛LA 阅读(699) 评论(0) 推荐(0)

摘要:荷兰国旗有三横条块构成,自上到下的三条颜色依次为红,白,蓝。现有若干由红,白,蓝三种颜色的条块序列,要将它们重新排列使所有相同颜色的条块在一起。本问题要求将所有红色的条块放在最左边,所有白色的条块放在中间,所有蓝色的条块放在最右边。方法一:View Code #include<iostream>using namespace std;int main(){ char ch[100]; int n; cin>>n; int R,W,B,len; cin.get(); for(int j=0;j<n;j++){ cin.getline(ch,... 阅读全文
posted @ 2012-05-11 11:30 爱嘉牛LA 阅读(1118) 评论(0) 推荐(0)

摘要:有红,黄,蓝,白,黑,5中颜色求若干个,每次取3个不同颜色的球,问有多少种取法:View Code #include<iostream>using namespace std;int main(){ enum color{red,yellow,blue,white,black}; enum color pri; int n,loop,i,j,k; char c; n=0; for(i=red;i<=black;i++){ for(j=red;j<=black;j++){ if(i!=j){ f... 阅读全文
posted @ 2012-05-08 08:06 爱嘉牛LA 阅读(502) 评论(0) 推荐(0)

摘要:文件中格式如下:4334.4 3.2 9.48.43 23.23 2.343.4 43.1 9.39.8 7.4 1.3View Code #include <fstream>#include <fstream>#include <iostream>using namespace std;int main(){ int n,m; ifstream fin("D:\训练样本集文件.txt"); fin>>n;//文件的第一个作为数组数据个数 fin>>m;//文件的第二个作为样本维数 double a[n][m]; 阅读全文
posted @ 2012-05-04 07:59 爱嘉牛LA 阅读(500) 评论(0) 推荐(0)

摘要:欧几里得距离 欧几里得距离定义: 欧几里得距离( Euclidean distance)也称欧式距离,它是一个通常采用的距离定义,它是在m维空间中两个点之间的真实距离。 在二维和三维空间中的欧式距离的就是两点之间的距离,二维的公式是 d = sqrt((x1-x2)^+(y1-y2)^) 三维的公式是 d=sqrt((x1-x2)^+(y1-y2)^+(z1-z2)^) 推广到n维空间,欧式距离的公式是 d=sqrt( ∑(xi1-xi2)^ ) 这里i=1,2..n xi1表示第一个点的第i维坐标,xi2表示第二个点的第i维坐标 n维欧氏空间是一个点集,它的每个点可以表示... 阅读全文
posted @ 2012-05-03 13:57 爱嘉牛LA 阅读(8589) 评论(2) 推荐(0)

摘要:View Code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main() { int a=2,b=9; srand((unsigned)time(NULL)); for(int i=0; i<10;i++ ) cout<<rand()%(b-a)+a<<endl; cout << endl; return 0; }<一>生成[a,b)的随机整数,使用rand()%(... 阅读全文
posted @ 2012-04-26 19:33 爱嘉牛LA 阅读(624) 评论(0) 推荐(1)

摘要:View Code 1 #include<iostream> 2 #include<string> 3 #include<sstream> 4 using namespace std; 5 int main(){ 6 int x,n,i=0; 7 long sum; 8 string line; 9 cin>>n;10 getline(cin,line);//吸取第一行的尾数结束标志11 while(getline(cin,line)){12 istringstream iss(line);13 sum=0;14... 阅读全文
posted @ 2012-04-20 23:17 爱嘉牛LA 阅读(558) 评论(4) 推荐(0)