CF852A Digits

CF852A Digits

Big brother next door yijian write a positive solution. I'll write a randomized Dafa, right?

We first consider a false greedy, each number is divided into one, so that the digital division and minimal. Although this is wrong, but we found that the probability of error is very small, so we can randomly each time a digital one or two numbers one. The latter is about one percent probability adjustment. We use this method to make the first division, and the remaining two have split each number a can. Finally, determine what the condition is satisfied, if not on the re-run again randomized.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<time.h>
#include<string>
#include<stdlib.h>
#define MAX 100
using namespace std;
char s[200500];
int ans[200500],cnt=0,n,nums;
int data[3][200500],add[3];
string output;
int rd() {
    int num=rand()%MAX;
    if(num==0)return 2;
    else return 1;
}
bool generate() {
    cnt=0,nums=0;output.clear();
    for(int i=0;i<n;i++) {
            if(i!=0)output.push_back('+');
            if(i!=n-1&&rd()==2) {
                ans[++cnt]=(s[i]-'0')*10+s[i+1]-'0';
                output.push_back(s[i]);output.push_back(s[i+1]);
                i++;
            }
            else {
                ans[++cnt]=s[i]-'0';
                output.push_back(s[i]);
            }
    }
    return 1;
}
void out() {
    cout<<output;
    printf("\n");
    for (int i = add[1]; i >=1; i--) {
        printf("%d", data[1][i]);
        if (i != 1)printf("+");
    }
    printf("\n");
    for (int i = add[2]; i >=1; i--) {
        printf("%d", data[2][i]);
        if (i != 1)printf("+");
    }
    return;
}
int main() {
    srand((unsigned)(time(NULL)));
    scanf("%d%s",&n,s);
    while(generate()) {
        long long tot=0,trans;add[0]=add[1]=add[2]=0;
        for(int i=1;i<=cnt;i++)tot+=ans[i];trans=tot;tot=0;
        while(trans){tot+=trans%10;data[1][++add[1]]=trans%10;trans/=10;}trans=tot;tot=0;
        while(trans){tot+=trans%10;data[2][++add[2]]=trans%10;trans/=10;}
        if(tot<=9){out();return 0;}
        else continue;
    }
}

Guess you like

Origin www.cnblogs.com/GavinZheng/p/11235672.html