当前位置:网站首页>1317. convert an integer to the sum of two zero free integers

1317. convert an integer to the sum of two zero free integers

2022-06-27 05:55:00 Mr Gao

1317. Convert an integer to the sum of two nonzero integers

「 Zero free integer 」 Is in the decimal representation It doesn't contain any 0 The positive integer .

Give you an integer n, Please return one A list of two integers [A, B], Satisfy :

A  and  B  Are zero free integers 
A + B = n

The problem data ensures that there is at least one effective solution .

If there are multiple effective solutions , You can return any of them .

Example 1:

Input :n = 2
Output :[1,1]
explain :A = 1, B = 1. A + B = n also A and B The decimal representation of does not contain any 0 .

Example 2:

Input :n = 11
Output :[2,9]

Example 3:

Input :n = 10000
Output :[1,9999]

Example 4:

Input :n = 69
Output :[1,68]

Example 5:

Input :n = 1010
Output :[11,999]

This question is comparatively simple , The solution code is as follows :

/** * Note: The returned array must be malloced, assume caller calls free(). */
bool f(int n){
    
    while(n){
    
        if(n%10==0){
    
            return false;

        }
        n=n/10;
    }
    return true;
}


int* getNoZeroIntegers(int n, int* returnSize){
    
    int *re=(int *)malloc(sizeof(int)*2);
    int i;
    for(i=1;i<n;i++){
    
        if(f(n-i)&&f(i)){
    
             re[0]=i;
            re[1]=n-i;
 
            *(returnSize)=2;
                return re;

        }
    }
    return re;
   

}
原网站

版权声明
本文为[Mr Gao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270540107986.html