c++大一期中考

来源:互联网 发布:seo推广网站 编辑:程序博客网 时间:2024/04/27 14:48
Description
The birthday of Star's girlfriend is coming, he wants to send her a special gift, all the stars of sky. It's not so hard for him because he knows magic. The sky can be regarded as a flat plane, each star has a coordinates (x, y), no two stars have the same location. Star can draw a rectangle whoes edges are paralleled to the coordinate axes, and he can catch all the stars in the rectangle, the energy cost equals to the area of the rectangle. In order to save energy, the rectangle should be as small as possible. Here comes your problem, give you the coordinates of all the stars, what's the smallest area of the rectangle that can catch all of them?
Input

The first line is an integer N, the number of stars. (0 < N <= 1000000)

The following N lines each contains two integers x and y (-10^9 <= x, y <= 10^9), means the coordinates of the stars. All the coordinates are different.

Output

One line, the smallest area.

Sample Input
Copy sample input to clipboard
41 11 -1-1 -1-1 1
Sample Output
4
Hint

You should use scanf, printf, and long long. 

flat plane: 平面

coordinate: 坐标

rectangle: 矩形

paralleled to the coordinate axes: 平行于坐标轴

Problem Source: lby

// Problem#: 12013
// Submission#: 3379289
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
    long long  x[1000000];
    long long  y[1000000];
int main()
{
    int n;
    cin>>n;

    for(int i=0;i<n;i++)
    {
        cin>>x[i]>>y[i];
    }
    sort(x,x+n);
    sort(y,y+n);
    long long r=(x[n-1]-x[0])*(y[n-1]-y[0]);
    cout<<r<<endl;
}                                

注意:大数组开在main外,否则runtime error.

           用long long 和__int64输入大数字,printf("%lld");

         

runtime  error (运行时错误)就是程序运行到一半,程序就崩溃了。

比如说:

①除以零

②数组越界:int a[3]; a[10000000]=10;

③指针越界:int * p; p=(int *)malloc(5 * sizeof(int)); *(p+1000000)=10;

④使用已经释放的空间:int * p; p=(int *)malloc(5 * sizeof(int));free(p); *p=10;

⑤数组开得太大,超出了栈的范围,造成栈溢出:int a[100000000];

有2种情况  第一种是数组越界了,具体为runtime error sigsegv  这个就就要自己去检查了,因为数组越界,编译无法识别错误的,第二种就是除数为0  具体为runtime error sigfpe 如果你那里有除数,应该考虑到除数为0的情况,这种情况一般是因为循环那里出错的,检查一下
对于定义部分:           是不是VC++上用_int64定义这种类型,在dev C中用long long定义而对于输入输出部分:在windows中用 "%I64d" 格式输入输出,而在linux中用 "%lld" 格式输入输出



解决time limit问题:

#include<iostream>
using namespace std;
long long  a[1000001];
int main()
{
    int   n,m;
    cin>>n>>m;
    long long  add=0;
    int sign=1;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        a[i]=a[i]+a[i-1];
    }
    for(int i=0;i<m;i++)
    {
        int op;
        cin>>op;
        if(op==1)
        {
            sign*=-1;
            add*=-1;
        }
        else if(op==2)
        {
            int  x;
            cin>>x;
            add+=x;
        }
        else if(op==3)
        {
            int l,r;
            long long  sum=0;
            cin>>l>>r;
            //cout<<add<<" "<<sign<<endl;
            cout<<((a[r-1]-a[l-2])*sign+add*(r-l+1))<<endl;
        }
    }
}


定义sign add,避免每次操作都遍历数组,适用于对数组进行同样操作的情况。


大数字表示,数位进位:

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[100];
    a[0]=1;
    int carry=0;
    int temp;
    int k=1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<k;j++)
        {
            temp=a[j]*3+carry;//carry表示进位
            a[j]=temp%10;
            carry=temp/10;
        }
        if(carry!=0)
        a[k++]=carry;
        carry=0;
    }
    //cout<<k<<endl;
    for(int i=k-1;i>=0;i--)
    cout<<a[i];
    cout<<endl;
}


字符填充:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int n,m;
    cin>>n;
    int a=1;
    for(int i=0;i<n;i++)
    {
        if(i==n-1)
        cout<<setfill('*')<<setw(i+1)<<"*"<<endl;//setfill表示除输出外剩余所有都是*,
        else
        cout<<setfill('+')<<setw(n-i-1)<<"+"<<setfill('*')<<setw(i+1)<<"*"<<endl;
    }
    /*cout<<setfill('*')
<<setw(2)<<21<<endl;
cout<<setw(3)<<21<<setw(3)<<" "<<endl;
cout<<setw(4)<<21<<endl;
cout<<"setfill('')"<<endl;
return 0;*/
}

0 0
原创粉丝点击