memset

DP问题(2) : hdu 1421

你。 提交于 2019-12-06 12:47:32
这道题很水 但我还是调了0.25h 感谢wsy大佬的帮助 emmm...... 首先,这肯定是一个n*k的dp 其中,i表示有i个物品,j表示需要搬j回 那么我们可以得出它的转移方程: dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(int)pow((w[i]-w[i-1]),2)); 让后就很简单了 才怪,上面的我10min就想出来了,下面才让我最头疼 初始化 下面是我原来写的初始化: memset(dp,0x7f,sizeof(dp)); 然后各种WA 然后改了一下 (25min) ,如下: for(int i=0;i<=n;i++) for(int j=1;j<=k;j++) dp[i][j]=inf; 然后就AC了!!! 一个memset让我调了0.25h! (好不值) 以下是AC代码: #include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> #include<cmath> #define inf 2147483647 using namespace std; int n,k; int w[2005]; int dp[2005][2005]; int main() { while(scanf("%d%d",&n,&k)!=EOF) { for(int

memset 字符串处理函数

血红的双手。 提交于 2019-12-06 05:27:00
#include <stdio.h> void *memset(void *s, int c, size_t n); 函数使用常量字节 c 填充 s 指向的前 n 个字节。 根据memset函数的不同,输出结果也不同,分为以下几种情况: memset(p, 0, sizeof(p)); //地址的大小都是4字节 0 0 0 0 -52 -52 -52 -52 -52 -52 memset(p, 0, sizeof(*p)); //*p表示的是一个字符变量, 只有一字节 0 -52 -52 -52 -52 -52 -52 -52 -52 -52 memset(p, 0, sizeof(str)); 0 0 0 0 0 0 0 0 0 0 memset(str, 0, sizeof(str)); 0 0 0 0 0 0 0 0 0 0 memset(p, 0, 10); //直接写10也行, 但不专业 0 0 0 0 0 0 0 0 0 0 来源: https://www.cnblogs.com/When6/p/11963914.html

memset// memcpy

蹲街弑〆低调 提交于 2019-12-06 05:13:01
ch只有最低的字节起作用 自己写的memcpy可能存在内存重叠问题,要特别注意 来源: https://www.cnblogs.com/focus-z/p/11963600.html

c++数组初始化赋值

走远了吗. 提交于 2019-12-05 22:56:58
1 #include<iostream> 2 #include<string.h> 3 #include<algorithm> 4 #define max 0x3f 5 using namespace std; 6 int main() 7 { 8 int b[6]; 9 int a[5][5]; 10 fill(a[0],a[0]+5*5,12); 11 // memset(a,0,sizeof(a)); 12 // memset(a,-1,sizeof(a)); 13 // memset(a,max,sizeof(a)); 14 /*fill对二维数组赋初值,要从a[0]开始,而不是a */ 15 /*而memset对二维或者一维数组赋值只能是赋值为0,-1,max(16进制)*/ 16 // memset(b,0,sizeof(b)); 17 // memset(b,-1,sizeof(b)); 18 // memset(b,max,sizeof(b)); 19 fill(b,b+6,9); 20 cout<<"a数组:\n"; 21 for(int i=0;i<5;i++) 22 { 23 for(int j=0;j<5;j++) 24 { 25 cout<<a[i][j]<<" "; 26 } 27 cout<<endl; 28 } 29 cout<<"b数组:\n";

The necessity to memset with '\\0', in a toy example

馋奶兔 提交于 2019-12-05 20:10:12
I encountered the following example of using memset in tutorialspoint : (I am not familiar with C.) #include <stdio.h> #include <string.h> int main(){ char src[40]; char dest[100]; memset(dest, '\0', sizeof(dest)); strcpy(src, "This is tutorialspoint.com"); strcpy(dest, src); printf("Final copied string : %s\n", dest); return(0); } I don't get why the memset line is used, as the compile and result are the same when that line is commented. I would like to ask is that line necessary? or is it a good practice to do so when doing strcpy()? or it is just one random line. Thanks! Keith Thompson It's

UVA - 1599 Ideal Path (BFS)

ⅰ亾dé卋堺 提交于 2019-12-05 15:49:52
传送门 题意 求带边权无向图从起点到终点的边权字典序最小的最短路路径 解法 两遍 BFS 第一遍逆向,给图分层 第二遍求路径,每层的点都只能沿最小的边权走向下一层的点。 代码 #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <queue> using namespace std; typedef long long LL; const int inf=0x3f3f3f3f; const LL INF=0x3f3f3f3f3f3f3f3f; const int MAXN=1e5+10; int n,m; int head[MAXN],nxt[MAXN*4],to[MAXN*4],c[MAXN*4],tot=1; int dis[MAXN],from[MAXN],path[MAXN]; bool vis[MAXN]; queue<int> que; queue<vector<int> > q2; void add(int u,int v,int w){ to[++tot]=v;c[tot]=w;nxt[tot]=head[u];head[u]=tot; to[++tot]=u;c[tot]=w;nxt[tot]=head[v];head[v]=tot; }

C++踩坑——用memset对vector进行初始化

浪子不回头ぞ 提交于 2019-12-05 14:02:32
在一段程序中,使用memset对vector进行了初始化,然后得到了错误的结果。找这个bug花费了很长时间。 vector中有其自身的结构,不能单纯的按字节进行初始化。使用memset对vector进行初始化,会破坏vector中的结构,造成vector中的数据错误。我使用memset将vector中元素全部置为1,最终发现结果相差甚远。 来源: https://www.cnblogs.com/Peyton-Li/p/11926566.html

Linux socket program Demo1(client & server)

瘦欲@ 提交于 2019-12-05 07:21:33
client and server Demo of socket. client send data to server. server send data to client. // this is client #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #define BUFF_SIZE 1024 int main(int argc, char* argv[]) { if(argc<=2) { printf("usage:%s ip_address port_number \r\n", basename(argv[0])); return 1; } char* ip = argv[1]; int port = atoi(argv[2]); struct sockaddr_in address; bzero(&address, sizeof(address)); address.sin_family = AF_INET; address.sin

Initializing a float array with memset

徘徊边缘 提交于 2019-12-05 01:21:41
This is the code that I want to try to write: #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <malloc.h> int main(int argc, char *argv[]) { float arry[3] = {0}; memset(arry, (int) 10.0, 3*sizeof(float)); return 0; } My problem is that I want to see if it's possible to use memset to make every entry of an array to be a number other than 0. However, After stepping through that line, the array contents change to a very small number (0). I wonder what I'm doing wrong in this case with using the memset() function. I hope this isn't a duplicate post, as none of

Can memset() be called with a null pointer if the size is 0?

回眸只為那壹抹淺笑 提交于 2019-12-04 23:12:47
For one reason or another, I want to hand-roll a zeroing version of malloc() . To minimize algorithmic complexity, I want to write: void * my_calloc(size_t size) { return memset(malloc(size), 0, size); } Is this well-defined when size == 0 ? It is fine to call malloc() with a zero size, but that allows it to return a null pointer. Will the subsequent invocation of memset be OK, or is this undefined behaviour and I need to add a conditional if (size) ? I would very much want to avoid redundant conditional checks! Assume for the moment that malloc() doesn't fail. In reality there'll be a hand