码迷,mamicode.com
首页 > 其他好文 > 详细

简单的代码汇总

时间:2020-01-19 22:27:32      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:初始   div   +=   内联   sys   operator   oid   flag   ==   

#include<iostream>
using namespace std;
class complex
{
    public://被公有外界使用的public, 
        complex (double r=0,double i=0)
            :re(r), im(i) 
            { }
        complex& operator += (const complex&);//内联函数:函数在class内定义完成就是内联函数 ,函数如果是inline就会运行的比较快,比较好 
        double real () const{return re; }
        double imag () const{return im; }
    private:         //private是数据部分,封装起来外面的数据,不希望被外界所看到 
        double re,im;
        friend complex&_doapl (complex*, const complex&);
 };
 /*inline double
 imag(const complex& x)
 {
     return x.imag();
 }*/
 int main()
 {
     complex c1(2,1);
     complex c2;
     complex* p=new complex(4);//创建对象 
     cout<<c1.real()<<endl;
     cout<<c1.imag();
     return 0;
  } 
//D进制的两数相加 
#include<iostream>
using namespace std;
int main()
{
    int a,b,d;
    cin>>a>>b>>d;
    int sum=a+b;
    int ans[31],num=0;  
    do{
        ans[num++]=sum%d;
        sum /=d;
    } while(sum!=0);
    for(int i=num-1;i>=0;i--)
    {
        cout<<ans[i];
    }
    return 0;
}
//hash散列表 
 #include<iostream>
 using namespace std; 
 const int maxn =10010;
 bool hashTable[maxn] = {false};//初始化为FALSE 
 int main()
{
    int n,m,x;
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        hashTable[x] =true;    
    }     
    for(int i=0;i<n;i++)
    {
        cin>>x;
        if(hashTable[x]==true)
        {
            cout<<"YES"<<endl; 
        }
        else
        {
            cout<<"NO"<<endl;
        }
        return 0;
    }
} 
//八皇后 
#include<bits/stdc++.h>
using namespace std;
const int count =0;
const int maxn=11;
int n ,P[maxn],hashTable[maxn]={false}; 
void generateP(int index)
{
    if(index==n+1)
    {
        bool flag =true;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(abs(i-j)==abs(P[i]-P[j]))
                {
                    flag =false;//不合法 
                }
            }
            
         } 
         if(flag) count++;
         return ;
    }
//    if(flag) count++;
//    return;
for(int x=1;x<=n;x++)
{
    if(hashTable[x]==false)
    {
        P[index]=x;
        hashTable[x]=true;
        generateP[index+1];
        hashTable[x]=false; 
    }
} 
}


int main()
{
    int n=3;
    generateP[1];
    return 0;
}
//背包问题 
#include<iostream>
#include<algorithm>
using namespace std;
int N,V,dp[100001],c[100001],w[1000001];
int main()
{
    int i,j;
    cout<<"N=";
    cin>>N;
    cout<<"V=";
    cin>>V;
    for(i=1;i<=0;i++) cin>>c[i]>>w[i];
    for(i=1;i<N;i++)
    {
        for(j=V;j>=0;j--)
        {
            if(j<c[i])    break;
            dp[j]=max(dp[j],dp[j-c[i]]+w[i]);
        }
     } 
     cout<<dp[V]<<endl;   
    system("pause");
    return 0;    
} 

简单的代码汇总

标签:初始   div   +=   内联   sys   operator   oid   flag   ==   

原文地址:https://www.cnblogs.com/chuxinbubian/p/12215677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!