B1011
- long long的最大值:9223372036854775807
- long long的最小值:-9223372036854775808
- unsigned long long的最大值:18446744073709551615 //20位
如果使用long long 再用printf 要用%lld
B1026
- 用long long 还要用%02d
B1046 划拳
A1025 排序
-
vector<Student> add_all_rank(vector<Student> &s)//给所有人排序
- 在函数体内对vector的对象(student是结构体)进行参数修改 必须要在传参的时候进行&引用
-
sort(a,a+5,cmp);
- 符合左闭右开
- 前两个参数必须是地址
-
bool cmp(Student a, Student b)
- 定义cmp的时候 对什么类型的对象比较就用什么类型的对象传参 这里是student
-
定义结构体对象
- 和类对象一样的定义方法
-
给函数传递数组
- void myFunction(int *param){…}
- void myFunction(int param[10]){…}
- void myFunction(int param[]){…}
-
返回数组类型的参数
- int * myFunction(){…}
- 另外,要注意不能返回局部变量的地址,除非定义局部变量为static变量。
int * getRandom( )
{
static int r[10];
srand( (unsigned)time( NULL ) );
for (int i = 0; i < 10; ++i)
{
r[i] = rand();
cout << r[i] << endl;
}
return r;
}
int main()
{
int *p;
p = getRandom();
//此时,p就指向了r,可以操作数组r了(注意r一定是全局的才行)。
}
vector<student>
取第i个元素student[i].id
vector<student>
取倒数第二个个元素student[student.size()-2]
size()*(student.end()-2)
end()
A1062/B1015 结构体排序
- 合理剪枝很重要 尤其是if else
-
删除vector中的特定元素
-
C++ vector中实际删除元素使用的是容器vecrot中std::vector::erase()方法。
-
C++ 中std::remove()并不删除元素,因为容器的size()没有变化,只是元素的替换。
-
iterator erase (iterator position); //删除指定元素
-
iterator erase (iterator first, iterator last); //删除指定范围内的元素
-
返回值:
- 指向删除元素(或范围)的下一个元素。
//这个步骤很费时间 会超时
vector<Student>::iterator it; // bug:得用Student 不能用int 因为迭代的是student
for (it = s.begin(); it != s.end();)
{
if ((*it).flag==5) { //删除值为5的元素
it = s.erase(it); //这种写法
}
else {
++it;
}
}
/*
erease
*/
- 输入输出的测试文件比较大的时候,(scanf和printf)比(cin/cout)能节省不少时间
B1031 15
- 把大写X写成了小写x
- 传递
string s
;要用getline(cin,s);
- 在字符数组中存储数字必须打上单引号
char str[11] = { '1','0','X','9','8','7','6','5','4','3','2' };