XX公司在线笔试题编程题之一

题目:

#include <iostream>
#include <vector>
#include <string>
#include <list>
#define N 1000005
using namespace std;

bool prime[N]={0};
vector<int> ps;  // 存放1到N内所有素数

void fenjie(vector<int> & suyinzi, int x){  // 素因子分解
	for(int i=0;i<ps.size();i++){
		while(x % ps[i] ==0){
			suyinzi.push_back(ps[i]);
			x /=ps[i];
		}
	}

}

char str[5][31]={
	"*-*****-**-*****-**-**-**-**-*",
	"|*|*|***|**||*||**|****||*||*|",
	"*******-**-**-**-**-*****-**-*",
	"|*|*|*|****|**|**||*|**||*|**|",
	"*-*****-**-*****-**-*****-**-*"
};
char print[5][4*1000]={0}; // 打印缓冲区

 
void set(int col, char ch){  //从col列开始设置要显示的字符
	if(ch == '*'){
		for(int i=0;i<5;i++) print[i][col] = ' ';
		print[2][col] = '*';
		return;
	}
	for(int i =0;i<5;i++) 
		for(int j=0;j<3;j++){
			char temp=str[i][(ch-'0')*3 + j];
			print[i][col+j] = temp == '*' ? ' ' : temp;
		}
	 
}
void set(int col, string str){ ///从第col列开始,设置要显示的字符串
	set(col,'*');
	col ++;
	for(int i=0;i<str.size();i++){
		set(col,str[i]);
		col += 3;
	}

}
string convert(int x){  //将整数转为字符串
	list<char> li;
	while(x!=0){
		li.push_front(x%10 + '0');
		x /= 10;
	}
	string str(li.begin(),li.end());
	return str;
	
}
int main()
{	 
	for(int i=2;i<1000;i++){
		if(prime[i]) {
			continue;
		}		 
		for(int j=i+i;j<N;j+=i)
			prime[j] =1;
	}
	for(int i =2;i< N;i++ ) if(!prime[i]) ps.push_back(i);	
	int x;
	while(cin >> x){
		vector<int> suyinzi;
		vector<string> vstr;
		fenjie(suyinzi,x);
		for(int i=0;i<suyinzi.size();i++){
			vstr.push_back(convert(suyinzi[i]));
		}

		int count = suyinzi.size(),col=0;
		::memset(print, 0,sizeof(print));
		for(int i=0;i<vstr.size();i++){
			set(col,vstr[i]);
			col += vstr[i].size()*3+1;
		}
		
		for(int i=0;i<5;i++){
			for(int j=1;j<col;j++)
				cout << print[i][j];
			cout << endl;
		}
 
	}
	return 0;
}

  

posted on 2015-09-15 21:53  gaoyanqing  阅读(422)  评论(0编辑  收藏  举报

导航