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

来源:互联网 发布:亚瑟士跑鞋推荐 知乎 编辑:程序博客网 时间:2024/06/05 15:28
#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;}

0 0
原创粉丝点击