第一种不太推荐 如果题目有时间复杂度要求或者是测试用例给的数组特别大,这种根本AC不了的 #include<bits/stdc++.h> using namespace std; int main(){ vector<int>num; int n; while(cin>>n) num.push_back(n); if(num.size() == 0) return 0; sort(num.begin(),num.end()); cout<<num[0]<<endl; return 0; } 第二种是采取二分法 看到有序就应该想到二分法的(前后两个小数组分别是非递减数组,注意是非递减) #include<bits/stdc++.h> using namespace std; int main(){ vector<int>num; int n; while(cin>>n) num.push_back(n); int left=0,right=num.size()-1; while(left<right) { int mid=(left+right)>>1; if(num[mid]>num[right]) left=mid+1;//当中间元素大于最后一个,表明最小值在右半区间 else if(num[mid]<num[right]) right=mid;//当中间元素小于最后一个,表明最小值在左半区间,注意没有减一操作 else right=right-1;//当中间元素 等于 最后一个,例如 2341001说明有重复元素,将范围缩小一个 } cout<<num[left]<<endl; return 0; }
import java.util.*; import java.io.*; public class Main{ public static int func(int[] arr){ int min=arr[0]; for(int i=1;i<arr.length;i++){ if(arr[i]<min){ min=arr[i]; } } return min; } public static void main(String[] args) throws IOException{ BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); //Scanner sc=new Scanner(System.in); String[] nums=input.readLine().split(" "); int num[]=new int[nums.length]; for(int i=0;i<num.length;i++){ num[i]=Integer.valueOf(nums[i]); } System.out.println(func(num)); } }
#include <iostream> #include <vector> using namespace std; int main(){ vector<int> vec; int val; while(cin >> val){ vec.push_back(val); } //有序数组 - 二分 int low = 0; int high = vec.size() - 1; while(low < high){ // 1 2 3 4 5 -> 3 4 5 1 2 if(vec[low] < vec[high]){ //第一次出现vec[low]<vec[high]即出现最小值 cout << vec[low] << endl; return 0; } int mid = low + (high - low)/2; //确定左、右部分的增减区间 if(vec[low] < vec[mid]){ //左区间是非递减的 low = mid + 1; } else if(vec[high] > vec[mid]){ //右区间也是非递减的 high = mid; } else{ ++low; } } cout << vec[low] << endl; //不要忘了这步,22222/22212这种类型 return 0; }
/* 找到第一个元素小于前面的元素,该值为最小 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main{ public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); //Scanner input = new Scanner(System.in);//会超时 //String[] str = input.nextLine().split(" "); String[] str = input.readLine().split(" "); int[] arr = new int[str.length]; for(int i = 0;i<str.length;i++) arr[i] = Integer.parseInt(str[i]); // int result = arr[0]; for(int i = 0;i<arr.length - 1;i++){ if(arr[i+1]<arr[i]){ result = arr[i+1]; break; } } System.out.println(result); } }
class Solution: def findMinForRaNum(self,nums): if len(nums) == 1: return nums[0] l, h = 0, len(nums)-1 while l < h: mid = int(l + (h - l)/2) if nums[mid] > nums[h]: l = mid + 1 else: h = mid return nums[l] while True: try: nums = list(map(int, raw_input().split())) s = Solution() result = s.findMinForRaNum(nums) print(result) except: break
/*import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main{ public static void main(String args[])throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str=in.readLine(); String []s=str.split(" "); int []a=new int[s.length]; for(int i=0;i<s.length;i++){ a[i]=Integer.parseInt(s[i]); } Arrays.sort(a); System.out.println(a[0]); } }*/ import java.util.*; public class Main{ public static void main(String args[]){ Scanner in=new Scanner(System.in); int n = 0; int[] a = new int[1000000]; while(in.hasNext()) { a[n++] = in.nextInt(); } int l=0; int r=n-1; while(r>l){ int mid=(l+r)/2; if(a[mid]<a[r])r=mid; else if(a[mid]>a[r])l=mid+1; else { r = r - 1; } } System.out.println(a[l]); } }
import java.util.*; public class Main{ public static void main(String []args){ fun81812(); } public static void fun81812() { Scanner in=new Scanner(System.in); int min=in.nextInt(); while(in.hasNext()) { int now=in.nextInt(); if(now<min) { min=now; } } System.out.println(min); /*String []k=in.nextLine().split(" "); int min=Integer.parseInt(k[0]); for(int i=0;i<k.length;i++) { int now =Integer.parseInt(k[i]); if(now<min) { min=now; } } System.out.println(min);*/ } }被自己蠢到的一道题,因为反正要读取输入啊,直接每次都比较保存了最小值不就好了?被描述带偏了也是第一次。下次注意额
def getMin(arr): low = 0 high = len(arr) - 1 if len(arr) == 0: return 0 if len(arr) ==1: return arr[0] while low <= high: mid = (low + high) >> 1 # 右移一位相当于除以2 if arr[mid]>arr[high]: low = mid + 1 elif arr[mid] < arr[low]: high = mid else: high -= 1 return arr[low]
#l = list(map(int, input().split())) #print(min(l)) class Solution: def minArray(self, numbers): for i in range(len(numbers)-1): if numbers[i] > numbers[i+1]: return numbers[i+1] return numbers[0] while True: try: numbers = list(map(int, input().split())) s = Solution() result = s.minArray(numbers) print(result) except: break
import java.util.Scanner; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException{ //Scanner sc=new Scanner(System.in);//超时 //String s=sc.nextLine(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s=br.readLine(); String[] ss=s.split(" "); int[] array=new int[ss.length]; for(int i=0;i<array.length;i++){ array[i]=Integer.parseInt(ss[i]); } System.out.println(getMin2(array)); } //法一 public static int getMin2(int[] array){ if(array==null){ return 0; } if(array.length==1){ return array[0]; }else{ int j=0; for(int i=0;i<array.length-1;i++){ if(array[i+1]<array[i]){ j=i+1; break; } } return array[j]; } }//法二public static int getMin(int[] array){ if(array==null){ return 0; } int start=0; int end=array.length-1; int mid=0; while(start<end){ if(start==end-1){ if(array[start]<array[end]){ return array[start]; }else{ return array[end]; } }else{ mid=(start+end)/2; if(array[mid]<array[start]){ end=mid; }else if(array[mid]>array[end]){ start=mid+1; }else{ int min=array[start]; for(int i=start+1;i<end;i++){ if(min>array[i]){ min=array[i]; } } return min; } } } return array[start]; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @Date: 2020-05-05 14:35 * @version: 1.0 */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] s = br.readLine().split(" "); int num[] = new int[s.length]; for (int i = 0; i < num.length; i++) num[i] = Integer.valueOf(s[i]); int l = 0; int r = num.length-1; int mid; while (l<=r){ mid = (l+r)>>1; if (num[mid]>=num[r]) l=mid+1; else r = mid; } System.out.println(num[r]); } }
import sys def solve(nums): if not nums: return 0 if len(nums) == 1: return nums[0] low, high = 0, len(nums)-1 while low < high - 1: mid = (low + high) // 2 if nums[low] < nums[mid] < nums[high]: return nums[low] if nums[mid] > nums[low]: low = mid + 1 elif nums[mid] < nums[low]: high = mid else: low += 1 return min(nums[low], nums[high]) nums = map(int, sys.stdin.readline().strip().split()) print solve(nums)
#include <iostream> #include <vector> using namespace std; void rotateArrayMin(vector<int>& vec) { int l = 0; int r = vec.size()-1; int mid; while(l < r) { mid = l + (r-l)/2; if(vec[mid] == vec[r]) { --r; } else if(vec[mid] < vec[r]) { r = mid; } //直接写成else l = mid + 1即可,多了左边的比较,效率也变低,比较蠢 else if(vec[mid] > vec[l]) { l = mid + 1; } else if(vec[mid] == vec[l]) { l = l + 1; } } cout << vec[l] << endl; } int main() { vector<int> vec; int n; while(cin >> n) vec.push_back(n); rotateArrayMin(vec); return 0; }
def minValueRotateArray(rotateArray): if rotateArray == []: return 0 left=0 right = len(rotateArray)-1 while left != right and left != right-1: if rotateArray[left] > rotateArray[right]: temp = int((left+right)/2) if rotateArray[temp] >= rotateArray[left]: left = temp else : right = temp elif rotateArray[left] == rotateArray[right]: temp = right-1 if rotateArray[temp] > rotateArray[left]: left = temp else : right = temp else : temp = int((left+right)/2) if rotateArray[temp] > rotateArray[left]: right = temp else : left = temp if rotateArray[left] > rotateArray[right]: return rotateArray[right] else : return rotateArray[left] if __name__ == '__main__': listx = input() listy = [] for ix in listx.split(' '): listy.append(int(ix)) minValue = minValueRotateArray(listy) print(minValue)