zoukankan      html  css  js  c++  java
  • PAT——1022. D进制的A+B

    输入两个非负10进制整数A和B(<=230-1),输出A+B的D (1 < D <= 10)进制数。

    输入格式:

    输入在一行中依次给出3个整数A、B和D。

    输出格式:

    输出A+B的D进制数。

    输入样例:

    123 456 8
    

    输出样例:

    1103

     1 package com.hone.basical;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 import java.util.Scanner;
     6 
     7 /**
     8  * 原题目:https://www.patest.cn/contests/pat-b-practise/1022
     9  * @author Xia
    10  * 超时版本
    11  * 
    12  */
    13 
    14 public class basicalLevel1022DecimalAB{
    15     public static void main(String[] args) {
    16         Scanner s = new Scanner(System.in);
    17         long a = s.nextLong();
    18         long b = s.nextLong();
    19         int D = s.nextInt();
    20         double c = a + b;
    21         List<Double> dnum = new     ArrayList<>();
    22         double x = 0;
    23         while (c!=0){
    24             x = c%D;
    25             dnum.add(x);
    26             c= (int)(c/D);
    27             if (c<D) {
    28                 dnum.add(c);
    29                 break;
    30             }
    31         }
    32         for (int i = dnum.size()-1; i >= 0; i--) {
    33             System.out.print((int)(dnum.get(i)/1.0));
    34         }
    35     }
    36 }  
     1 package com.hone.basical;
     2 
     3 import java.util.Scanner;
     4 
     5 /**
     6  * 原题目:https://www.patest.cn/contests/pat-b-practise/1022
     7  * @author Xia
     8  * 核心:做了这么多题目,建议所有的进制转换都可以字符串来承接最后转化的效果
     9  */
    10 
    11 public class basicalLevel1022DecimalABImprove{
    12     public static void main(String[] args) {
    13         Scanner s = new Scanner(System.in);
    14         long a = s.nextLong();
    15         long b = s.nextLong();
    16         int D = s.nextInt();
    17         long c = a + b;
    18         String num = "";            //处理成字符串是这种问题常见的方式,因为更加容易拼接
    19         if (c == 0) {
    20             num = num+"0";
    21         }else {
    22             while (c!=0) {
    23                 int ref = (int) (c%D);
    24                 num = ref+num;
    25                 c = c/D;
    26             }
    27         }
    28         System.out.println(num);
    29     }
    30 }  




  • 相关阅读:
    c语言知识
    数字地与模拟地
    C语言实现顺序表(增删)
    传统数据库、Nosql数据库与云数据库区别?
    大数据处理架构如何
    warning: implicit declaration of function 'func1' [-Wimplicit-function-declaration]
    window10创建virtualenv虚拟环境
    二叉树的实现以及三种遍历方法--代码
    损失函数--KL散度与交叉熵
    市场回测与对冲套利
  • 原文地址:https://www.cnblogs.com/xiaxj/p/7978866.html
Copyright © 2011-2022 走看看