暑假第一天 桶和map练习
今日笔记
/**
桶的作用:记录某些数是否出现过 或 统计某些数过的次数
int tong[100000] = {0};
使用
tong[x] ++
tong[x] = ...
缺点:
1.桶的范围有限
100000000
2.只能标记整数(字符串 和 小数 和 很大的整数都不行)
tong["abc"] ++
tong[3.14] ++;
map<下标(字符串、整数、小数), 数据类型> tong;
map的具体功能
//使用
tong[元素] ++;
//查询
tong[元素]
//获得个数
tong.size();
*/
今日练习
桶的练习:
B3701避雷针
P1152 欢乐的跳
B4555 加密
T759192 字符串魔法
map练习:
P11227 扑克牌
T668576 A-B 数对
P1918 保龄球
T648769 词频统计
--------------------------------------------
第一题: B3701 避雷针(无问题)
第二题 P1152 欢乐的跳(错误)
错误原因:
题目求如果数组两个连续元素之间差的绝对值包括了 [1,n−1] 之间的所有整数, 我没有理解 连续元素 和 [1~n-1]的范围导致错误
错误代码:
#include<bits/stdc++.h>
using namespace std;
int a[2000],st[2000];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=2;i<=n;i++)
{
st[a[i]]=abs(a[i-1]-a[i]); //错误
}
for(int i=1;i<=n;i++) //错误
{
if(st[i]==0)
{
cout<<"Not jolly";
return 0;
}
}
cout<<"Jolly";
return 0;
}
正确代码:
#include <bits/stdc++.h>
using namespace std;
int a[1010], s[10000010];
int n, x;
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
}
//标记相邻元素
for(int i = 1; i < n; i ++)
{
int k = abs(a[i] - a[i + 1]);
s[k] ++;
}
//判断条件[1,n-1] 看每个数有没有出现
for(int i = 1; i < n; i ++)
{
if(s[i] == 0)
{
cout << "Not jolly";
return 0;
}
}
cout << "Jolly";
return 0;
}
第三题 B4555 加密(....)
.....
.....