memset初始化int数组
memset(a1,0,sizeof(a1));//数组初始化为0 memset(a1,127,sizeof(a1));//数组初始化为一个很大且接近int类型上限的正数 memset(a1,128,sizeof(a1));//数组初始化为很小且接近int类型下限的负数 memset(a1,-1,sizeof(a1));//数组初始化为-1 memset(a1,255,sizeof(a1));//数组初始化为-1
---------------------------------------------------------------------------------------------------------------------------------------------------
我在我电脑上实验memset对int数组初始化其它值发现和我要的结果不一致,于是百度一下
https://www.it1352.com/348968.html
memset的设置每个目标缓冲区到指定值的字节。在您的系统, INT 是四个字节,每个调用 memset的后是5。因此,电网[0] 值为 0x05050505 (十六进制),这是 84215045 < / code>十进制。
一些平台提供可供选择的API来 memset的的写更宽模式的目标缓冲区;例如,在OS X或iOS,你可以使用:
INT模式= 5;
memset_pattern4(网格和放大器;格局,sizeof的网格);
让你似乎期望的行为。你瞄准什么平台?
在C ++中,你应该只使用的std :: fill_n :
的std :: fill_n(网格,100,5);
---------------------------------------------------------------------------------------------------------------------------------------------------
一开始代码中是不含有#include <string.h>,后面百度memset在此作用域尚未声明,将头文件 #include <string> 改为 #include <string.h>
另附题目链接 https://www.luogu.com.cn/problem/P1618
#include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #include <string.h> using namespace std; int ha[10]; void check1(int t) { ha[t / 100] = 1; ha[t / 10 % 10] = 1; ha[t % 10]=1; } bool check(int x, int y, int z) { /*for (int i = 1; i <= 9; i++) ha[i] = 0;*/ memset(ha, 0, sizeof(ha)); if (y > 999|| z>999) return 0; check1(x); check1(y); check1(z); for (int i = 1; i <= 9; i++) { if (ha[i] == 0) return 0; } return 1; } int main() { int A, B, C; int x, y, z; int sum = 0; cin >> A >> B >> C; for (int x = 123; x <= 987; x++) { if (x * B % A || x * C % A) continue; y = x * B / A; z = x * C / A; if (check(x, y, z)) { cout << x<<" " << y << " " << z << endl; sum++; } } if (sum == 0) cout << "No!!!" << endl; return 0; }
本文来自博客园,作者:magicat,转载请注明原文链接:https://www.cnblogs.com/magicat/p/15061019.html
浙公网安备 33010602011771号