2018年模板大集合!!!!没有一个优秀的模板就是等着被摩擦[各种用法]

fgets的使用方法:

fgets(s1,sizeof(s1),stdin)

c++11中类似hash_map,解决冲突问题,当做比较快的map来用


#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
cc_hash_table<string, int> h1; //拉链法处理冲突(比较快
gp_hash_table<string, int> h2; //探测法处理冲突

对拍生成一棵树,这里把每个点乱序加入vector中,然后每两个点直接连一条边即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int q1[maxn],q2[maxn];
bool vis[maxn];
vector<int>edge;
int main(){
    int i,j,k,f1,f2,f3,f4,t1,t2,t3,t4;
    int n,m;
    n=10;m=20;
    srand(time(NULL));
    cout <<n <<" "<<m<< endl; //n个点,n-1条边
    for(i=1;i<=n;i++){
        t1=rand()%n+1;
        if(vis[t1]){
        i--;
        continue;
        }
        edge.push_back(t1);
        vis[t1]=1;
    }
    for(i=1;i<edge.size();i++){
        cout << edge[i-1] <<" "<<edge[i]<<endl;
    }
    return 0;
}

poj2420(模拟退火):

有意思,找到一个点,其距离所有点的距离和最小,输出这个距离

//#include<bits/stdc++.h>
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
using namespace std;
const int N=1005;
const double eps=1e-8;//搜索条件终止点
const double delta=0.99; //下降速度
const int T=10000;//初始温度
const int maxn=1e5+7;
const int inf=1e9+7;
int dr[4]={0,1,-1,0};
int dc[4]={1,0,0,-1};
struct ttt{
    double x,y;
};
ttt q1[maxn];
double dis(ttt a,ttt b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int n;
double Sum(ttt x){
    double len1=0;
    for(int i=1;i<=n;i++){
        len1+=dis(x,q1[i]);
    }
    return len1;
}
double solve(){
    ttt s,q;
    double t=T;
    double ans=inf;
    s.x=s.y=0;
    for(int i=1;i<=n;i++){
        s.x+=q1[i].x;s.y+=q1[i].y;
    }
    s.x/=n*1.0;s.y/=n*1.0;
    int ci=0,j;
    bool find1;
    while(t>eps){
        find1=1;
        while(find1){ //当这个步长能找到,那么就接着找
            ci++;
            find1=0;
            for(int i=0;i<4;i++){ //4个方向,乘步长
            j=i;
            //j=rand()%4+1; //这里多一手随机
            q.x=s.x+dr[j]*t;
            q.y=s.y+dc[j]*t;
            double tmp=Sum(q);
            if(ans>tmp)
                ans=tmp,s=q,find1=1;
        }
        }
        t*=delta;
    }
    //printf("times =  %d\n",ci);
    return ans;
}
int main(){
    int i,j,k,f1,f2,f3,f4,t1,t2,t3,t4,m;
    //freopen("in.txt","r",stdin);
    freopen("out1.txt","w",stdout);
    while(scanf("%d",&n)!=EOF){
        for(i=1;i<=n;i++)
        scanf("%lf %lf",&q1[i].x,&q1[i].y);
        //printf("%lf\n",q1[i].x);
        printf("%.0f\n",solve());
    }
    return 0;
}

读入流的使用:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+7;
string s;
stringstream ss;
char s1[maxn];
int main(){
    freopen("in.txt","r",stdin);
    int i,j,k,f1,f2,f3,f4,t1,t2,t3,t4,n,m;
    n=3;
    while(getline(cin,s)){ //s改为char 就可以得到之间的空格数量了
        ss.clear();
        ss.str(s);
        int sum=0;
        while(1){
            ss >> s1;
            if(ss.fail())break;
            cout <<s1 <<endl;
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值