1、两个数之和

  1. import java.io.*;
  2. import java.util.*;
  3. public class Solution {
  4. public static void main(String[] args) {
  5. /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
  6. Scanner scanner = new Scanner(System.in);
  7. int c;
  8. int a = scanner.nextInt();
  9. int b = scanner.nextInt();
  10. scanner.close();
  11. c=twoSum(a,b);
  12. System.out.println(c);
  13. }
  14. public static int twoSum(int a,int b){
  15. int c;
  16. c = a+b;
  17. return c;
  18. }
  19. }

2、一个数组中所有元素之和

  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12. class Result {
  13. /*
  14. * Complete the 'simpleArraySum' function below.
  15. *
  16. * The function is expected to return an INTEGER.
  17. * The function accepts INTEGER_ARRAY ar as parameter.
  18. */
  19. public static int simpleArraySum(List<Integer> ar) {
  20. // Write your code here
  21. int result = 0;
  22. int num;
  23. num = ar.size() - 1;
  24. while(num >= 0){
  25. result = result + ar.get(num);
  26. num--;
  27. }
  28. return result;
  29. }
  30. }
  31. public class Solution {
  32. public static void main(String[] args) throws IOException {
  33. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  34. BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  35. int arCount = Integer.parseInt(bufferedReader.readLine().trim());
  36. List<Integer> ar = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  37. .map(Integer::parseInt)
  38. .collect(toList());
  39. int result = Result.simpleArraySum(ar);
  40. bufferedWriter.write(String.valueOf(result));
  41. bufferedWriter.newLine();
  42. bufferedReader.close();
  43. bufferedWriter.close();
  44. }
  45. }

3、一个数组中所有元素之和(元素为大整数)

  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12. class Result {
  13. /*
  14. * Complete the 'aVeryBigSum' function below.
  15. *
  16. * The function is expected to return a LONG_INTEGER.
  17. * The function accepts LONG_INTEGER_ARRAY ar as parameter.
  18. */
  19. public static long aVeryBigSum(List<Long> ar) {
  20. // Write your code here
  21. int num;
  22. num=ar.size()-1;
  23. long result = 0;
  24. while(num>=0){
  25. result = result + ar.get(num);
  26. num--;
  27. }
  28. return result;
  29. }
  30. }
  31. public class Solution {
  32. public static void main(String[] args) throws IOException {
  33. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  34. BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  35. int arCount = Integer.parseInt(bufferedReader.readLine().trim());
  36. List<Long> ar = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  37. .map(Long::parseLong)
  38. .collect(toList());
  39. long result = Result.aVeryBigSum(ar);
  40. bufferedWriter.write(String.valueOf(result));
  41. bufferedWriter.newLine();
  42. bufferedReader.close();
  43. bufferedWriter.close();
  44. }
  45. }

4、一个数组两条对角线之差取绝对值

  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12. class Result {
  13. /*
  14. * Complete the 'diagonalDifference' function below.
  15. *
  16. * The function is expected to return an INTEGER.
  17. * The function accepts 2D_INTEGER_ARRAY arr as parameter.
  18. */
  19. public static int diagonalDifference(List<List<Integer>> arr) {
  20. // Write your code here
  21. int num = arr.size()-1;
  22. int n = num;
  23. int dia1 = 0;
  24. int dia2 = 0;
  25. int result;
  26. int i,j;
  27. while(num>=0){
  28. i = n-num;
  29. j = num;
  30. List<Integer> term = arr.get(i);
  31. dia1 = dia1 + term.get(i);
  32. dia2 = dia2 + term.get(j);
  33. num--;
  34. }
  35. result = dia1 - dia2;
  36. if(result<0){
  37. result = result * (-1);
  38. }
  39. return result;
  40. }
  41. }
  42. public class Solution {
  43. public static void main(String[] args) throws IOException {
  44. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  45. BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  46. int n = Integer.parseInt(bufferedReader.readLine().trim());
  47. List<List<Integer>> arr = new ArrayList<>();
  48. IntStream.range(0, n).forEach(i -> {
  49. try {
  50. arr.add(
  51. Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  52. .map(Integer::parseInt)
  53. .collect(toList())
  54. );
  55. } catch (IOException ex) {
  56. throw new RuntimeException(ex);
  57. }
  58. });
  59. int result = Result.diagonalDifference(arr);
  60. bufferedWriter.write(String.valueOf(result));
  61. bufferedWriter.newLine();
  62. bufferedReader.close();
  63. bufferedWriter.close();
  64. }
  65. }

5、一个数组中正,负,零的比例计算

  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12. class Result {
  13. /*
  14. * Complete the 'plusMinus' function below.
  15. *
  16. * The function accepts INTEGER_ARRAY arr as parameter.
  17. */
  18. public static void plusMinus(List<Integer> arr) {
  19. // Write your code here
  20. int num;
  21. int pos = 0, fu = 0,zero = 0;
  22. double a,b,c;
  23. num = arr.size() - 1;
  24. int n = num + 1;
  25. while(num >= 0){
  26. if(arr.get(num) > 0){
  27. pos = pos + 1;
  28. }else if (arr.get(num) == 0){
  29. zero = zero + 1;
  30. }else {
  31. fu = fu + 1;
  32. }
  33. num--;
  34. }
  35. a =(float) pos / n;
  36. b =(float) fu / n;
  37. c = (float) zero / n;
  38. String s1 = String.format("%.6f", a);
  39. String s2 = String.format("%.6f", b);
  40. String s3 = String.format("%.6f", c);
  41. System.out.println(s1);
  42. System.out.println(s2);
  43. System.out.println(s3);
  44. }
  45. }
  46. public class Solution {
  47. public static void main(String[] args) throws IOException {
  48. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  49. int n = Integer.parseInt(bufferedReader.readLine().trim());
  50. List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  51. .map(Integer::parseInt)
  52. .collect(toList());
  53. Result.plusMinus(arr);
  54. bufferedReader.close();
  55. }
  56. }

6、给定正整数n,打印右对齐上三角

  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12. class Result {
  13. /*
  14. * Complete the 'staircase' function below.
  15. *
  16. * The function accepts INTEGER n as parameter.
  17. */
  18. public static void staircase(int n) {
  19. // Write your code here
  20. int i = 1,j = n;
  21. while(j>0){
  22. for (int k = n; k > 0; k--) {
  23. if (k > i){
  24. System.out.print(" ");
  25. }else{
  26. System.out.print("#");
  27. }
  28. if (k == 1){
  29. System.out.println("");
  30. }
  31. }
  32. j--;
  33. i++;
  34. }
  35. }
  36. }
  37. public class Solution {
  38. public static void main(String[] args) throws IOException {
  39. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  40. int n = Integer.parseInt(bufferedReader.readLine().trim());
  41. Result.staircase(n);
  42. bufferedReader.close();
  43. }
  44. }

7、给定5个元素数组,求其中4个元素的最大之和与最小之和

  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12. class Result {
  13. /*
  14. * Complete the 'miniMaxSum' function below.
  15. *
  16. * The function accepts INTEGER_ARRAY arr as parameter.
  17. */
  18. public static void miniMaxSum(List<Integer> arr) {
  19. // Write your code here
  20. int num;
  21. int min = arr.get(0);
  22. int max = 0;
  23. int j = 0,k = 0;
  24. num = arr.size();
  25. for (int i = 0; i < num; i++) {
  26. if (min > arr.get(i)){
  27. min = arr.get(i);
  28. j = i;
  29. }
  30. if (max < arr.get(i)){
  31. max = arr.get(i);
  32. k =i;
  33. }
  34. }
  35. long sum1 = 0;
  36. long sum2 = 0;
  37. for (int i = 0; i < num; i++) {
  38. if (i != j){
  39. sum1 = sum1 + arr.get(i);
  40. }
  41. if(i != k){
  42. sum2 = sum2 + arr.get(i);
  43. }
  44. }
  45. /*System.out.println(j);
  46. System.out.println(min);
  47. System.out.println(k);
  48. System.out.println(max);
  49. */
  50. System.out.print(sum2 + " " + sum1);
  51. }
  52. }
  53. public class Solution {
  54. public static void main(String[] args) throws IOException {
  55. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  56. List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  57. .map(Integer::parseInt)
  58. .collect(toList());
  59. Result.miniMaxSum(arr);
  60. bufferedReader.close();
  61. }
  62. }

8、一个数组中最大数值的个数

  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12. class Result {
  13. /*
  14. * Complete the 'birthdayCakeCandles' function below.
  15. *
  16. * The function is expected to return an INTEGER.
  17. * The function accepts INTEGER_ARRAY candles as parameter.
  18. */
  19. public static int birthdayCakeCandles(List<Integer> candles) {
  20. // Write your code here
  21. int num;
  22. long max = 0;
  23. int result = 0;
  24. num = candles.size();
  25. for (int i = 0; i < num; i++) {
  26. if (max < candles.get(i)){
  27. max = candles.get(i);
  28. }
  29. }
  30. for (int i = 0; i < num; i++) {
  31. if (candles.get(i)==max){
  32. result = result + 1;
  33. }
  34. }
  35. return result;
  36. }
  37. }
  38. public class Solution {
  39. public static void main(String[] args) throws IOException {
  40. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  41. BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  42. int candlesCount = Integer.parseInt(bufferedReader.readLine().trim());
  43. List<Integer> candles = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  44. .map(Integer::parseInt)
  45. .collect(toList());
  46. int result = Result.birthdayCakeCandles(candles);
  47. bufferedWriter.write(String.valueOf(result));
  48. bufferedWriter.newLine();
  49. bufferedReader.close();
  50. bufferedWriter.close();
  51. }
  52. }