顺序表
STL–顺序表的使用
# include <iostream>
# include <vector>
using namespace std;
int main ( ) {
vector< int > list;
return 0 ;
}
杭州电子科技大学在线评测
杭州电子科技大学在线评测官网
2008 数值统计
# include <iostream>
using namespace std;
int main ( )
{
int n;
while ( cin >> n) {
if ( n == 0 ) {
break ;
}
int negative = 0 ;
int zero = 0 ;
int positive = 0 ;
for ( int i = 1 ; i <= n; i++ ) {
double num;
cin >> num;
if ( num < 0 ) {
negative++ ;
}
if ( num == 0 ) {
zero++ ;
}
if ( num > 0 ) {
positive++ ;
}
}
cout << negative << ' ' << zero << ' ' << positive << endl;
}
}
使用顺序表实现
# include <iostream>
using namespace std;
# define eleType double
struct SequentialList {
eleType * elements;
int size;
int capacity;
} ;
void initializeList ( SequentialList* list, int capacity) {
list-> elements = new eleType[ capacity] ;
list-> size = 0 ;
list-> capacity = capacity;
}
void destroyList ( SequentialList* list) {
delete [ ] list-> elements;
}
eleType size ( SequentialList* list) {
return list-> size;
}
eleType isEmpty ( SequentialList* list) {
return list-> size == 0 ;
}
eleType insert ( SequentialList* list, int index, eleType element) {
if ( index < 0 || index > list-> size) {
throw std:: invalid_argument ( "Invalid index" ) ;
}
if ( list-> size == list-> capacity) {
int newCapacity = list-> capacity * 2 ;
eleType* newElements = new eleType[ newCapacity] ;
for ( int i = 0 ; i < list-> size; ++ i) {
newElements[ i] = list-> elements[ i] ;
}
delete [ ] list-> elements;
list-> elements = newElements;
list-> capacity = newCapacity;
}
for ( int i = list-> size; i > index; i-- ) {
list-> elements[ i] = list-> elements[ i - 1 ] ;
}
list-> elements[ index] = element;
list-> size++ ;
}
void deleteElement ( SequentialList* list, int index) {
if ( index < 0 || index >= list-> size) {
throw std:: invalid_argument ( "Invalid index" ) ;
}
for ( int i = index; i < list-> size - 1 ; ++ i) {
list-> elements[ i] = list-> elements[ i + 1 ] ;
}
list-> size-- ;
}
eleType findElement ( SequentialList* list, eleType element) {
for ( int i = 0 ; i < list-> size; ++ i) {
if ( list-> elements[ i] == element) {
return i;
}
}
return - 1 ;
}
eleType getElement ( SequentialList* list, int index) {
if ( index < 0 || index >= list-> size) {
throw std:: invalid_argument ( "Invalid index" ) ;
}
return list-> elements[ index] ;
}
void updateElement ( SequentialList* list, int index, eleType value) {
if ( index < 0 || index >= list-> size) {
throw std:: invalid_argument ( "Invalid index" ) ;
}
list-> elements[ index] = value;
}
int main ( )
{
int n;
while ( cin >> n && n) {
SequentialList s;
initializeList ( & s, 1 ) ;
for ( int i = 0 ; i < n; i++ ) {
eleType x;
cin >> x;
insert ( & s, i, x) ;
}
int pcnt = 0 , zcnt = 0 , ncnt = 0 ;
for ( int i = 0 ; i < size ( & s) ; i++ ) {
eleType element = getElement ( & s, i) ;
if ( element > 1e-8 ) {
pcnt++ ;
}
else if ( element < - 1e-8 ) {
ncnt++ ;
}
else {
zcnt++ ;
}
}
cout << ncnt << ' ' << zcnt << ' ' << pcnt << endl;
}
}
2014 青年歌手大奖赛_评委会打分
# include <iostream>
using namespace std;
# define eleType double
struct SequentialList {
eleType* elements;
int size;
int capacity;
} ;
void initializeList ( SequentialList* list, int capacity) {
list-> elements = new eleType[ capacity] ;
list-> size = 0 ;
list-> capacity = capacity;
}
void destroyList ( SequentialList* list) {
delete [ ] list-> elements;
}
int size ( SequentialList* list) {
return list-> size;
}
eleType isEmpty ( SequentialList* list) {
return list-> size == 0 ;
}
void insert ( SequentialList* list, int index, eleType element) {
if ( index < 0 || index > list-> size) {
throw std:: invalid_argument ( "Invalid index" ) ;
}
if ( list-> size == list-> capacity) {
int newCapacity = list-> capacity * 2 ;
eleType* newElements = new eleType[ newCapacity] ;
for ( int i = 0 ; i < list-> size; ++ i) {
newElements[ i] = list-> elements[ i] ;
}
delete [ ] list-> elements;
list-> elements = newElements;
list-> capacity = newCapacity;
}
for ( int i = list-> size; i > index; i-- ) {
list-> elements[ i] = list-> elements[ i - 1 ] ;
}
list-> elements[ index] = element;
list-> size++ ;
}
void deleteElement ( SequentialList* list, int index) {
if ( index < 0 || index >= list-> size) {
throw std:: invalid_argument ( "Invalid index" ) ;
}
for ( int i = index; i < list-> size - 1 ; ++ i) {
list-> elements[ i] = list-> elements[ i + 1 ] ;
}
list-> size-- ;
}
eleType findElement ( SequentialList* list, eleType element) {
for ( int i = 0 ; i < list-> size; ++ i) {
if ( list-> elements[ i] == element) {
return i;
}
}
return - 1 ;
}
eleType getElement ( SequentialList* list, int index) {
if ( index < 0 || index >= list-> size) {
throw std:: invalid_argument ( "Invalid index" ) ;
}
return list-> elements[ index] ;
}
void updateElement ( SequentialList* list, int index, eleType value) {
if ( index < 0 || index >= list-> size) {
throw std:: invalid_argument ( "Invalid index" ) ;
}
list-> elements[ index] = value;
}
int main ( )
{
int n;
while ( cin >> n) {
SequentialList s;
initializeList ( & s, n) ;
eleType max_score = - 100000000 ;
eleType min_score = 100000000 ;
eleType sum = 0 ;
for ( int i = 0 ; i < n; i++ ) {
eleType num;
cin >> num;
insert ( & s, i, num) ;
}
for ( int i = 0 ; i < size ( & s) ; i++ ) {
if ( getElement ( & s, i) > max_score) {
max_score = getElement ( & s, i) ;
}
if ( getElement ( & s, i) < min_score) {
min_score = getElement ( & s, i) ;
}
sum += getElement ( & s, i) ;
}
sum -= max_score;
sum -= min_score;
printf ( "%.2f\n" , sum / ( n - 2 ) ) ;
}
}
Leetcode题目
class Solution {
public :
int game ( vector< int > & guess, vector< int > & answer) {
int count = 0 ;
for ( int i = 0 ; i < 3 ; i++ ) {
if ( guess[ i] == answer[ i] ) {
count++ ;
}
}
return count;
}
} ;
class Solution {
public :
int minCount ( vector< int > & coins) {
int count = 0 ;
for ( int i = 0 ; i < coins. size ( ) ; i++ ) {
count += ( coins[ i] + 1 ) / 2 ;
}
return count;
}
} ;
class Solution {
public :
int smallestEqual ( vector< int > & nums) {
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
if ( i % 10 == nums[ i] ) {
return i;
}
}
return - 1 ;
}
} ;
class Solution {
public :
int findMaxConsecutiveOnes ( vector< int > & nums) {
int count = 0 ;
int end = 0 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
if ( nums[ i] == 1 ) {
count = count + 1 ;
if ( count > end) {
end = count;
}
} else {
count = 0 ;
}
}
return end;
}
} ;
class Solution {
public :
int countKDifference ( vector< int > & nums, int k) {
int count = 0 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
for ( int j = i + 1 ; j < nums. size ( ) ; j++ ) {
if ( abs ( nums[ i] - nums[ j] ) == k) {
count++ ;
}
}
}
return count;
}
} ;
class Solution {
public :
int maxProduct ( vector< int > & nums) {
int maxIndex = 0 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
if ( nums[ i] > nums[ maxIndex] ) {
maxIndex = i;
}
}
int secMaxIndex = - 1 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
if ( i != maxIndex) {
if ( secMaxIndex == - 1 || nums[ i] > nums[ secMaxIndex] ) {
secMaxIndex = i;
}
}
}
return ( nums[ maxIndex] - 1 ) * ( nums[ secMaxIndex] - 1 ) ;
}
} ;
class Solution {
public :
int differenceOfSum ( vector< int > & nums) {
int x = 0 , y = 0 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
x += nums[ i] ;
while ( nums[ i] ) {
y += nums[ i] % 10 ;
nums[ i] = nums[ i] / 10 ;
}
}
return abs ( x - y) ;
}
} ;
class Solution {
public :
int maximizeSum ( vector< int > & nums, int k) {
int count = 0 ;
while ( k-- ) {
int maxIndex = 0 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
if ( nums[ i] > nums[ maxIndex] ) {
maxIndex = i;
}
}
count += nums[ maxIndex] ;
nums[ maxIndex] += 1 ;
}
return count;
}
} ;
class Solution {
public :
int arithmeticTriplets ( vector< int > & nums, int diff) {
int count = 0 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
for ( int j = i + 1 ; j < nums. size ( ) ; j++ ) {
if ( nums[ j] - nums[ i] == diff) {
for ( int k = j + 1 ; k < nums. size ( ) ; k++ ) {
if ( nums[ k] - nums[ j] == diff) {
count++ ;
}
}
}
}
}
return count;
}
} ;
class Solution {
public :
int removeElement ( vector< int > & nums, int val) {
int l = 0 ;
int r = nums. size ( ) - 1 ;
while ( l <= r) {
if ( nums[ l] == val) {
int temp = nums[ l] ;
nums[ l] = nums[ r] ;
nums[ r] = temp;
r-- ;
} else {
l++ ;
}
}
return r + 1 ;
}
} ;
class Solution {
public :
vector< int > buildArray ( vector< int > & nums) {
vector< int > result;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
int ans = nums[ nums[ i] ] ;
result. push_back ( ans) ;
}
return result;
}
} ;
class Solution {
public :
vector< int > getConcatenation ( vector< int > & nums) {
vector< int > result;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
result. push_back ( nums[ i] ) ;
}
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
result. push_back ( nums[ i] ) ;
}
return result;
}
} ;
class Solution {
public :
vector< bool > kidsWithCandies ( vector< int > & candies, int extraCandies) {
vector< bool > result;
for ( int i = 0 ; i < candies. size ( ) ; i++ ) {
int maxIndex = 0 ;
candies[ i] += extraCandies;
for ( int j = 0 ; j < candies. size ( ) ; j++ ) {
if ( candies[ j] > candies[ maxIndex] ) {
maxIndex = j;
}
}
if ( candies[ i] == candies[ maxIndex] ) {
result. push_back ( true ) ;
} else {
result. push_back ( false ) ;
}
candies[ i] -= extraCandies;
}
return result;
}
} ;
class Solution {
public :
int findMiddleIndex ( vector< int > & nums) {
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
int l = 0 ;
int r = 0 ;
for ( int j = 0 ; j < i; j++ ) {
l += nums[ j] ;
}
for ( int j = i + 1 ; j < nums. size ( ) ; j++ ) {
r += nums[ j] ;
}
if ( l == r) {
return i;
}
}
return - 1 ;
}
} ;
class Solution {
public :
int singleNonDuplicate ( vector< int > & nums) {
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
if ( i == 0 ) {
if ( nums. size ( ) == 1 || nums[ i] != nums[ i + 1 ] ) {
return nums[ i] ;
}
} else if ( i == nums. size ( ) - 1 ) {
if ( nums[ i] != nums[ i - 1 ] ) {
return nums[ i] ;
}
} else {
if ( nums[ i - 1 ] != nums[ i] && nums[ i] != nums[ i + 1 ] ) {
return nums[ i] ;
}
}
}
return 0 ;
}
} ;
class Solution {
public :
vector< int > getRow ( int rowIndex) {
int nums[ 34 ] [ 34 ] ;
for ( int i = 0 ; i <= rowIndex; i++ ) {
for ( int j = 0 ; j <= i; j++ ) {
if ( j == 0 || i == j) {
nums[ i] [ j] = 1 ;
} else {
nums[ i] [ j] = nums[ i- 1 ] [ j] + nums[ i- 1 ] [ j- 1 ] ;
}
}
}
vector< int > result;
for ( int j = 0 ; j <= rowIndex; j++ ) {
result. push_back ( nums[ rowIndex] [ j] ) ;
}
return result;
}
} ;
class Solution {
public :
int minOperations ( vector< int > & nums, int k) {
int count = 0 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
if ( nums[ i] < k) {
count++ ;
}
}
return count;
}
} ;
class Solution {
public :
vector< int > findPeaks ( vector< int > & mountain) {
vector< int > result;
for ( int i = 1 ; i < mountain. size ( ) - 1 ; i++ ) {
if ( ( mountain[ i] > mountain[ i - 1 ] ) && ( mountain[ i] > mountain[ i + 1 ] ) ) {
result. push_back ( i) ;
}
}
return result;
}
} ;
class Solution {
public :
int countTestedDevices ( vector< int > & batteryPercentages) {
int count = 0 ;
for ( int i = 0 ; i < batteryPercentages. size ( ) ; i++ ) {
if ( batteryPercentages[ i] > 0 ) {
count++ ;
for ( int j = i + 1 ; j < batteryPercentages. size ( ) ; j++ ) {
batteryPercentages[ j] = max ( 0 , batteryPercentages[ j] - 1 ) ;
}
}
}
return count;
}
} ;
class Solution {
public :
int countPairs ( vector< int > & nums, int target) {
int count = 0 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
for ( int j = i + 1 ; j < nums. size ( ) ; j++ ) {
if ( nums[ i] + nums[ j] < target) {
count++ ;
}
}
}
return count;
}
} ;
class Solution {
public :
int sumIndicesWithKSetBits ( vector< int > & nums, int k) {
int sum = 0 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
int num = i;
int count = 0 ;
while ( num) {
if ( num & 1 ) {
count++ ;
}
num >>= 1 ;
}
if ( count == k) {
sum += nums[ i] ;
}
}
return sum;
}
} ;
class Solution {
public :
vector< int > numberOfPairs ( vector< int > & nums) {
int count = 0 ;
bool judge[ 100 ] = { 0 } ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
for ( int j = 0 ; j < i; j++ ) {
if ( judge[ j] == true ) {
continue ;
}
if ( nums[ i] == nums[ j] ) {
judge[ i] = judge[ j] = true ;
count++ ;
break ;
}
}
}
return { count, ( int ) nums. size ( ) - 2 * count} ;
}
} ;
class Solution {
public :
int duplicateNumbersXOR ( vector< int > & nums) {
int count = 0 ;
long visited = 0 ;
for ( int i = 0 ; i < nums. size ( ) ; i++ ) {
int x = nums[ i] ;
if ( visited & ( ( long ) 1 << x) ) {
count ^= x;
} else {
visited |= ( ( long ) 1 << x) ;
}
}
return count;
}
} ;