当前位置:网站首页>Sum of two numbers, the numbers in the array are converted to decimal, added, and output inversely

Sum of two numbers, the numbers in the array are converted to decimal, added, and output inversely

2022-07-05 06:36:00 Said it was all

public class  Sum of two numbers  {
    
    public static void main(String[] args) {
    
         Sum of two numbers  t=new  Sum of two numbers ();
        System.out.println(" Enter the first array , Whitespace separated ");
        int arr1[]=t.arr();
        int sum1=0;
        int sum2=0;
        for(int i=0;i<arr1.length;i++){
    
            sum1 = sum1+(int) (arr1[i]*(Math.pow(10,arr1.length-(i+1))));
        }
        System.out.println(" Enter the second array , Whitespace separated ");
        int arr2[]=t.arr();
        for ( int i=0;i<arr2.length;i++){
    
            sum2 = sum2+(int) (arr2[i]*(Math.pow(10,arr1.length-(i+1))));
        }
        int sum=sum1+sum2;
        String ssum=String.valueOf(sum);
        String sumarry[] = ssum.split("");
        for(int i=0;i<sumarry.length;i++){
    
            if(i==sumarry.length-(i+1)) break; // Odd critical condition 
            if(i>sumarry.length-(i+1)) break; // Even critical condition 
            String c=sumarry[sumarry.length-(i+1)];
            sumarry[sumarry.length-(i+1)] =sumarry[i];
            sumarry[i]=c;
        }
        for (String a:sumarry){
    
            System.out.print(a);
        }
    }
    public int[] arr (){
    
        Scanner sc = new Scanner(System.in);
        String inputString = sc.nextLine();
        String stringArray[] = inputString.split(" ");
        int intarr[]=new int[stringArray.length];
        for (int i=0;i<stringArray.length;i++){
    
            intarr[i]=Integer.parseInt(stringArray[i]);
        }
        return intarr;
    }
}

Example : Insert picture description here
Similar to the second question of Li Kou , But there is no linked list here .

原网站

版权声明
本文为[Said it was all]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140603261429.html