当前位置: 代码迷 >> 综合 >> HDOJ 1230 火星A+B
  详细解决方案

HDOJ 1230 火星A+B

热度:20   发布时间:2023-10-21 19:39:20.0

HDACM1230

思路:直接把相应位置的值相加,然后在进位,最后把不必要的前缀0去掉,然后输出(输出注意格式)

import java.util.Scanner;public class Main{
    public static void main(String[] args) {
// boolean prime[] = new boolean[200];
// for (int i = 2; i < prime.length; i++) {
    
// if (!prime[i]) {
    
// for (int j = i*2; j < prime.length; j += i) {
    
// prime[j] = true;
// }
// }
// }
// int j = 0;
// for (int i = 2; i < prime.length; i++) {
    
// if (!prime[i]) {
    
// j++;
// System.out.print(i+",");
// if (j==26) {
    
// break;
// }
// }
// }int mars[] = {
   2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};Scanner sc = new Scanner(System.in);while (sc.hasNext()) {String s1 = sc.next();String s2 = sc.next();String[] a = s1.split(""+',');String[] b = s2.split(""+',');if ("0".equals(a[0])&&"0".equals(b[0])) {break;}int sum[] = new int[26];int j = 0;for (int i = a.length-1; i>=0; i--) {sum[j++] += Integer.parseInt(a[i]);}j = 0;for (int i = b.length-1; i>=0; i--) {sum[j++] += Integer.parseInt(b[i]);}int c = a.length > b.length ? a.length : b.length;for (int i = 0; i < c+1; i++) {if(sum[i]>=mars[i]){sum[i] -= mars[i];sum[i+1]++;}}int i = c;while (sum[i]==0) {i--;}while (i>0) {System.out.print(sum[i]+",");i--;}System.out.println(sum[0]);}sc.close();}
}