数组三(巧用数组下标)
一.数组作计数器
1.票数统计
2.求一批整数中出现最多的个位数字
3.输出GPTL
4.数据排序(桶排序)
二.数组作标记
1.整数去重
2.小乐乐与排序
3.校门外的树
4.阅览室
5.念数字(哈希存储)
6.查验身份证
7.水仙花数
#include <bits/stdc++.h>
using namespace std;
int main() {
int a;
int s[2000] = {};//使用数组千万千万要记得初始化
while (cin >> a)
s[a]++;
for (int i = 1; i <= 10; i++) {
if (i != 1)
cout << " ";
cout << s[i];
}
return 0;
}
★ ★ ★
数组解法:
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, x, m;
int s[100000] = {};//定义一个int型的数组
cin >> a;
for (int i = 0; i < a; i++) {
cin >> x;
do {//由题意知输入的为非负整数即可能为0,因此不用while循环
m = x % 10;
s[m]++;// 把数组下标和统计对象联系在一起,将数组用作计数器
x /= 10;
}while(x);
}
int ma = 0;
for (int i = 0; i < 10; i++)
ma = max(ma, s[i]);//if(s[i]>ma)ma=a[i];
cout << ma << ":";
for (int i = 0; i <= 9; i++) {
if (s[i] == ma)
cout << " " << i ;//输出次数出现最多的几个数
}
return 0;
}
字符串解法:
#include <bits/stdc++.h>
using namespace std;
int main() {
int a;
int c[10000] = {};
string s;
cin >> a;
getchar();
getline(cin, s); //cin输入时不能跳过空字符
for (int i = 0; i < s.size(); i++)
c[s[i]]++;//扫描并计录每个字符出现的次数
int ma = 0;
for (char i = '0'; i <= '9'; i++) { //s是字符串,s[i]是数字字符
ma = max(ma, c[i]);//c[i]中i是字符变量,c[i]是整型
}
cout << ma << ":";
for (char i = '0'; i <= '9'; i++) {
if (ma == c[i])
cout << " " << i;
}
return 0;
}
★★★★★
#include <bits/stdc++.h>
using namespace std;
int main() {
int sum ;
string s;
cin >> s;
int c[256] = {};
for (int i = 0; i < s.size(); i++) {
c[toupper(s[i])]++;//toupper转换大写字符
}
char str[] = "GPLT";//字符数组
do {
//int sum=0;不能这样写,因为这样相当于重新定义了一个sum
//而该sum只作用于循环体内,与while里的sum不是同一个,从而失去了原来的意义
sum = 0;
for (int i = 0; i < 4; i++) {
if (c[str[i]])//GPTL每个字符的个数
c[str[i]]--,
cout << str[i];
sum += c[str[i]];
}
} while (sum);
return 0;
}
★ ★ ★
#include <bits/stdc++.h>
using namespace std;
int f[2000];//放在主函数外自动初始化
int main() {
int n, x;
//int f[2000]={};
cin >> n;
while (n--) {
cin >> x;
f[x]++;
}
int c = 0; //是否输出空格的关键
for (int i = 0; i <= 1000; i++) {
while (f[i]--) {
if (c)
cout << " ";
c = 1;
cout << i;
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int f[10000];//数组定义在主函数外,自动初始化
int main() {
int n, x;
cin >> n;
// while(n--)这样写无法控制空格的输出
// {
// cin>>x;
// if(f[x]!=1)cout<<x;
// f[x]=1;
// }
for (int i = 0; i < n; i++) {
cin >> x;
if (f[x] != 1) {//f[x]=1,表示i输出过,f[x]=0否则未输出过
if (i)
cout << " ";
cout << x;
}
f[x] = 1;//把数组下标和处理的对象联系在一起,将数组用作标记
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int f[1000000];
int main() {
int n, x;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
f[x] = 1;
}
int c = 0;
for (int i = 0; i <= 100000; i++) {
if (f[i] == 1) {
if (c != 0)
cout << " ";
cout << i;
c = 1;
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int f[10010];
int main() {
int m, n, a, b, sum = 0;
cin >> m >> n;
while (n--) {
cin >> a >> b;
for (int i = a; i <= b; i++) {
f[i] = 1;//f[i]=1表示树被挖走
}
}
for (int i = 0; i <= m; i++) {
if (f[i] != 1)
sum++;
}
cout << sum ;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int f[2000];
int t[2000];
int main()
{
int n,a,h,m;
char c,d;
cin>>n;
while(n--)
{
int sum=0,k=0;
while(cin>>a>>c>>h>>d>>m&&a)
{
if(c=='S')
{
t[a]=h*60+m;
f[a]=1;//标志已经借过
}
else
{
if(f[a]==1){
sum+=h*60+m-t[a];
k++;
f[a]=0;//!!!重要且易忽略的一点借完书还了之后还可能再借,因此要重置为0
}
}
}
if(k)
cout<<k<<" "<<int(sum*1.0/k+0.5)<<endl;
else cout<<0<<" "<<0<<endl;
}
return 0;
}
⭐⭐⭐哈希存储法
#include <bits/stdc++.h>
using namespace std;
int main() {
string a[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
string s;
cin >> s;
int i = 0;
if (s[i] == '-') {//先判断负数情况很重要!i的用法有技巧也很重要!
cout <<"fu";
i++;
}
while (i < s.size()) {
if (i)
cout << " ";
cout << a[s[i] - '0'];//s[i]为字符需要-'0'转换为数字
i++;
}
return 0;
}
#include <stdio.h>
int main() {
int N, j;
int i = 1;
int ch[100];
scanf("%d", &N);
if (N < 0) {
printf("fu ");
N = -N;
} else if (N == 0) {
printf("ling");
}
while (N > 0) {
ch[i] = N % 10;
N = N / 10;
i++;
}
for (int j = i - 1; j > 0; j--) {
switch (ch[j]) {
case 1:
printf("yi");
break;
case 2:
printf("er");
break;
case 3:
printf("san");
break;
case 4:
printf("si");
break;
case 5:
printf("wu");
break;
case 6:
printf("liu");
break;
case 7:
printf("qi");
break;
case 8:
printf("ba");
break;
case 9:
printf("jiu");
break;
case 0:
printf("ling");
break;
}
if (j != 1) {
printf(" ");
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, c = 0, f = 0, sum = 0, d;
int a[18] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char m[] = "10x98765432";
cin >> n;
string s;
while (n--) {
sum = 0;
c = 0;
cin >> s;
for (int i = 0; i < s.size() - 1; i++) {
if (!isdigit(s[i]))
c = 1;
sum += (s[i] - '0') * a[i];
}
d = sum % 11;
if (c == 1 || m[d] != s.back()) {
f++;
cout << s<<endl;
}
}
if (f == 0)
cout << "All passed"<<endl;
return 0;
}
⭐⭐⭐查表法
#include <bits/stdc++.h>
using namespace std;
int a[11];
int main() {
int n, k, sum = 0;
cin >> n;
for (int i = 1; i <= 10; i++) {//相当于求了i的n次方并依次储存在对应的下标中
a[i] = 1;
for (int j = 1; j <= n; j++)
a[i] *= i;
}
for (int i = a[10] / 10; i < a[10]; i++) {
k = i;
sum = 0;
while (k) {
sum += a[k % 10];
k /= 10;
}
if (sum == i)
cout << i << endl;
}
return 0;
}