新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 C/C++编程思想 』 → 常用算法(C++描述)--网友提供 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 21959 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 常用算法(C++描述)--网友提供 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     DMman 帅哥哟,离线,有人找我吗?魔羯座1984-1-11
      
      
      威望:1
      头衔:数据挖掘青年
      等级:研二(Pi-Calculus看得一头雾水)(版主)
      文章:803
      积分:5806
      门派:W3CHINA.ORG
      注册:2007/4/9

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给DMman发送一个短消息 把DMman加入好友 查看DMman的个人资料 搜索DMman在『 C/C++编程思想 』的所有贴子 点击这里发送电邮给DMman 访问DMman的主页 引用回复这个贴子 回复这个贴子 查看DMman的博客11
    发贴心情 

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

    /*【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.0file://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;
    }

    ----------------------------------------------
    数据挖掘青年 http://blogger.org.cn/blog/blog.asp?name=DMman
    纪录片之家 (很多纪录片下载)http://www.jlpzj.com/?fromuid=137653

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/9/27 12:57:00
     
     DMman 帅哥哟,离线,有人找我吗?魔羯座1984-1-11
      
      
      威望:1
      头衔:数据挖掘青年
      等级:研二(Pi-Calculus看得一头雾水)(版主)
      文章:803
      积分:5806
      门派:W3CHINA.ORG
      注册:2007/4/9

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给DMman发送一个短消息 把DMman加入好友 查看DMman的个人资料 搜索DMman在『 C/C++编程思想 』的所有贴子 点击这里发送电邮给DMman 访问DMman的主页 引用回复这个贴子 回复这个贴子 查看DMman的博客12
    发贴心情 
    我水,就写个简单点的好了(复数——学习重载最好了)

    #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;
    }

    ----------------------------------------------
    数据挖掘青年 http://blogger.org.cn/blog/blog.asp?name=DMman
    纪录片之家 (很多纪录片下载)http://www.jlpzj.com/?fromuid=137653

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/9/27 12:58:00
     
     DMman 帅哥哟,离线,有人找我吗?魔羯座1984-1-11
      
      
      威望:1
      头衔:数据挖掘青年
      等级:研二(Pi-Calculus看得一头雾水)(版主)
      文章:803
      积分:5806
      门派:W3CHINA.ORG
      注册:2007/4/9

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给DMman发送一个短消息 把DMman加入好友 查看DMman的个人资料 搜索DMman在『 C/C++编程思想 』的所有贴子 点击这里发送电邮给DMman 访问DMman的主页 引用回复这个贴子 回复这个贴子 查看DMman的博客13
    发贴心情 
    我把书上的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);

    }

    ----------------------------------------------
    数据挖掘青年 http://blogger.org.cn/blog/blog.asp?name=DMman
    纪录片之家 (很多纪录片下载)http://www.jlpzj.com/?fromuid=137653

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/9/27 12:58:00
     
     liuyuanyang 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:8
      积分:82
      门派:XML.ORG.CN
      注册:2008/5/25

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给liuyuanyang发送一个短消息 把liuyuanyang加入好友 查看liuyuanyang的个人资料 搜索liuyuanyang在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看liuyuanyang的博客14
    发贴心情 
    谢谢~~很好~

    ----------------------------------------------
    www.luabbs.com 编程技术论坛 Lua C C++ Linux PHP MySql 游戏 Symbian PSP

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/1/16 20:32:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/2 16:48:13

    本主题贴数14,分页: [1] [2]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    93.750ms