顺序表ADT模板设计及简单应用:找匹配

时间限制: 1S类别: DS:线性表->线性表应用

问题描述:

输入范例:

100000
100000 99999 99998 99997 99996 99995 99994 99993 99992 99991 99990 99989 99988 99987 99986 99985 99984 99983 99982 99981 99980 99979 99978 99977 99976 99975 99974 99973 99972 99971 99970 99969 99968 99967 99966 99965 99964 99963 99962 99961 99960 99959 99958 99957 99956 99955 99954 99953 99952 99951 99950 99949 99948 99947 99946 99945 99944 99943 99942 99941 99940 99939 99938 99937 99936 99935 99934 99933 99932 99931 99930 99929 99928 99927 99926 99925 99924 99923 99922 99921 99920 99919 99918 99917 99916 99915 99914 99913 99912 99911 99910 99909 99908 99907 99906 99905 99904 99903 99902 99901 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

输出范例:

100000,99999,99998,99997,99996,99995,99994,99993,99992,99991,99990,99989,99988,99987,99986,99985,99984,99983,99982,99981,99980,99979,99978,99977,99976,99975,99974,99973,99972,99971,99970,99969,99968,99967,99966,99965,99964,99963,99962,99961,99960,99959,99958,99957,99956,99955,99954,99953,99952,99951,99950,99949,99948,99947,99946,99945,99944,99943,99942,99941,99940,99939,99938,99937,99936,99935,99934,99933,99932,99931,99930,99929,99928,99927,99926,99925,99924,99923,99922,99921,99920,99919,99918,99917,99916,99915,99914,99913,99912,99911,99910,99909,99908,99907,99906,99905,99904,99903,99902,99901,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149

51

从题目可以观察出来这里用的算法是:哈希表

我的代码如下:

 1 //序表ADT模板设计及简单应用:找匹配
2 #include<iostream>
3 #include<cstring>
4 #include<sstream>
5 #include<vector>
6 #include<cstdlib>//atoi!
7 using namespace std;
8 template<class T>
9 int getCount(vector<T>& A, int N)
10 {
11 int ans = 0;
12 //哈希表法
13 vector<int>hash(N);
14 hash.assign(N + 1, 0);//先预留N+1的空间
15 typename vector<T>::iterator it = A.begin();//制造遍历迭代器
16 while (it != A.end())
17 {
18 hash.at(*it) = 1;//标记为1
19 it++;
20 }
21 it = A.begin();
22 while (it != A.end())
23 {
24 int temp = (int)*it;//要强制转换
25 hash.at(temp) = 0;//防止出现50+50=100的情况!
26 if (hash.at(N + 1 - temp) == 1)
27 {
28 ans+=1;
29
30 }
31 it++;
32 }
33
34 return ans;
35 }
36 int main()
37 {
38 vector<string>a;
39 vector<int>b;
40 int n;
41 cin >> n;
42 cin.get();//吸收一下回车
43
44 //从字符串中提取数 插入到vector b中
45 string str;
46 getline(cin, str);
47 stringstream room;
48 room.str(str);
49 string temp;
50 while (room >> temp)
51 {
52 a.push_back(temp);
53 }
54 for (int i = 0; i < a.size(); ++i)
55 {
56 int ttemp;
57 ttemp = atoi(a[i].c_str());
58 b.push_back(ttemp);
59 }
60
61 //遍历输出
62 for (int i = 0; i < b.size() - 1; ++i)
63 {
64 cout << b[i] << ",";
65 }
66 cout << b[b.size() - 1];
67 cout << endl<<endl;
68 int ans = getCount(b, n);
69 cout << ans;
70 return 0;
71 }

数据结构:DHU顺序表ADT模板设计及简单应用:找匹配的更多相关文章

  1. hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)

    基础数据结构——顺序表(2) Time Limit: 1000 MS    Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...

  2. C++顺序表(模板总结)

    C++顺序表(模板总结) 总结: 1.模板类的实质是什么:让程序员写出和类型无关的代码 2.模板的对象时什么:方法或者类 3.是对类中的一系列操作,提供一个不固定数据类型的方法 用模板做的类的时候要指 ...

  3. [Python] 数据结构--实现顺序表、链表、栈和队列

    说明: 本文主要展示Python实现的几种常用数据结构:顺序表.链表.栈和队列. 附有实现代码. 来源主要参考网络文章. 一.顺序表 1.顺序表的结构 一个顺序表的完整信息包括两部分,一部分是表中元素 ...

  4. C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用

    摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解.即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第一篇,主要针对线性表中的顺序表(动 ...

  5. [数据结构]C#顺序表的实现

    在数据结构的学习当中,想必C++可能是大家接触最多的入门语言了 但是C#的数据结构却很少看到,今天我写了一个C#顺序表的顺序存储结构 顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是 ...

  6. 【PHP数据结构】顺序表(数组)的相关逻辑操作

    在定义好了物理结构,也就是存储结构之后,我们就需要对这个存储结构进行一系列的逻辑操作.在这里,我们就从顺序表入手,因为这个结构非常简单,就是我们最常用的数组.那么针对数组,我们通常都会有哪些操作呢? ...

  7. 数据结构:顺序表(python版)

    顺序表python版的实现(部分功能未实现) #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object): def __ini ...

  8. 顺序表 C++模板实现

    #include <iostream> using namespace std; template <typename T> class list{ private: int ...

  9. 数据结构之顺序表,c#实现

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

随机推荐

  1. 【C#反射】开篇

    微软官方教程:https://docs.microsoft.com/zh-cn/dotnet/framework/reflection-and-codedom/viewing-type-informa ...

  2. Shell脚本编写登陆小程序.sh

    转至:https://www.cnblogs.com/gaohongyu/articles/12072594.html #!/bin/bash #Author:GaoHongYu #QQ:106176 ...

  3. killall 、kill 、pkill 命令区别

    转至:https://zhuanlan.zhihu.com/p/87904563 killall 命令 Linux系统中的killall命令用于杀死指定名字的进程(kill processes by ...

  4. 用RecyclerView实现水平滚动和网格视图

    建立RecyclerViewActivity.java文件 1 public class RecyclerViewActivity extends AppCompatActivity { 2 priv ...

  5. springboot----四、yaml配置注入

    四.yaml配置注入 4.1.配置文件 SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的 application.properties 语法结构 :key=value applic ...

  6. 还不会用springboot写接口?快看这里,手把手操作,一发入魂~

    1.springboot简介 Spring Boot 可以轻松创建可以"直接运行"的独立的.生产级的基于 Spring 的应用程序. 特征 创建独立的 Spring 应用程序 直接 ...

  7. MATLAB绘制一幅中国地图

    今天博主跟大家讲一下如何用MATLAB制作一幅中国地图,那废话不多说,我们先看一下最终效果吧. mercator墨卡托圆柱投影地图 lambert兰伯特圆锥投影地图 一张中国地图大概包括以下要素: 中 ...

  8. 官宣 .NET 7 Preview 2

    今天,我们很高兴发布 .NET 7 预览版 2..NET 7 的第二个预览版包括对 RegEx 源生成器的增强.将 NativeAOT 从实验状态转移到运行时的进展,以及对"dotnet n ...

  9. [apue] linux 文件系统那些事儿

    前言 说到 linux 的文件系统,好多人第一印象是 ext2/ext3/ext4 等具体的文件系统,本文不涉及这些,因为研究具体的文件系统难免会陷入细节,甚至拉大段的源码做分析,反而不能从宏观的角度 ...

  10. 使用 Docker 部署 LNMP 并搭建 wordpress

    准备 系统版本:CentOS Linux release 7.4.1708 (Core)   内核版本:3.10.0-693.el7.x86_64    IP:192.168.31.43    可访问 ...