缩小的陆地+排序

来源:互联网 发布:使命召唤12优化差 编辑:程序博客网 时间:2024/04/27 02:56


1.http://www.tsinsen.com/ViewGProblem.page?gpid=A1016

题目,编号1016

代码

#include <iostream>#include <math.h>using namespace std;int main(){    int n=0;    double x,y;    cin>>n;    int result[100000];    for(int i=0;i<n;i++)    {        cin>>x>>y;        result[i]=(x*x+y*y)/100*M_PI+1;    }
    int pos=rand()%(e-s+1)+s;    int key=a[pos];
for(int i=0;i<n;i++) cout << result[i] << endl; return 0;}
比较坑的是,我一开始除的是50,忽略了土地是半圆。要先恢复成整个圆。

另外题目说明求出整数也要加一。

比方说20.1->21

20->21

并且int转换直接就是去点小数点,所以统一处理加一。

M_PI是π的表示。要引入math

2.char[]赋值时要注意最后一个空间留给了'\0',char str[4]="abc";且不能是"a,b,c"或者任何其他符号。

3.快排,用数列中的任意数,用的是c++

3.1随机数的头文件codeblocks

#include <time.h>#include <stdlib.h>
随机的选取数列中的元素
    int pos=rand()%(e-s+1)+s;    int key=a[pos];
//因为是随机的,所以要注意两个地方,一个是最后谁与选定数字交换,low还是high。
//以及要防止low,high越界low不能大于high然后还去找他们之间的随机数。每次快排都要保证两边的序号不能交错。
//还有输出的时候别忘了空格,我忘了,又一直用单数测试就没发现
#include <iostream>#include <math.h>#include <time.h>#include <stdlib.h>using namespace std;int a[202];void qs(int s,int e){    if(s<=e){    srand((int)time(NULL));    int pos=rand()%(e-s+1)+s;    int key=a[pos];    int low=s;    int high=e;    while(low<=high)    {        while(a[low]<=key&&low<=high)            low++;        while(a[high]>=key&&low<=high)            high--;        if(low<=high)            swap(a[low],a[high]);    }    if(pos>=low)    {        swap(a[low],a[pos]);        qs(low+1,e);        qs(s,high);    }    if(pos<=high)    {        swap(a[high],a[pos]);        qs(s,high-1);        qs(low,e);    }    }}int main(){    int n;    cin>>n;    for(int i=0; i<n; i++)        cin>>a[i];    qs(0,n-1);    for(int i=0; i<n; i++)        cout<<a[i]<<" ";    return 0;}
//使用随机的序号定为key可以避免出现最坏情况,也就是数列本来有序,每次都选第一个,那么递归深度就是n,不论是存储空间还是时间都浪费了。
//但是能节省就节省,所以,最好还是用提供的算法
#include <iostream>#include <algorithm>using namespace std;int main(){    int n;    cin>>n;    int a[202];    for(int i=0;i<n;i++)        cin>>a[i];        sort(a,a+n);        for(int i=0;i<n;i++)            cout<<a[i]<<" ";    return 0;}
//这里要注意一个石头文件,一个是sort的用法,n是长度。
                                             
0 0