#include<iostream> using namespace std; int main(){ int number; while(cin>>number){ int* a=new int[number]; int temp; for(int i=0;i<number;i++) cin>>a[i]; int min=1000000,max=-1000000; for(int i=0;i<number;i++){ if(a[i]<min) min=a[i]; if(a[i]>max) max=a[i]; } cout<<max<<' '<<min<<endl; } }
import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int[] nums = new int[n]; for(int i=0;i<n;i++){ nums[i] = sc.nextInt(); } Arrays.sort(nums); System.out.println(nums[n-1]+" "+nums[0]); } } }
#include<iostream> using namespace std; int main() { int n,temp,min,max; while(scanf("%d",&n)!=EOF) { cin>>temp; min=max=temp; while(--n) { cin>>temp; if(temp>max)max=temp; if(min>temp)min=temp; } cout<<max<<" "<<min<<endl; } }这个题,很明显不需要初始化max和min,也不需要另外开辟数组,很简单啊
#include <iostream> using namespace std; int main(){ int num; while(scanf("%d",&num) != EOF){ int data, max = -65536, min =65535; for(int i = 0; i < num; i++){ cin >> data; if(data>=max){ max = data; } if(data <= min){ min = data; } } cout << max << " " << min <<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int n; while(cin>>n){ int min,max; // 边读边保存最大最小值 cin>>min; max = min; int temp; for(int i=1;i<n;i++){ cin>>temp; min = min<temp?min:temp; max = max>temp?max:temp; } cout<<max<<" "<<min<<endl; } return 0; }
//用sort库函数,时间复杂度高 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int Maximum = 10000; int array[Maximum]; int main(){ int N; while(cin>>N){ for(int i=0; i<N; i++){ cin>>array[i]; } sort(array,array+N); cout<<array[N-1]<<" "<<array[0]<<endl; } return 0; }
#include <bits/stdc++.h>
const int INF=1<<30;
using namespace std;
int main()
{
int n;
while(cin>>n){
int num[n];
int maxv=-INF;int minv=INF;
for(int i=0;i<n;i++)
{
cin>>num[i];
if(maxv<num[i])
maxv=num[i];
if(minv>num[i])
minv=num[i];
}
cout<<maxv<<" "<<minv<<endl;
}
return 0;
}
#include<stdio.h> #include<algorithm> using namespace std; int main(){ int n; int buf[10000]; while(scanf("%d",&n)!=EOF){ for(int i=0;i<n;i++){ scanf("%d",&buf[i]); } sort(buf,buf+n); printf("%d %d\n",buf[n-1],buf[0]); } return 0; }
//万水千山总是情,来个冒泡行不行 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); while(input.hasNext()){ int n=input.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=input.nextInt(); } for(int i=1;i<arr.length;i++){ for(int j=0;j<arr.length-i;j++){ if(arr[j]>arr[j+1]){ int temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } System.out.println(arr[n-1]+" "+arr[0]); } } }
#include<stdio.h> int main (){ int n,i,max,min,t; for(;~scanf("%d",&n)&&~scanf("%d",&t);) for (max=min=t,i = 1; i < n && ~scanf("%d",&t) && (max<t?max = t:1,min>t?min = t:1);i==n-1?printf("%d %d\n",max,min):0,i++); }
//so easy package tsinghua.minMax; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while(scanner.hasNext()) { int numOfNum=scanner.nextInt(); int min=Integer.MAX_VALUE; int max=Integer.MIN_VALUE; for(int i=0;i<numOfNum;i++) { int tempNum=scanner.nextInt(); if(tempNum<min) min=tempNum; if(tempNum>max) max=tempNum; } System.out.println(max+" "+min); } } }