递推递归专题 M 数值分解

    xiaoxiao2021-03-25  10

    1、简单描述

    多次测试,输入n,尽可能分解出多的3,如果最后为3和1,不如直接分解成4,最后输出分解的数字串和空格,输出一共多少个数字,以及这些数字的乘积。

    2、思路

    分循环条件写程序

    # include <bits/stdc++.h> using namespace std; int main() { int n; while(scanf("%d", &n) != EOF) { if(n == 1) { cout << "1 "<< endl; cout << "1 1"<< endl; continue; } ||一种特殊情况 int cnt = n / 3; ||计算会分解出多少个三,控制循环条件 int yu = n % 3; ||根据余数的不同分情况讨论 if(yu == 1) ||余数为一 { cnt--; ||退1 int sum = 4; for(int i = 0; i < cnt; i++) sum *= 3; for(int i = 0; i < cnt; i++) cout << 3 << " "; cout << 4 << " " << endl; cout << cnt + 1 << " " << sum << endl; } else { int sum = 1; for(int i = 0; i < cnt; i++) sum *= 3; if(yu == 0) ||余数为0 { for(int i = 0; i < cnt; i++) cout << 3 << " "; cout << endl; cout << cnt << " " << sum << endl; } else ||余数为2 { for(int i = 0; i < cnt; i++) cout << 3 << " "; cout << yu << " " << endl; cout << cnt + 1 << " " << sum * yu << endl; } } } return 0; } 找不同条件,分条件循环。

    转载请注明原文地址: https://ju.6miu.com/read-200346.html

    最新回复(0)