杭电 java版解题集(更新ing)

本文集包含了一系列经典的编程问题解决方案,涵盖了从简单的加法运算到复杂的矩阵求和等各类算法题目,通过Java语言实现,适合初学者及进阶者学习与参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1000 A + B Problem

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a+b);
        }
    }
}

1001 Sum Problem

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while(sc.hasNext()) {
      int sum = 0;
      int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
          sum += i;
        }
        System.out.println(sum);
        System.out.println();
      }
    }
}

1002 A + B Problem II

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    for (int i = 1; i <= n; i++) {
      if (i != 1) System.out.println();
      BigInteger a = sc.nextBigInteger();
      BigInteger b = sc.nextBigInteger();
      System.out.println("Case " + i + ":");
      System.out.println(a + " + " + b + " = " + a.add(b));


    }
  }

}

1003 Max Sum

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int n = sc.nextInt();
    for (int i = 0; i < n; i++) {
      int m = sc.nextInt();
      int index = 0, l = 0, r = 0;
      int max = sc.nextInt();
      int sum = max;
      if (sum < 0) {
        sum = 0;
        l = 1;
      }
      for (int j = 1; j < m; j++) {
        int k = sc.nextInt();
        sum += k;
        if (sum > max) {
          max = sum;
          index = l;
          r = j;
        }

        if (sum < 0) {
          l = j+1;
          sum = 0;
        }
      }
      System.out.println("Case "+(i+1)+":");
      System.out.println(max+" "+(index+1)+" "+(r+1));
      if ((i+1) != n) {
        System.out.println();
      }
    }

  }
}

1004 Let the Balloon Rise

import java.text.DecimalFormat;
import java.util.Scanner;

import static java.lang.StrictMath.sin;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

       while (sc.hasNext()) {
         int n = sc.nextInt();
         String[] str = new String[1001];
         int []c = new int[1001];
         for (int i = 0; i <= 1000; i++) c[i] = 1;
         
         if (n == 0) break;
         for (int i = 0; i < n; i++) {
           String per = sc.next();
           str[i] = per;
           if (i >= 1) {
             for (int j = 0; j <= i; j++) {
               if (str[i].equals(str[j])) {
                 c[j]++;
               }
             }
           }
         }
         int max = c[0], index = 0;
         for (int i = 0; i < c.length; i++) {
           if (c[i] > max) {
             max = c[i];
             index = i;
           }
         }
         System.out.println(str[index]);
     }
    }
}

1005 Number Sequence



import java.util.Scanner;

public class Main_1 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      int a = sc.nextInt();
      int b = sc.nextInt();
      int n = sc.nextInt();
      if (a == 0 && b == 0 && n == 0) System.exit(0);
      int[] f = new int[50];
      f[1] = 1;
      f[2] = 1;
      for (int i = 3; i < 50; i++) {
        f[i] = (a * f[i - 1] + b * f[i - 2]) % 7;
      }
       System.out.println(f[n%49]);}

  }
}

1012 u Calculate e

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
      int n = 2;
      double s=2.5, t;
      System.out.println("n e");
      System.out.println("- -----------");
      System.out.println("0 1");
      System.out.println("1 2");
      System.out.println("2 2.5");
      DecimalFormat df = new DecimalFormat("0.000000000");
      for(int i=3; i<=9; i++) {
        n = i*n;
        s += 1.0/n;
        System.out.println(i + " " +df.format(s));
      }
    }
  }

1040 As Easy As A+B

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while(t > 0) {
      t--;
      int n = sc.nextInt();
      int[] c = new int [n];
      for (int i = 0; i < n; i++) {
        c[i] = sc.nextInt();
      }
      Arrays.sort(c);
      System.out.print(c[0]);
      for (int i = 1; i < n; i++) {
        System.out.print(" "+c[i]);
      }
      System.out.println();
    }


  }
}

 

1042 N!

package com.company;

import java.math.BigInteger;
import java.util.Scanner;

public class Main_7 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      int x = sc.nextInt();
      BigInteger a = BigInteger.valueOf(x);
      BigInteger b = BigInteger.ONE;
      BigInteger s = BigInteger.ONE;
      for (BigInteger i = BigInteger.ONE; i.compareTo(a) <= 0; i = i.add(b)) {
        s = s.multiply(i);
      }
      System.out.println(s);
    }
  }

}

1089       A+B for Input-Output Practice (I)

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      long a = sc.nextInt();
      long b = sc.nextInt();
      System.out.println(a+b);
    }
  }
}

1090 A+B for Input-Output Practice (II)

 

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      long a = sc.nextInt();
      long b = sc.nextInt();
      System.out.println(a+b);
    }
  }
}
[ Copy to Clipboard ]    [ Save to File]

1091 A+B for Input-Output Practice (III)

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main{
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      long a = sc.nextInt();
      long b = sc.nextInt();
      if (a==0 && b == 0) System.exit(0);
      System.out.println(a+b);
    }
  }
}

1092


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      long n = sc.nextInt();
      long s = 0 , x;
      if (n == 0) System.exit(0);
      for (int i = 0; i < n; i++) {
        x = sc.nextInt();
        s += x;
      }
      System.out.println(s);
    }
  }
}

1093

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while(t > 0) {
      t--;
      long n = sc.nextInt();
      long s = 0 , x;
      //if (n == 0) System.exit(0);
      for (int i = 0; i < n; i++) {
        x = sc.nextInt();
        s += x;
      }
      System.out.println(s);
    }


  }
}

1094

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      long n = sc.nextInt();
      long s = 0 , x;
      //if (n == 0) System.exit(0);
      for (int i = 0; i < n; i++) {
        x = sc.nextInt();
        s += x;
      }
      System.out.println(s);
    }
  }
}

1095

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      long a = sc.nextInt();
      long b = sc.nextInt();
      System.out.println(a+b);
      System.out.println();
    }
  }
}

1096

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while(t > 0) {
      t--;
      long n = sc.nextInt();
      long s = 0 , x;
      //if (n == 0) System.exit(0);
      for (int i = 0; i < n; i++) {
        x = sc.nextInt();
        s += x;
      }
      System.out.println(s);
      if (t != 0) System.out.println();
    }


  }
}

1097  A hard puzzle

package com.company;

import java.util.Scanner;



public class Main {
  public static int pow(int a, int b) {
     a = a % 10;
    int res = 1;
    while (b > 0){
      if (b%2 == 1)
        res = res*a % 10;
      b = b/2;
      a =  a * a % 10;

    }
    return res;
  }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
	      while (sc.hasNext()) {
	        int a = sc.nextInt();
	        int b = sc.nextInt();
	        System.out.println(pow(a, b));

     }
    }
}

1098

import java.math.BigInteger;
import java.util.Scanner;

public class Main{
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      int k = sc.nextInt();
      int i;
      for (i = 0; i <= 100; i++) {
        if((18+i*k)%65==0) {
          System.out.println(i);
          break;
        }

      }
      if (i == 101) System.out.println("no");
    }
  }
}

1106 排序




import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc =  new Scanner(System.in);
    while (sc.hasNext()) {

      int k = 0;
      String s = sc.nextLine();
      for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == '5') {
          k++;
        }
        else break;
      }
      s = s.substring(k);
      String[] str = s.split("5+");
      int[]c = new int[str.length];
      for (int i = 0; i < str.length; i++) {
        c[i] = Integer.parseInt(str[i]);
      }
      Arrays.sort(c);


    System.out.print(c[0]);
    for (int i = 1; i < c.length; i++) {
      System.out.print( " " + c[i]);
    }
    System.out.println();
  }
  }
}

1108

import java.util.Scanner;


public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      long a = sc.nextInt();
      long b = sc.nextInt();
      //if (a==0 && b == 0) System.exit(0);
      System.out.println((a*b)/gcd(a, b));

    }
  }

  private static long gcd(long a, long b) {
    return b == 0 ? a : gcd(b, a%b);
  }
}

1201 18岁生日



import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {

  public static  int isyear (int y) {
    if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) return 1;
    else return 0;
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while (t > 0) {
      t--;
      String data = sc.next();
      int year = Integer.valueOf(data.substring(0, 4));
      int mon = Integer.valueOf(data.substring(5, data.lastIndexOf('-')));
      int day = Integer.valueOf(data.substring(data.lastIndexOf('-') + 1));
     // System.out.println(year + " "+mon + " " + day);
      if ( mon == 2 && day == 29) {
        System.out.println("-1");

      }
      else {
        int s = 365*18;
        for (int i = year + 1; i <= year + 17; i++) {
          s += isyear(i);
        }

        /*Calendar cl = new GregorianCalendar(year, mon - 1, day);
        if (isyear(year)) {
          s += (366 - cl.get(Calendar.DAY_OF_YEAR));
        }
        if (!isyear(year)) {
          s += (365 - cl.get(Calendar.DAY_OF_YEAR));
        }
        Calendar cl1 = new GregorianCalendar(year + 18, mon - 1, day);
        s += cl1.get(Calendar.DAY_OF_YEAR);*/
        if (mon > 2) s += isyear(18+year);
        else  if (mon < 2 || day <= 28) s += isyear(year);
        System.out.println(s);
      }
    }
  }
}

1215

import java.util.Scanner;

public class Main {
    /*public static long f(int n) {
        long res = 1;
        for (int i = 2; i < Math.sqrt(n); i++) {
             if (n % i == 0) {
                res += i;
                res += (n/i);
            }
        }
        if ( (Math.sqrt(n)*Math.sqrt(n)) == n && n != 1) {
            res += Math.sqrt(n);
        }
        return res;
    }
    public static long f1(int n) {
        long res = 1;
        for (int i = 2; i < Math.sqrt(n); i++) {
            if (n % i == 0) {
                res += f(i);
                res += f(n/i);
            }
        }
        if ( (Math.sqrt(n)*Math.sqrt(n)) == n && n != 1) {
            res += f((int)(Math.sqrt(n)));
        }
        return res;
    }*/

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long[]a = new long[500001];
        for (int i = 1; i <= 250000; i++) {
            for (int j = i+i; j <= 500000; j+=i)
            a[j] += i;
        }

        int t;
        t = sc.nextInt();
        while (t > 0) {
            t--;
            int n;
            n = sc.nextInt();
            System.out.println(a[n]);
        }
    }
}

1219AC Me

package com.company;

import java.math.BigInteger;
import java.util.Scanner;

public class Main_1 {
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      String str = sc.nextLine();
      int[] mat = new int[26];
      for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        if(ch <= 'z'&&ch >= 'a') {
          mat[(int)(ch-'a')]++;
        }
      }
      for (int i = 0; i < mat.length; i++) {
        System.out.println((char)('a'+i)+":"+mat[i]);
      }
      System.out.println();
    }
  }
}

 

package com.company;


import java.util.Scanner;

public class Main {
    /*public static long f(int n) {
        long res = 1;
        for (int i = 2; i < Math.sqrt(n); i++) {
             if (n % i == 0) {
                res += i;
                res += (n/i);
            }
        }
        if ( (Math.sqrt(n)*Math.sqrt(n)) == n && n != 1) {
            res += Math.sqrt(n);
        }
        return res;
    }
    */

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long[]a = new long[500001];
        for (int i = 1; i <= 250000; i++) {
            for (int j = i+i; j <= 500000; j+=i)
            a[j] += i;
        }

        int t;
        t = sc.nextInt();
        while (t > 0) {
            t--;
            int n;
            n = sc.nextInt();
            System.out.println(a[n]);
        }
    }
}

1232畅通工程



import java.util.Scanner;

public class Main {
  static int[]p = new int[20000];
  public static int find(int x) {
      if (x != p[x]) return p[x] = find(p[x]);
      else return x;
  }
  public static void union(int x, int y) {
    int fx = find(x);
    int fy = find(y);
    if (fx != fy) {
      p[fy] = fx;
    }
  }
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      int N = sc.nextInt();
      if (N == 0) return;
      int M = sc.nextInt();
      for (int i = 1; i <= N; i++) {
        p[i] = i;
      }
      while (M > 0) {
        M--;
        int n = sc.nextInt();
        int m = sc.nextInt();
        union(n, m);
      }
      int sum = 0;
      for (int i = 1; i <= N; i++) {
        if (i == p[i]) sum++;
      }
      System.out.println(sum-1);
    }

  }
}

1249

import java.util.Scanner;

public class Main{
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int t = sc.nextInt();
    for (int i = 1; i <= t; i++) {
      int n = sc.nextInt();
      System.out.println(2+3*n*(n-1));
    }
  }
}

1570 AC



import java.util.Scanner;

public class Main {
    public static long A(long n, long m) {
        long res = 1;
        for (long i = n; i >= (n-m+1); i--) {
            res *= i;
           // System.out.println(res);
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long t;
        t = sc.nextLong();
        while (t > 0) {
            t--;
            String c = sc.next();
            long n = sc.nextLong();
            long m = sc.nextLong();
            if ("A".equals(c)) {
                long s = A(n, m);
                System.out.println(s);
            }
            if ("C".equals(c)) {
                System.out.println((A(n, m)/ (A(m, m))));
            }
        }
    }
}

1559最大子矩阵

package com.company;

import java.util.Scanner;

public class Main_6 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while (t > 0) {
      t--;
      int n = sc.nextInt();
      int m = sc.nextInt();
      int x = sc.nextInt();
      int y = sc.nextInt();
      long[][] dp = new long[n+1][m+1];
      long a;
      //if (n == 0) System.exit(0);
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
          a = sc.nextInt();
          dp[i][j] = dp[i][j - 1] + dp[i - 1][j] + a - dp[i - 1][j - 1];
        }
      }
      long max = -1, temp;
      for (int i = x; i <= n; i++) {
        for (int j = y; j <= m; j++) {
          temp = (dp[i][j] - dp[i - x][j] - dp[i][j - y] + dp[i - x][j - y]);
          max = Math.max(temp, max);
        }
      }
      System.out.println(max);

    }

  }
}

 

1753 大明A+B




import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc =  new Scanner(System.in);
    while (sc.hasNextBigDecimal()) {
      BigDecimal a = sc.nextBigDecimal();
      BigDecimal b = sc.nextBigDecimal();
      System.out.println(a.add(b).stripTrailingZeros().toPlainString());
      //toplainString转为普通计数法给一个字符串1.238761976E-10得到0.0000000001238761976
      //stripTrailingZeros去掉末尾0给一个字符串1245600.000得到1245600这个字符串
    }
  }
}

 

1720A+B Coming

package com.company;

import java.util.Scanner;

public class Main_1720{
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    while (sc.hasNext()) {
      String sa = sc.next();
      String sb = sc.next();
      int a = Integer.parseInt(sa, 16);
      int b = Integer.parseInt(sb, 16);

      System.out.println(a+b);
    }
  }
}

 

2000ASCII码排序


import java.util.Scanner;



public class Main{
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String str;
    char[] ch = new char[3];
    char temp;
    while(scan.hasNext()){
      str = scan.next();
      for(int i = 0;i < 3; i++)
        ch[i] = str.charAt(i);
      for(int j = 0;j < 3; j++){
        for(int i = 0;i < 2; i++)
          if(ch[i] > ch[i+1]){
            temp = ch[i];
            ch[i] = ch[i+1];
            ch[i+1] = temp;
          }
      }
      for(int i = 0; i < 2; i++)
        System.out.print(ch[i] + " ");
      System.out.println(ch[2]);
    }
  }
}

2001.计算两点间的距离



import java.text.DecimalFormat;
import java.util.Scanner;

public class Main_6 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      double x1 = sc.nextDouble();
      double y1 = sc.nextDouble();
      double x2 = sc.nextDouble();
      double y2 = sc.nextDouble();
      DecimalFormat df = new DecimalFormat(".00");
      System.out.println(df.format(Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
    }
  }
}

2005.第几天?            

package com.company;


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main_7 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
      String data = sc.next();
      int year = Integer.valueOf(data.substring(0, 4));
      int mon = Integer.valueOf(data.substring(5, data.lastIndexOf('/')));
      int day = Integer.valueOf(data.substring(data.lastIndexOf('/') + 1));
      Calendar cl = new GregorianCalendar(year, mon - 1, day);
      System.out.println(cl.get(Calendar.DAY_OF_YEAR));
    }
  }
}

2030 汉字统计


import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

/*
 * 	不是可显示字符的就是汉字
 */


      int n = sc.nextInt();
      sc.nextLine();
      while(n!=0) {
        n--;
        String str = sc.nextLine();
        int sum = 0;
        for (int i = 0; i < str.length(); i++) {
          if((int)str.charAt(i)<0 || (int)str.charAt(i)>127) {
            sum++;
          }
        }
        System.out.println(sum);
      }
  }
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值