| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5652 人关注过本帖, 6 人收藏
标题:常用算法(C++描述)
只看楼主 加入收藏
风中涟漪
Rank: 1
等 级:新手上路
帖 子:234
专家分:0
注 册:2004-8-9
收藏
得分:0 
以下是引用北雪の月弦在2004-11-04 07:27:24的发言: 想问一下,大数的加, 减, 乘, 除该怎么算呀?
用数组处理进位

2004-11-05 16:58
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
得分:0 

我出一个,很简单的,Selection Algorithm,

该算法的功能是:1。Sorting the list in decreasing order, and then the Kth largest value will be in position K.

2。A related technique to this would be to find the largest value and then move it to the last location

in the list. If we again look for the largest value in the list, ignoring the value we already found,

we get the second largest value, which can be moved to the second last location in the list.

If we con tinue this process, we will find the Kth largest value on the Kth pass.

This gives the algorithm:

#include"iostream"

using namespace std;

void Find_Kth_Largest(int list[],int n,int k){

//list:the value to look through

//n:the size of the list

//k:the element to select

int i,largest,largest_location,j;

for(i=1;i<=k;i++){//for_1

largest=list[1];

largest_location=1;

for(j=2;j<=n-(i-1);j++){//for_2

if(list[j]>largest){

largest=list[j];

largest_location=j;

}//if

}//for_2

swap(list[n-(i-1)],list[largest_location]);//exchanging, making the largest elements

//in the last elements of the list

}//for_1

cout<<"The number is:"<<largest<<"\n";

}//Find_Kth_Largest

void swap(int &a,int &b){//exchange

int temp;

temp=a;

a=b;

b=temp;

}//swap

void main(){

int List[16],K,i;

cout<<"Please input the K:";

cin>>K;

cout<<"\n";

List[0]=0;

for(i=1;i<=15;i++){

List[i]=2*i-1;

List[0]++;//List[0] is stored the length of the List

}//for

cout<<"\n"<<List[0]<<"\n";//output the length

Find_Kth_Largest(List,List[0],K);

}

需要翻译

[此贴子已经被zinking于2005-10-21 13:44:26编辑过]

2004-11-06 09:32
三少爷
Rank: 1
等 级:新手上路
帖 子:192
专家分:0
注 册:2004-4-29
收藏
得分:0 

偶来个,,看上去挺吓人的,其实更简单~

/*【Background】

Personally it's considered as meaningful in exercise

【Problem Description】

求n之内的所有偶数表示为两个素数之和。 注:歌德巴赫猜想是要证明对任何大于6的自然数n命题成立。

【Input Example】

10

【Output】

6=3+3

8=3+5

10=3+7 */

file://IDE:VC++6.0 file://long型最大值范围内求解素数(之所以只是用long,因为范围再大也是一样永远是有限个数无法完全 file://证明此命题,只是一个示例验证罢了) file://素数就用的那经典判定法:2到此数square root不能被此数整除 #include <iostream.h> #include <math.h> #include <conio.h>

bool isPrime(long n) { for(int i=2;i<=sqrt(n);i++) if(n%i==0) return false; return true; }

int main() { long n; cin>>n; for(long i=6;i<=n;i+=2) for(long j=3;j<=i;j+=2) if(isPrime(j)&&isPrime(i-j)) { cout<<i<<"="<<j<<"+"<<i-j<<endl; break; } getch(); return 0; }


2004-11-12 23:18
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
得分:0 

我水,就写个简单点的好了(复数——学习重载最好了)

#include<iostream.h> class Fushu { public: Fushu(){}; Fushu(float r,float i); friend Fushu operator +(Fushu &c1,Fushu &c2); friend Fushu operator -(Fushu &c1,Fushu &c2); friend Fushu operator *(Fushu &c1,Fushu &c2); friend Fushu operator /(Fushu &c1,Fushu &c2); void display(); private: float real; float imag; }; Fushu::Fushu(float r,float i) { real=r; imag=i; } void Fushu::display() { if(real!=0) { if(imag>0) { if(imag=1) cout<<real<<"+i"<<endl; else cout<<real<<"+"<<imag<<"i"<<endl; } if(imag<0) cout<<real<<imag<<"i"<<endl; else cout<<real<<endl; } else { if(imag>0) { if(imag=1) cout<<"i"<<endl; else cout<<"+"<<imag<<"i"<<endl; } if(imag<0) cout<<imag<<"i"<<endl; if(imag=0) cout<<"0"<<endl; } } Fushu operator +(Fushu &c1,Fushu &c2) { Fushu temp; temp.real=c1.real+c2.real; temp.imag=c1.imag+c2.imag; return temp; } Fushu operator -(Fushu &c1,Fushu &c2) { Fushu temp; temp.real=c1.real-c2.real; temp.imag=c1.imag-c2.imag; return temp; } Fushu operator *(Fushu &c1,Fushu &c2) { Fushu temp; temp.real=c1.real*c2.real-c1.imag*c2.imag; temp.imag=c1.real*c2.imag+c2.real*c1.imag; return temp; } Fushu operator /(Fushu &c1,Fushu &c2) { Fushu temp; float tt; tt=c2.real*c2.real+c2.imag*c2.imag; temp.real=(c1.real*c2.real+c1.imag*c2.imag)/tt; temp.imag=(-c1.real*c2.imag+c2.real*c1.imag)/tt; return temp; } int main() { Fushu x(1,1),y(1,-1),z; z=x+y; z.display(); z=x-y; z.display(); z=x*y; z.display(); z=x/y; z.display(); return 0; }


2004-11-13 19:22
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
得分:0 

我把书上的Selection Algorithm编程实现了:

#include"iostream"

using namespace std;

void Find_Kth_Largest(int list[],int n,int k){

//list:the value to look through

//n:the size of the list

//k:the element to select

int i,largest,largest_location,j;

for(i=1;i<=k;i++){//for_1

largest=list[1];

largest_location=1;

for(j=2;j<=n-(i-1);j++){//for_2

if(list[j]>largest){

largest=list[j];

largest_location=j;

}//if

}//for_2

swap(list[n-(i-1)],list[largest_location]);//exchanging, making the largest elements

//in the last elements of the list

}//for_1

cout<<"The number is:"<<largest<<"\n";

}//Find_Kth_Largest

void swap(int &a,int &b){//exchange

int temp;

temp=a;

a=b;

b=temp;

}//swap

void main(){

int List[16],K,i;

cout<<"Please input the K:";

cin>>K;

cout<<"\n";

List[0]=0;

for(i=1;i<=15;i++){

List[i]=2*i-1;

List[0]++;//List[0] is stored the length of the List

}//for

cout<<"\n"<<List[0]<<"\n";//output the length

Find_Kth_Largest(List,List[0],K);

}

2004-11-22 03:58
唐伯猫
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:5323
专家分:58
注 册:2005-8-9
收藏
得分:0 
都是高手啊!

<iframe name="alimamaifrm" frameborder="0" marginheight="0" marginwidth="0" border="0" scrolling="no" width="300" height="170" src="/go/app/tbk_app/chongzhi_300_170.php?pid=mm_28854300_2441872_11377541&page=chongzhi_300_170.php&size_w=300&size_h=170&stru_phone=1&stru_game=1&stru_travel=1" ></iframe>
2005-08-09 18:14
Wolfelee
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2005-3-10
收藏
得分:0 
引用:===》 “josephus问题(算法思想)

//N个小孩围成一圈报数,凡是报到指定数字的小孩离开圈子 //打印最后剩下的小孩的号码

#include<iostream.h>

void main() { const int N=10; //假定有10个小孩 int kid[N],*p; int interval; //报到此数的小孩离开 int count=0,leave=0; //报数计数器和离开的小孩数

for(int i=0;i<N;i++) kid[i]=i+1; //给每个小孩一个号码

cout<<"please input the Count off number: "; cin>>interval;

while(leave!=N-1) //当离开人数不等于总人数 { for(p=kid;p<kid+N;p++) //从号码是1的小孩开始数

if(*p!=0) //已离开的小孩不用报数 { count++;

if(count==interval) //报到此数时 { count=0; //由1开始重新报数 cout<<*p<<" "; *p=0; //离开的小孩号码置零 leave++; } } } ” 其中: “while(leave!=N-1) //当离开人数不等于总人数” 中注释是不是应该“//当离开人数不等于总人数‘减1啊?’”


对酒当歌,人生几何?
2005-08-09 23:10
wwwindowsxp
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-18
收藏
得分:0 
关于汉诺塔问题的另类解
汉诺塔的终极经典的算法。 可以随手画出答案的方法。 http://bbs.ddvip.net/forumdisplay.php?f=32
汉诺塔.pdf

[此贴子已经被作者于2005-10-18 16:20:44编辑过]

2005-10-18 16:12
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
这样的代码未尝不好,只是读起来实在费解阿,我提议帖子的作者把
你们自己的代码加上详细的注解,
这样才真正帮助了大家提高了思想。
不只过不过份
特别是kai阿,knocker阿,在这里可以秀一下了。

http://kongfuziandlife. http://codeanddesign.
2005-10-18 18:41
whaozhe
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2005-10-21
收藏
得分:0 
我想学编程!!!



真的!1

2005-10-21 11:56
快速回复:常用算法(C++描述)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019836 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved