1、编一个程序求质数的和,例如F(7)=2+3+5+7+11+13 +17=58。(软件业巨无霸——微软笔试面试题目(1))
2、自己定义数据结构,写出程序:二叉树的前序遍历。(中国最重要的电信设备和全面电信解决方案供应商之一——阿尔卡特(中国)的面试题目)
3、编程输出以下格式的数据。(趣味题)
When i=0
1
When i=1
7 8 9
6 1 2
5 4 3
When i=2
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
4、编程输出以下格式的数据。(趣味题)
when i=1:
1
4 5 2
3
when i=2:
1 2
8 9 10 3
7 12 11 4
6 5
5、将一个N进制数转换成M进制数。(Kingsoft金山公司C/C++笔试题)
6、设计一函数,求整数区间[a,b]和[c,d]的交集。(Kingsoft金山公司C/C++笔试题)
7、编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。如输入2004年12月31日23时59分59秒,则输出2005年1月1日0时0分0秒
8、文件中有一组整数,要求排序后输出到另一个文件中。
9、有n个人,从第一个人开始报数,报到 m 的出列,再从下一个开始报数,直到最后一个人为幸运者,编程实现。
ANSWER:(个人愚见)
第一题:
#include <windows.h>
#include "iostream.h"
#include <math.h>
// 判断一个数是否为素数,只要用这个值依次除以2到小于它的开方数的最大整数,
// 如果其中有一个数可以整除,那么该值不为素数,反之为素数
BOOL IsPrimeNum(DWORD dwNum)
{
INT i=2,j=(INT)sqrt(dwNum);
while( i<=j ){
if( dwNum%i==0 ) break;
i++;
}
if( i>j )
return TRUE;
else
return FALSE;
}
void GetPrimeArray(DWORD dwPrimeNum,DWORD dwPrimeArr[],DWORD &dwPrimeSum)
{
dwPrimeSum=0;
DWORD i=0,j=2;
while( i<dwPrimeNum ){
if( IsPrimeNum(j) )
{
dwPrimeArr[i] = j;
dwPrimeSum += j;
i++;
}
j++;
}
}
void main()
{
DWORD dwNum,dwSum;
DWORD dwPrime[1000] = {0};
cout<<"Please input the number(0 quit):";
cin>>dwNum;
while( dwNum )
{
GetPrimeArray(dwNum,dwPrime,dwSum);
cout<<"F("<<dwNum<<")= "<<endl;
for(DWORD t=0; t<dwNum; t++)
{
cout<<dwPrime[t]<<"+";
if( (t+1)%10==0 )
cout<<endl;
}
cout<<"/b="<<dwSum<<endl<<endl;
cout<<"Please input the number(0 quit):";
cin>>dwNum;
}
}
第二题:
#include <cstdlib>
#include <iostream>
using namespace std;
struct BiTree ///< 声明二叉树的结构
{
int data;
BiTree *left;
BiTree *right;
};
// 插入节点值
BiTree* InsertNode(BiTree* pRoot,int node)
{
BiTree* pCurrNode; ///< 声明目前节点指针
BiTree* pParentNode; ///< 声明父亲接点指针
BiTree* pNewNode = new BiTree; ///< 建立新节点的内存空间
pNewNode->data=node; ///< 存入节点的内容
pNewNode->right=NULL; ///< 设置右指针的初值
pNewNode->left=NULL; ///< 设置左指针的初值
if(pRoot == NULL)
return pNewNode;
else{
pCurrNode = pRoot;
while(pCurrNode!=NULL){
pParentNode = pCurrNode;
(node<pCurrNode->data) ? pCurrNode = pCurrNode->left : pCurrNode =pCurrNode->right; ///< 寻找空的节点便插入
}
(pParentNode->data>node) ? pParentNode->left = pNewNode : pParentNode->right = pNewNode;
}
return pRoot;
}
// 建立二叉树
BiTree* CreatBiTree(int data[],int len)
{
BiTree* pRoot = NULL; ///< 根节点指针
for(int i=0; i<len; i++) ///< 建立树状结构
pRoot = InsertNode(pRoot,data[i]);
return pRoot;
}
// 打印二叉树
void PrintBiTree(BiTree* pRoot)
{
BiTree* pPointer = pRoot->left;
if( pPointer!=NULL )
printf("Left subtree node of root:/n");
while(pPointer!=NULL){
printf("[%2d]/n",pPointer->data);
pPointer = pPointer->left; ///< 指向左节点
}
pPointer = pRoot->right;
if( pPointer!=NULL )
printf("Right subtree node of root:/n");
while(pPointer!=NULL){
printf("[%2d]/n",pPointer->data); ///< 打印节点的内容
pPointer = pPointer->right; ///< 指向右节点
}
}
// 前序遍历
void PreOder(BiTree* pPointer)
{
if(pPointer!=NULL){
cout << pPointer->data<<" ";
PreOder(pPointer->left);
PreOder(pPointer->right); ///< 节点--左子树--右子树
}
}
// 释放资源
void Release(BiTree* pRoot)
{
if( pRoot==NULL )
return;
Release(pRoot->left);
Release(pRoot->right);
delete pRoot;
pRoot = NULL;
}
void main()
{
BiTree* pRoot = NULL;
int index=0,value;
int nodelist[100] = {0};
printf("Please input the elements of binary tree (Exit for 0):/n");
scanf("%d",&value);
while (value!= 0){
nodelist[index++] = value;
scanf("%d",&value);
}
pRoot = CreatBiTree(nodelist,index); ///< 建立二叉树
PrintBiTree(pRoot); ///< 打印二叉树节点的内容
cout <<"/nThe preoder travesal result is:"<<endl;
PreOder(pRoot);
cout<<endl<<endl;
Release(pRoot); ///< 释放资源
}
第三题
#include <fstream.h>
struct Tpoint{
int x;
int y;
};
Tpoint point[100000];
ofstream out("answer.text");
void fun(){
point[1].x=0;point[1].y=0;
point[2].x=1;point[2].y=0;
int num=2;
for(int i=1;i<=200;i=i+2){
for(int j=1;j<=i;j++){
num++;
point[num].x=point[num-1].x;
point[num].y=point[num-1].y-1;
}
for(j=1;j<=i+1;j++){
num++;
point[num].x=point[num-1].x-1;
point[num].y=point[num-1].y;
}
for(j=1;j<=i+1;j++){
num++;
point[num].x=point[num-1].x;
point[num].y=point[num-1].y+1;
}
for(j=1;j<=i+2;j++){
num++;
point[num].x=point[num-1].x+1;
point[num].y=point[num-1].y;
}
}
}
void output(int n){
for(int i=n;i>=-n;i--){
for(int j=-n;j<=n;j++){
for(int s=1;s<1000;s++)
if(point[s].x == j && point[s].y == i){
if(s<10) out<<" ";
if(s>=10 && s<100) out<<" ";
if(s>=100 && s<1000) out<<" ";
out<<s;
}
}
out<<endl;
}
}
void main(){
fun();
for(int i=0;i<16;i++){
out<<"When i="<<i<<endl<<endl;
output(i);
out<<endl;
}
}
第四题
#include <fstream.h>
struct Tpoint{
int x;
int y;
};
Tpoint point[10000];
ofstream out("answer.text");
void Interesting(int time){
point[1].x=2;point[1].y=-1;
int num=1;
for(int i=time+1;i>=0;i=i-2){
if(i==time-1) i++;
if( num > (time*4+time*time) ) break;
for(int j=1;j<=i;j++){
num++;
if( num > (time*4+time*time) ) break;
point[num].x=point[num-1].x+1;
point[num].y=point[num-1].y;
if(num==time+1){
point[num].x=point[num-1].x+1;
point[num].y=point[num-1].y-1;
break;
}
}
for(j=1;j<=i-1;j++){
num++;
if( num > (time*4+time*time) ) break;
point[num].x=point[num-1].x;
point[num].y=point[num-1].y-1;
if(num==2*time+1){
point[num].x=point[num-1].x-1;
point[num].y=point[num-1].y-1;
break;
}
}
for(j=1;j<=i-1;j++){
num++;
if( num > (time*4+time*time) ) break;
point[num].x=point[num-1].x-1;
point[num].y=point[num-1].y;
if(num==3*time+1){
point[num].x=point[num-1].x-1;
point[num].y=point[num-1].y+1;
break;
}
}
for(j=1;j<=i-2;j++){
num++;
if( num > (time*4+time*time) ) break;
point[num].x=point[num-1].x;
point[num].y=point[num-1].y+1;
}
}
for(int y=-1;y>=-(time+2);y--){
for(int x=1;x<=time+2;x++){
if( (x==1 && y==-1) || (x==time+2 && y==-1) ||
(x==1 && y==-(time+2)) || (x==time+2 && y==-(time+2)) ) out<<" ";
for(int s=1;s<1000;s++)
if(point[s].x == x && point[s].y == y){
if(s<10) out<<" ";
if(s>=10 && s<100) out<<" ";
if(s>=100 && s<1000) out<<" ";
out<<s;
}
}
out<<endl;
}
}
void main(){
for(int i=1;i<30;i++){
out<<"when i="<<i<<":"<<endl<<endl;
Interesting(i);
out<<endl;
}
}
第五题
#include<iostream.h>
#include<math.h>
#include<string.h>
int M2N_System(int M,int N,char *p,int a[])
{
if( M<2 || M>36 || N<2 || N>36 ) // 2~36进制
return -1;
char cTop = (char)(M<=9 ? M : (M+'A'-10)); // 为了检查输入
int i = 1,sum = 0;
int len = strlen(p);
while(*p!='/0')
{
if( *p>cTop ) // 出错退出
return -1;
if(*p>='0' && *p<='9')
sum += (int(*p)-48)*(int)pow(M,len-i);
if(*p>='A' && *p<='Z') // 用大写字母表示
sum += (int(*p)-55)*(int)pow(M,len-i);
i++;
p++;
}
for(i=0;sum>0;i++)
{
a[i] = sum%N;
sum = sum/N;
}
return --i;
}
void main()
{
char *x="555";
int M,N;
int a[64] = {0};
cout<<"请输入原数的进制和要转换的进制:";
cin>>M>>N;
int i = M2N_System(M,N,x,a);
while(i>=0)
{
if(a[i]<10)
cout<<a[i];
else
cout<<char(a[i]+'A'-10);
i--;
}
cout<<endl<<endl;
}
第六题
#include <iostream.h>
void main()
{
int a,b,c,d;
cout<<"请输入第一个整数区间:";
cin>>a>>b;
cout<<"请输入第二个整数区间:";
cin>>c>>d;
if( b<a || d<c ){
cout<<"输入错误!";
return;
}
if( b<c || d<a ){
cout<<"交集为空!";
return;
}
int left = (a>=c ? a : c);
int right = (b<=d ? b : d);
cout<<"交集为:["<<left<<","<<right<<"]"<<endl<<endl;
}
第七题
#include <stdio.h>
#include <stdlib.h>
/* define function */
void InputData (void);
int LeapYear (int year);
void NextSec (void);
/* month[0]: leap year month[1]: common year */
int Amonth[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int leap = 0;
int year, month, day, hour, minute, second;
/*
* Function Name: LeapYear ()
* Describe: judge the current year is leap year or no
* Paramete: int year: current year
* ReturnValue: 1: is leap year 0: no
*/
int LeapYear (int year)
{
if ((year % 4 == 0) || (year % 400 == 0) && (year % 100 != 0))
{
return 1;
}
return 0;
}
/*
* Function Name: InputData ()
* Describe: input data
* Paramete: void
* ReturnValue: void
*/
void InputData (void)
{
printf (" Input Year( Year>=1000 ): ");
if ((scanf ("%d", &year) != 1) || (year < 1000))
{
puts ("Input year error!");
getchar ();
exit (0);
}
leap = LeapYear (year);
printf (" Input Month( 1=<Month<=12 ): ");
if ((scanf ("%d", &month) != 1) || (month < 1) || (month > 12))
{
puts (" Input month error!");
getchar ();
exit (0);
}
printf (" Input Day( 1=<Day<=31 ): ");
if ((scanf ("%d", &day) != 1) || (day < 1) || (day > Amonth[leap][month]))
{
puts (" Input day error!");
getchar ();
exit (0);
}
printf (" Input Hour( 0=<Hour=<23 ): ");
if ((scanf ("%d", &hour) != 1) || (hour < 0) || (hour > 23))
{
puts (" Input hour error!");
getchar ();
exit (0);
}
printf (" Input Minute( 0=<Minute=<59 ): ");
if ((scanf ("%d", &minute) != 1) || (minute < 0) || (minute > 59))
{
puts (" Input minute error!");
getchar ();
exit (0);
}
printf (" Input Second( 0=<Second=<59 ): ");
if ((scanf ("%d", &second) != 1) || (second < 0) || (second > 59))
{
puts (" Input Second error!");
getchar ();
exit (0);
}
}
/*
* Function Name: NextSec ()
* Describe: count the next second by the current time
* Paramete: void
* ReturnValue: void
*/
void NextSec (void)
{
if (second != 59)
{
second += 1;
printf (" After second add 1: %d-%02d-%02d %02d:%02d:%02d /n", year, month, day, hour, minute, second);
return;
}
second = 0;
if (minute != 59)
{
minute += 1;
printf (" After second add 1: %d-%02d-%02d %02d:%02d:%02d /n", year, month, day, hour, minute, second);
return;
}
minute = 0;
if (hour != 23)
{
hour += 1;
printf (" After second add 1: %d-%02d-%02d %02d:%02d:%02d /n", year, month, day, hour, minute, second);
return;
}
hour = 0;
if (day != Amonth[leap][month])
{
day += 1;
printf (" After second add 1: %d-%02d-%02d %02d:%02d:%02d /n", year, month, day, hour, minute, second);
return;
}
day = 1;
if (month != 12)
{
month += 1;
printf (" After second add 1: %d-%02d-%02d %02d:%02d:%02d /n", year, month, day, hour, minute, second);
return;
}
month = 1;
year += 1;
printf (" After second add 1: %d-%02d-%02d %02d:%02d:%02d /n", year, month, day, hour, minute, second);
}
int main (void)
{
InputData ();
NextSec ();
return 0;
}
第八题
#include <iostream.h>
#include <afxwin.h>
#include <string.h>
#include<fstream>
#include <vector>
using namespace std;
void QuickSort(int nFrom,int nTo,vector<int>& array)
{
int nLeft = nFrom,nRight = nTo,nTemp;
int nMiddle = array[(nLeft+nRight)/2];
do{
while( array[nLeft]<nMiddle && nLeft<nTo ) nLeft++;
while( array[nRight]>nMiddle && nRight>nFrom ) nRight--;
if( nLeft<=nRight ){
nTemp = array[nLeft]; array[nLeft] = array[nRight]; array[nRight] = nTemp;
nLeft++;
nRight--;
}
} while (nLeft<=nRight);
if( nRight>nFrom )
QuickSort(nFrom,nRight,array);
if( nLeft<nTo )
QuickSort(nLeft,nTo,array);
}
void main( void )
{
vector<int> data;
ifstream in("c://data.txt");
ofstream out("c://result.txt");
if ( !in){
cout<<"file error!";
exit(1);
}
int temp;
while (!in.eof()){
in>>temp;
data.push_back(temp);
}
in.close(); //关闭输入文件流
QuickSort(0,data.size()-1,data);
if ( !out){
cout<<"file error!";
exit(1);
}
vector<int>::iterator myIterator;
for(myIterator=data.begin(); myIterator!=data.end(); ++myIterator)
out<<*myIterator<<" ";
// for (int i=0; i<data.size(); i++)
// out<<data.at(i)<<" ";
out.close(); //关闭输出文件流
}
第九题
通常的解法:
typedef struct Interest{
int data;
struct Interest *next;
}node;
void ShowData(node *pt,int num)
{
for(int i=1; i<=num; i++){
cout<<pt->data<<" ";
pt = pt->next;
}
cout<<endl;
}
void Interesting(node *pt,int n,int m)
{
ShowData(pt,n);
node *temp;
while( (n--)>1 ){
int num = m;
while( (num--)>2 ) // 移动指针到指定位置
pt = pt->next;
temp=pt->next;
pt->next=temp->next;
pt=temp->next;
delete temp;
ShowData(pt,n);
}
}
void main()
{
cout<<"Please input the number n and m: ";
int n,m;
cin>>n>>m;
node *p,*head=new node;
p=head;
for(int i=1;i<=n;i++){ // 创建链表
node *s=new node;
s->data=i;
p->next=s;
p=s;
s->next=head;
}
p->next=head->next;
node *pt=head->next;
Interesting(pt,n,m);
cout<<fun(n,m)<<endl;
}
数学解法:
此题为约瑟夫环问题
先把问题稍微改变一下,并不影响原意:
n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号
我们知道第一个人(编号一定是m%n-1) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m%n的人开始):
k k+1 k+2 ... n-2, n-1, 0, 1, 2, ... k-2
并且从k开始报0。
现在我们把他们的编号做一下转换:
k --> 0
k+1 --> 1
k+2 --> 2
...
...
k-2 --> n-2
k-1 --> n-1
变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:例如x是最终的胜利者,那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗?!!变回去的公式很简单,相信大家都可以推出来:x'=(x+k)%n
如何知道(n-1)个人报数的问题的解?对,只要知道(n-2)个人的解就行了。(n-2)个人的解呢?当然是先求(n-3)的情况 ---- 这显然就是一个倒推问题!好了,思路出来了,下面写递推公式:
令f表示i个人玩游戏报m退出最后胜利者的编号,最后的结果自然是f[n]
递推公式
f[1]=0;
f=(f[i-1]+m)%i; (i>1)
有了这个公式,我们要做的就是从1-n顺序算出f的数值,最后结果是f[n]。因为实际生活中编号总是从1开始,我们输出f[n]+1由于是逐级递推,不需要保存每个f,程序也是异常简单:
#include<iostream>
using namespace std;
int fun(int n, int m)
{
int i, r = 0;
for (i = 2; i <= n; i++)
r = (r + m) % i;
return r+1;
}
void main()
{
int i, m;
cin >> i >> m;
cout << fun( i, m );
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/CHRYSLER_300C/archive/2009/06/05/4244441.aspx