Problem Statement for TheLuckySum

        Problem Statement for TheLuckySum Problem Statement             John thinks 4 and 7 are lucky digits, and all other digits are not lucky. A lucky number is a number that contains only lucky digits in decimal notation. Some numbers can be represented as a sum of only lucky numbers. Given an int n, return a int[] whose elements sum to exactly n. Each element of the int[] must be a lucky number. If there are multiple solutions, only consider those that contain the minimum possible number of elements, and return the one among those that comes earliest lexicographically. A int[] a1 comes before a int[] a2 lexicographically if a1 contains a smaller number at the first position where they differ. If n cannot be represented as a sum of lucky numbers, return an empty int[] instead.     Constraints -        n will be between 1 and 1,000,000,000, inclusive. Examples

0)                    11 Returns: {4, 7 } It is simple: 11 = 4 + 7.

1)                    12 Returns: {4, 4, 4 } Now we need three summands to get 12.

2)                    13 Returns: { } And now we can not get 13 at all.

3)                    100 Returns: {4, 4, 4, 44, 44 }

Solution

I got a idea,every lucky num is consisted of several 4s or/and several 7s,so i think may be we can resolve it by this. Below is the my code,

getSingleLuckArray() is get lucky int [] with one lucky digit.
getLuckArray is get lucky int [] with two lucky digits, and it will invoke getSingleLuckArray(),
maybe it will get lots of int[] arrays, but we just get the array with fewest nums.

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;


public class LuckySum {

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        int [] result = getLuckArray(2800);
        System.out.print('{');
        if(result != null)
        {
            for (int i = 0; i < result.length; i++) {
                System.out.print(result[i]);
                if(i!= result.length-1)
                {
                    System.out.print(',');
                }
            }
        }
        System.out.print('}');
    }

    public static int[] getLuckArray(int sum)
    {
        int [] retVal = null;
        int countOf4s = sum/4, countOf7s = sum/7;

        ArrayList
  
   result = null, temp = null;
        for (int i = countOf7s; i >=0; i--) 
        {
            for(int j=0; j<=countOf4s; j++)
            {
                if((i*7 + j*4) == sum)
                {
                    temp = new ArrayList
  
  ();
                    temp.addAll(getSingleLuckArray(i*7, 7));
                    temp.addAll(getSingleLuckArray(j*4, 4));
                    if(result == null)
                    {
                        result = temp;
                    }
                    else
                    {
                        if(result.size() > temp.size())
                        {
                            result = temp;
                        }
                    }
                }
            }
        }
        if(result != null)
        {
            retVal = new int[result.size()];
            for (int i = 0; i < retVal.length; i++) {
                retVal[i] = result.get(i);
            }
            Arrays.sort(retVal);
        }
        return retVal;
    }

    public static ArrayList getSingleLuckArray(int sum, int luckDiginal)
    {
        int countOfLuckNum = sum / luckDiginal;

        int bitCount = Integer.toString(countOfLuckNum).length();
        ArrayList
  
   al = new ArrayList
  
  ();
        int bitValue = 0;
        for(int i=bitCount; i>0; i--)
        {
            bitValue = getLuckNums(i, luckDiginal);
            while(true)
            {
                if(bitValue <= sum)
                {
                    al.add(bitValue);
                    sum = sum - bitValue;
                }
                else
                {
                    break;
                }
            }
        }

        return al;
    }

    public static int getLuckNums(int bitCount, int luckDiginal)
    {
        int luckNums = 0;
        StringBuffer sb = new StringBuffer();
        for(int i=bitCount; i>0; i--)
        {
            sb.append("1");
        }
        luckNums = Integer.parseInt(sb.toString()) * luckDiginal;

        return luckNums;
    }

}


It will print out "{4,7,7,7,444,777,777,777}"

Here is an example MATLAB code for simulating a 2D FDTD problem: ``` % Define simulation parameters dx = 0.01; % Spatial step size dt = 0.0001; % Time step size tmax = 1; % Maximum simulation time L = 1; % Length of simulation domain W = 1; % Width of simulation domain x = 0:dx:L; % Spatial grid y = 0:dx:W; T = 0:dt:tmax; % Time grid c = 1; % Wave speed % Define source waveform f0 = 1e9; % Center frequency of Gaussian pulse t0 = 0.5e-9; % Width of Gaussian pulse A = 1; % Amplitude of Gaussian pulse source = A*exp(-((T-t0).^2)/(2*t0^2)).*sin(2*pi*f0*(T-t0)); % Initialize electric and magnetic fields Ez = zeros(length(x),length(y),length(T)); Hy = zeros(length(x),length(y),length(T)); % Main FDTD loop for n = 1:length(T) % Update magnetic field for i = 1:length(x)-1 for j = 1:length(y)-1 Hy(i,j,n+1) = Hy(i,j,n) + dt/(mu*dx)*(Ez(i,j+1,n) - Ez(i,j,n) - Ez(i+1,j,n) + Ez(i+1,j+1,n)); end end % Update electric field for i = 2:length(x)-1 for j = 2:length(y)-1 Ez(i,j,n+1) = Ez(i,j,n) + dt/(eps*dx)*(Hy(i,j,n+1) - Hy(i,j-1,n+1) - Hy(i-1,j,n+1) + Hy(i-1,j-1,n+1)); end end % Add source to electric field Ez(1,round(length(y)/2),n+1) = Ez(1,round(length(y)/2),n+1) + source(n); end % Plot results figure; for n = 1:length(T) surf(x,y,Ez(:,:,n)'); shading interp; xlabel('x'); ylabel('y'); zlabel('Ez'); title(['Time = ',num2str(T(n))]); axis([0 L 0 W -1 1]); pause(0.01); end ``` This code simulates a 2D FDTD problem in a rectangular domain with a Gaussian pulse source at one edge. The electric and magnetic fields are updated using the FDTD method, and the results are plotted as a 3D surface over time.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值