-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61_Is_Continuous.cpp
More file actions
91 lines (67 loc) · 1.68 KB
/
61_Is_Continuous.cpp
File metadata and controls
91 lines (67 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
//
// Created by mark on 2019/7/28.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:61. 扑克牌的顺序。从扑克牌中抽取5张牌,判断是不是顺子 。
2. 思路: 1. 排序数组
2. 统计0的个数
3. 统计相邻数字之间的空缺总数
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <assert.h>
#include <cstdio>
#include <fstream>
#include <map>
#include <set>
#include <deque>
#include <algorithm>
using namespace std;
bool isContinuous(vector<int> &nums)
{
if(nums.size() == 0)
return false;
sort(nums.begin(), nums.end());
int number_zero = 0;
int number_gap = 0;
// 统计0的个数
for(int i = 0; i < nums.size() && nums[i] == 0; i++)
number_zero++;
// 统计间隔数目
int small = number_zero; // 从0的下一位开始统计
int big = small + 1;
while(big < nums.size())
{
if(nums[small] == nums[big])
return false;
number_gap += nums[big] - nums[small] - 1;
small = big;
big++;
}
return number_gap > number_zero ? false : true;
}
int main()
{
vector<int> nums;
cout << "输入5张牌:";
for(int i = 0; i < 5; i++)
{
int num;
cin >> num;
nums.push_back(num);
}
for(vector<int>::iterator iter = nums.begin(); iter != nums.end(); iter++)
cout << *iter << " ";
cout << endl;
if(isContinuous(nums))
cout << "是顺子!" << endl;
else
cout << "不是顺子!" << endl;
return 0;
}