第二周集训vjudge总结

第二周vjudge

1.初始化函数【C/C++】

(1) memset是计算机中C/C++语言初始化函数。作用是将某一块内存中的内容全部设置为指定的值, 这个函数通常为新申请的内存做初始化工作。
(2) 函数原型: extern void *memset(void *buffer, int c, int count)

buffer: 为指针或是数组
c: 是赋给buffer的值
count: 是buffer的长度
(3)C中: #include <string.h>
C++中: #include <cstring.h>
(4)要点: memset可以方便快速的清空一个结构类型的变量或数组。
例1. 用在对定义的字符串进行初始化为‘ ’或‘/0’;

char a[100];
memset(a,/0, sizeof(a));

例2. 用在对定义的数字数组进行初始化为0或-1等;

int a[100];
memset(a, 0, sizeof(a));
2.比较函数

在用C写题时,涉及比较大小寻找最大最小值时,可以借用C++的min/max函数。
注:代码前应加上:

#include<algorithm>
using namespace std;
3.排序函数

C语言中排序需要敲出排序部分(冒泡,选择,插入等),但因容易书写的算法效率并不高(时间复杂度有点高)效率高的排序算法写起来又太复杂。因而在写C中若涉及排序问题,可以调用C++的sort函数。
(1)sort()函数给整型数组排序
sort(一个地址A(从哪一个开始排序),(好好思考这个位置)一个地址B(你想要停止排序的那个元素的后一位地址),一个控制排序规则的函数);好像有点抽象,简单来说,就是从A到B-1的所有元素参与排序,如果是升序,那么排序规则函数不用写,如果是降序或者是其他自定义排序,请看后文cmp函数书写。

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int a[11]={0,1,2,13,45,2,33,1,6,78,4};
    sort(a,a+11);
    for(int i=0;i<11;i++)
    {
        printf("%d\n", a[i]);
    }
    return 0;
}

特别注意的是若从a[0]-a[10]排序,写的是sort(a,a+11)。

(2)其他的排序方式
如果不写排序规则函数,sort函数默认从小到大排序,现在我们想从大到小排序怎么办?

#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int a[11]={0,1,2,13,45,2,33,1,6,78,4};
    sort(a,a+11,cmp);
    for(int i=0;i<11;i++)
    {
        printf("%d\n", a[i]);
    }
    return 0;
}

只需要加上:

bool cmp(int a,int b)
{
    return a>b;
}

并且在调用sort的时候第三个参数写cmp就行了。如sort(a,a+11,cmp)是给a[0]到a[10]排序。

(3)string类也可以用sort排序

#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
bool cmp(string a,string b)
{
    return a>b;
}
int main()
{
    string a[4]={"hhhhh","heheheh","xxxxxx","kkkkkk"};
    sort(a,a+4,cmp);
    for(int i=0;i<4;i++)
        printf("%d\n", a[i]);
    return 0;
}

(4)以结构体为例的高级排序
以题目为例讲解:
在这里插入图片描述在这里插入图片描述代码实现如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
struct goods
{
	int value;
	int day;
}a[100005];

struct goods temp;
int vis[100005];

bool cmp(struct goods a, struct goods b)
{
	if(a.value == b.value)
		return a.day > b.day;
	else
		return a.value > b.value;
}

using namespace std;
int main()
{
	int n, i, j, t, ans;

	while(~scanf("%d", &n))
	{
		for(i=0;i<100005;i++)
		{
			vis[i]=0;
		}
		for(i = 0; i < n; i++)
		{
			scanf("%d %d", &a[i].value, &a[i].day);
		}		
		
		sort(a, a+n, cmp);
		
		ans = 0;
		for(i = 0; i < n; i++)
		{
			if(vis[a[i].day] == 0)
			{
				vis[a[i].day] =1;
				ans += a[i].value;
			}		
			else
			{
                for(j=a[i].day; j>=1;j--)
                {
                    if(!vis[j])
                    {
                        vis[j]=1;
                        ans+=a[i].value;
                        break;
                    }
                }
            }	
		}			
			
	printf("%d\n", ans);	
	}
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值