360笔试题————计算器格式输出一个数的素因子乘积

这是一个C++程序,用于读取整数n,找出其素数因子,并以矩阵形式输出这些素因子的乘积。程序使用了动态数组存储素数因子,将计算结果转换为字符串,然后利用预定义的字符矩阵将其转换成矩阵展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int n;
//存放素数因子
int arr[1000000];

//数字矩阵
char number[10][5][3] =
{
    {' ','-',' ','|',' ','|',' ',' ',' ','|',' ','|',' ','-',' '},
    {' ',' ',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ',' ',' '},
    {' ','-',' ',' ',' ','|',' ','-',' ','|',' ',' ',' ','-',' '},
    {' ','-',' ',' ',' ','|',' ','-',' ',' ',' ','|',' ','-',' '},
    {' ',' ',' ','|',' ','|',' ','-',' ',' ',' ','|',' ',' ',' '},
    {' ','-',' ','|',' ',' ',' ','-',' ',' ',' ','|',' ','-',' '},
    {' ','-',' ','|',' ',' ',' ','-',' ','|',' ','|',' ','-',' '},
    {' ','-',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ',' ',' '},
    {' ','-',' ','|',' ','|',' ','-',' ','|',' ','|',' ','-',' '},
    {' ','-',' ','|',' ','|',' ','-',' ',' ',' ','|',' ','-',' '}
};

//乘号
char mul[5] = {' ',' ','*',' ',' '};


int main( )
{

    while(cin>>n)
    {
        int counter = 0;
        for(int i=2; i<n; i++)
        {
            while(n!=i)
            {
                if(n%i==0)
                {
                    arr[counter++] = i;
                    n=n/i;
                }
                else break;
            }
        }
        arr[counter++] = n;
        
        //中间字符串结果
        string result = "";
        for(int i = 0; i < counter; i++)
        {
            string str;
            char t[256];

            sprintf(t,"%d",arr[i]);
            str = t;
            if(i != counter - 1)
            {
                result += str + '*';
            }
            else
            {
                result += str;
            }
        }

        //转化为矩阵形式
        for(int o = 0; o < 5; o++)
        {
            for(int j = 0; j < result.length(); j++)
            {
                if(result[j] != '*'){
                    for(int q = 0;q < 3;q++){
                        cout << number[result[j] - '0'][o][q];
                    }
                }else{
                    cout << mul[o];
                }
            }

            cout << endl;
        }


    }



    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值