• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
magicat
博客园    首页    新随笔    联系   管理    订阅  订阅
memset在此作用域尚未声明,初始化int数组和对其他值初始化错误,洛谷P1618 三连击(升级版)

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

posted on 2021-07-26 13:41  magicat  阅读(469)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3