成绩排序
题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩
都按先录入排列在前的规则处理。
示例:
jack 70
peter 96
Tom 70
smith 67
从高到低
peter 96
jack 70
Tom 70
smith 67
从低到高
smith 67
jack 70
Tom 70
peter 96
示例
输入
//输入多行,先输入要排序的人的个数,然后输入排序方法0(降序)或者1(升序)再分别输入他们的名字和成绩,以一个空格隔开 3 0 fang 90 yang 50 ning 70
输出
//按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开 fang 90 ning 70 yang 50
C++
#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; struct student{ string name; int score; }; bool cmp0(const student &a, const student &b){ // 从高到低排序 return a.score > b.score; } bool cmp1(const student &a, const student &b){ // 从低到高排序 return a.score < b.score; } int main(){ //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int N, type; while(cin >> N >> type){ vector<student> stud(N); for(int i = 0; i < N; i ++){ cin >> stud[i].name >> stud[i].score; } if(type == 0) stable_sort(stud.begin(), stud.end(), cmp0); else stable_sort(stud.begin(), stud.end(), cmp1); for(int i = 0; i < N; i ++){ cout << stud[i].name << " " << stud[i].score << endl; } } return 0; }
Java
/* Java 思路 按插入顺序读,可以用LinkedHashMap 有重复的名字,重复的分数 所以用 name + " " + score 做key,用score做value 先将分数排序,再遍历分数(该分数i只找一次) 每次从LinkedHashMap中遍历,找到与i相等的key */ import java.util.*; public class Main{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int order = sc.nextInt(); ArrayList<Integer> list = new ArrayList(); LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); for (int i = 0; i < n; i++) { String name = sc.next(); int score = sc.nextInt(); list.add(score); map.put(name + " " + score, score); } Collections.sort(list); if (order == 0) Collections.reverse(list); int pre = -1; for (int i : list) { if (pre == i) continue; for (String key : map.keySet()) if (map.get(key).equals(i)) System.out.println(key); pre = i; } } } }
二维数组中的查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
C++
/* 思路 * 矩阵是有序的,从左下角来看,向上数字递减,向右数字递增, * 因此从左下角开始查找,当要查找数字比左下角数字大时。右移 * 要查找数字比左下角数字小时,上移 */ class Solution { public: bool Find(vector<vector<int> > array,int target) { int rowCount = array.size(); int colCount = array[0].size(); int i,j; for(i=rowCount-1,j=0;i>=0&&j<colCount;) { if(target == array[i][j]) return true; if(target < array[i][j]) { i--; continue; } if(target > array[i][j]) { j++; continue; } } return false; } };
java
/* 两种思路 一种是: 把每一行看成有序递增的数组, 利用二分查找, 通过遍历每一行得到答案, 时间复杂度是nlogn */ public class Solution { public boolean Find(int [][] array,int target) { for(int i=0;i<array.length;i++){ int low=0; int high=array[i].length-1; while(low<=high){ int mid=(low+high)/2; if(target>array[i][mid]) low=mid+1; else if(target<array[i][mid]) high=mid-1; else return true; } } return false; } } /* 另外一种思路是: 利用二维数组由上到下,由左到右递增的规律, 那么选取右上角或者左下角的元素a[row][col]与target进行比较, 当target小于元素a[row][col]时,那么target必定在元素a所在行的左边, 即col--; 当target大于元素a[row][col]时,那么target必定在元素a所在列的下边, 即row++; */ public class Solution { public boolean Find(int [][] array,int target) { int row=0; int col=array[0].length-1; while(row<=array.length-1&&col>=0){ if(target==array[row][col]) return true; else if(target>array[row][col]) row++; else col--; } return false; } }
斐波那契数列
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
C++
class Solution { public: int Fibonacci(int n) { int f = 0, g = 1; while(n--) { g += f; f = g - f; } return f; } };
Java
public class Solution { public int Fibonacci(int n) { int preNum=1; int prePreNum=0; int result=0; if(n==0) return 0; if(n==1) return 1; for(int i=2;i<=n;i++){ result=preNum+prePreNum; prePreNum=preNum; preNum=result; } return result; } }
来源:https://www.cnblogs.com/wwj99/p/12163841.html