c++大一期中考
来源:互联网 发布:seo推广网站 编辑:程序博客网 时间:2024/04/27 14:48
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.
One line, the smallest area.
41 11 -1-1 -1-1 1
4
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;*/
}
- c++大一期中考
- [C] ACCP一期大作业
- 中考
- 名师指导:中考物理三大“法宝”
- 2015智锋互联C++/C语言一期培训视频课程
- 中考记
- 中考RP++
- 中考誓师大会
- 一期一会
- 中考相关网站
- 又在中考......
- 中考在考谁?
- 中考满分作文
- 大连市中考数学试题分析
- 中考信息考完了
- 下个星期四中考
- 中考挺紧张的
- 中考期间在餐厅
- 在java中sax读取远程xml
- 二分图最小路径覆盖——POJ 3020
- 东方智软,成长型的创业公司
- Samba服务器的配置
- Beginning Auto Layout Tutorial in iOS 7: Part 1
- c++大一期中考
- gradle教程
- 将博客搬至CSDN
- Labyrinth(枚举+DP)
- 关于appium之初体验
- 开发中遇到的问题小记
- 数据结构实验之链表四:有序链表的归并
- 判断出栈顺序是否正确
- NSSet 和 NSMutableSet 定义和基本用法