Additive Number 306

题目链接:https://leetcode.com/problems/additive-number/

题目描述:一串数字是Additive number,必须存在一个划分将这一串数字分成n个数字,n必须大于3,并且除了前两个数字之外,每个数字都是前两个数字之和,此外,不存在包含前导零的数字。

题目分析:深度搜索

代码实现:

#include <map>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <stack>

using namespace std;

bool judge(string a, string b, string sum) {
    //cout<<a<<", "<<b<<", "<<sum<<endl;
    int lena = a.length();
    int lenb = b.length();
    int lens = sum.length();

    if (lens < lena || lens < lenb) return 0;
    int c = 0;
    while(lena > 0 && lenb > 0) {
        int tmp = a[lena-1] + b[lenb-1] - ‘0‘ - ‘0‘ + c;
        c = tmp / 10;
        tmp = tmp % 10;
        if (sum[lens-1] != tmp+‘0‘) return 0;
        lena--;
        lenb--;
        lens--;
    }

    while(lena > 0) {
        int tmp = a[lena-1] + c -‘0‘;
        c = tmp / 10;
        tmp = tmp % 10;
        if (sum[lens-1] != tmp+‘0‘) return 0;
        lena--;
        lens--;
    }

    while(lenb > 0) {
        int tmp = b[lenb-1] + c - ‘0‘;
        c = tmp /10;
        tmp = tmp %10;
        if (sum[lens-1] != tmp+‘0‘) return 0;
        lenb--;
        lens--;
    }

    if ((c==0 && lens==0) || (lens==1 && sum[0]==c+‘0‘))
        return 1;

    return 0;
}

bool df(string a, string b, string num) {
    int lena = a.length();
    int lenb = b.length();
    int lenn = num.length();
    int len = max(lena, lenb);
    if (!lenn) return 1;
    if (len > lenn) return 0;
    if ((a[0] == ‘0‘ && lena>1) || (b[0] == ‘0‘ && lenb>1) || (num[0] == ‘0‘ && lenn>1)) return 0;

    bool ret = judge(a, b, num.substr(0, len));
    //cout<<a<<", "<<b<<", "<<num.substr(0, len)<<", ret:"<<ret<<endl;
    if (ret) {
        if (df(b, num.substr(0, len), num.substr(len)))
            return 1;
    }

    if (len+1 > lenn) return 0;

    ret = judge(a, b, num.substr(0, len+1));
    //cout<<a<<", "<<b<<", "<<num.substr(0, len+1)<<", ret:"<<ret<<endl;
    if (ret) {
        if (df(b, num.substr(0, len+1), num.substr(len+1)))
            return 1;
    }

    return 0;
}

bool isAdditiveNumber(string num) {
    int len = num.length();
    for (int i = 0; i < len-2; i++) {
        for (int j = i+1; j < len-1; j++) {
            if (df(num.substr(0, i+1), num.substr(i+1, j-i), num.substr(j+1)))
                return 1;
        }
    }

    return 0;
}

int main()
{
    string s;
    while(cin>>s) {
        cout<<s<<" is "<<isAdditiveNumber(s)<<endl;
    }
    return 0;
}
时间: 2024-12-25 23:38:04

Additive Number 306的相关文章

306. Additive Number

Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two. For e

[leetcode] 306. Additive Number 解题报告

题目链接: https://leetcode.com/problems/additive-number/ Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the

Leetcode 306. Additive Number

Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two. For e

306. Additive Number java solutions

Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two. For e

306 Additive Number 加法数

Additive number is a string whose digits can form additive sequence.A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.For exa

[LeetCode][JavaScript]Additive Number

Additive Number Additive number is a positive integer whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum o

Leetcode: Additive Number

Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two. For e

Additive Number

没啥好说的,注意用long(明明题目里说integer,汗) public class Solution { public boolean isAdditiveNumber(String num) { int length = num.length(); int i = 1; while (i < length && Long.valueOf(num.substring(0, i)) <= Integer.MAX_VALUE) { int j = i + 1; while (j

QNX 多线程 (线程1每隔20ms读取 number;线程2每隔10ms计算一次)

#include <pthread.h>#include <stdio.h>#include <sys/time.h>#include <string.h>#include <unistd.h>#define MAX 10 pthread_t thread[2];pthread_mutex_t mut;int number=0,i; void *thread1(){while(1){number=i;printf ("Thread1: