vijos P1033 整数分解(版本2)

来源:互联网 发布:拼多多提现网络繁忙 编辑:程序博客网 时间:2024/05/22 20:07

~~~题目链接~~~


思路:

3 = 1+24 = 2+25 = 2+36 = 3+37 = 2+2+38 = 2+3+39 = 3+3+310 = 2+2+3+3..........
通过分解发现把一个数分解成尽可能多的2与3就为最大值。


code:

#include <stdio.h>#include <string.h>#include <math.h>using namespace std;struct node{    int num[1002];    int cnt;}p;void multipy(int c, int pow, int n){    int i = 0, cnt = p.cnt;    if(c >= n) return ;    for(i = 0; i<=cnt; i++)        p.num[i] *= pow;    for(i = 0; i<cnt; i++)        if(p.num[i]>9) p.num[i+1] += p.num[i]/10, p.num[i] = p.num[i]%10;    if(p.num[i]>9) p.num[i+1] += p.num[i]/10, p.num[i] = p.num[i]%10, cnt = i+1;    p.cnt = cnt;    multipy(c+1, pow, n);}int main(){    //freopen("input.txt", "r", stdin);    int i = 0, j = 0, n = 0, x = 0, y = 0;    scanf("%d", &n);    if(n == 1) printf("1\n");    else    {        x = n/3;        y = (n%3)/2;        if(n%3%2) x--, y += 2;        p.cnt = 0;        memset(p.num, 0, sizeof(p.num));        p.num[0] = 3;        multipy(0, 3, x-1);        if(x <= 0) p.num[0] = 2, y--;        multipy(0, 2, y);        for(i = p.cnt; i>-1; i--)            printf("%d", p.num[i]);        printf("\n");    }    //printf("%.0lf", pow(3, x)*pow(2, y));    return 0;}


原创粉丝点击