Write a program to find the index of an integer in an array. Assume that the array size is 50. The program will read the array elements first and then input an integer to search in the array. If the integer appears more than one time in the array, give the index for the first appearance of this integer. If the integer does not appear in the array, print “Not found”. The input has -1 as the end, but -1 is not saved in the array.
时间: 2023-06-01 20:04:50 浏览: 171
Sure, here's a sample Python code that can solve the problem:
```
array = []
for i in range(50):
num = int(input())
if num == -1:
break
else:
array.append(num)
search_num = int(input())
if search_num in array:
index = array.index(search_num)
print(index)
else:
print("Not found")
```
Basically, the code first reads the input numbers and saves them into an array until it reaches -1. Then, it reads the search number and uses the `index()` function to find its index in the array. If the number is not found, it prints "Not found". Hope this helps!
相关问题
Write a C++ program to determine and display the smallest and largest valuescontained in 99-elcment floating-point array w.
Sure, here's a program in C++ to determine and display the smallest and largest values contained in a 99-element floating-point array `w`:
```cpp
#include <iostream>
#include <limits> // needed for numeric_limits
int main() {
float w[99]; // declare the array
float smallest, largest; // variables to store the smallest and largest values
// code to fill the array with values goes here
// initialize smallest and largest to the first element of the array
smallest = w[0];
largest = w[0];
// loop through the array and update smallest and largest as necessary
for (int i = 1; i < 99; i++) {
if (w[i] < smallest) {
smallest = w[i];
}
if (w[i] > largest) {
largest = w[i];
}
}
// display the smallest and largest values
std::cout << "Smallest value: " << smallest << std::endl;
std::cout << "Largest value: " << largest << std::endl;
return 0;
}
```
Note that you'll need to add code to fill the array `w` with values before the loop that determines the smallest and largest values. Also, in C++ we can use `numeric_limits` from the `<limits>` header to initialize `smallest` and `largest` to the maximum and minimum possible values for a float, respectively, like this:
```cpp
float smallest = std::numeric_limits<float>::max();
float largest = std::numeric_limits<float>::min();
```
This way, we don't need to assume that the first element of the array is the smallest or largest.
Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
To solve this problem, we can use Boyer-Moore Voting Algorithm, which runs in O(n) time complexity and O(1) space complexity.
The idea behind this algorithm is to cancel out all occurrences of an element e that is not the majority element and count the remaining element as the majority element.
Here is the implementation of the algorithm in Python:
```
def majorityElement(nums):
count = 0
candidate = None
for num in nums:
if count == 0:
candidate = num
count += (1 if num == candidate else -1)
return candidate
```
In the above code, we first initialize the count and candidate variables to 0 and None respectively. Then, we loop through the array and check if the count is 0, if it is, we set the candidate to the current element. We then increment the count if the current element is the candidate, otherwise we decrement it.
At the end of the loop, the candidate variable will hold the majority element.