2009年校程序设计大赛五个题目

// Test_TianMo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "stdio.h"

#include "math.h"

#include "iostream.h"

void test_menu()
{
 cout<<endl<<endl<<endl;
 cout<<"***************************程序题目******************************"<<endl;
 cout<<endl;
 cout<<"     1  打印星形         "<<"  2  统计字符串中0-9数字的个数      "<<endl;
 cout<<endl;
 cout<<"     3  打印原始数据编号 "<<"  4  测试数字由素数相乘             "<<endl;
 cout<<endl;
 cout<<"     5  循环追击问题"<<"       0  退出系统                       "<<endl;
 cout<<endl;
 cout<<"*****************************************************************"<<endl;
 cout<<endl;
 cout<<"                                      电信10709 tianmo 2010-09-27"<<endl;
}

void test_one()
{
 int i,j,n; 
 while(1)
 {
  cout<<"input a number :";
  cin>>n;
  if (n%2==0)
  {
   cout<<"请输入一个正奇数!"<<endl;
  } 
  else
  {
   break;
  }
 }
 for (i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   if (i<n/2)
   {
    if ((j>=-i+n/2) && (j<=i+n/2))
    {
     cout<<"*";
    } 
    else
    {
     cout<<" ";
    }
   } 
   else
   {
    if ((j>=i-n/2) && (j<=-i+n/2+n-1))
    {
     cout<<"*";
    } 
    else
    {
     cout<<" ";
    }
   }
  }
  cout<<endl;
 }
 

}

void test_two()
{ 
 int i,nNum;  
 char nChar;
 int *aa=new int[10];
 printf("请输入一组测试字符串:");
 for(i=0;i<10;i++)
 {
  aa[i]=0;
 }
 while (1)
 {
  nChar=getchar();
  if (nChar==10)
   {
    break;
   }
  nNum=nChar-48;
  for (i=0;i<=9;i++)
  {
   if (nNum==i)
   {
    aa[i]++;
   }
   
  }
 }
 for (i=0;i<=9;i++)
 {
  if (aa[i]!=0)
  {
   cout<<i<<aa[i]; 
  }
  
 }
 cout<<endl;
 
}
void test_three()
{
 
 int i,j,t,k,nCount=0;
 char c;
 int *a =new int[];
 int *aa =new int[]; 
 printf("请输入一组测试数字:");
 while(1)
 {
  c = getchar();
  if (c == 10)
  {
   break;
  }
  if (c!=' '&& (c>=48&&c<=57))
  {
   a[nCount]=c-48;
   aa[nCount]=c-48;
   nCount++;
  }
 
 }
 //冒泡法排序
/* for (i=0;i<nCount-1;i++)
 {
  for (j=0;j<nCount-i-1;j++)
  {
   if (aa[j]>aa[j+1])
   {
    t=aa[j];
    aa[j]=aa[j+1];
    aa[j+1]=t;
   }
  }
 }
*/
 //选择法排序
 for (i=0;i<nCount-1;i++)
 {
  k=i;
  for (j=i+1;j<nCount;j++)
  {
   if (aa[k]>aa[j])
   {
    k=j;
   }
  }
  if(k != i)
  {
   t=aa[i];
   aa[i]=aa[k];
   aa[k]=t;
  }
 }
 cout<<endl;
 for (j=0;j<nCount;j++)
 {
  for (i=0;i<nCount;i++)
  {
   if (aa[j]==a[i])
   {
    cout<<i+1<<" ";
   }
  }
 }
 cout<<endl;
}

void test_four()
{
 int i,j,max,n,nCount=0;
 int t,p,tag,nTag;
 bool b;
 cout<<"please input a number of testing:";
 cin>>n;
 int *a =new int[n];
 for (i=0;i<n;i++)
 {
  cin>>a[i];
 }
 max =a[0];
 for (i=1;i<n;i++)
 {
  if (a[i]>max)
  {
   max = a[i];
  }
 }
 int *aa =new int[max];
 for (i=0;i<max;i++)
 {
  aa[i]=0;
 }
 for (j=2;j<=max;j++)
 {
  b=false;
  for (i=2;i<=sqrt(j);i++)
  {
   if (j%i==0)
   {
    b=true;
    break;
   }
  }
  if (b==false)
  {
   aa[nCount++]=j;   
  }
 }
 i=0;
 while(i<n)
 {
  nTag=0;
  t=1;
  tag=0;  
  cout<<a[i]<<" = ";
  for (j=0;j<nCount;j++)
  {
   p=1;
   while (1)
   {
    if (a[i]%int(pow(aa[j],p))==0)
    {
     p++;
     nTag++;
     if (nTag==1)
     {
      cout<<aa[j];
     } 
     else
     {
      cout<<" * "<<aa[j];
     }
     t=t*aa[j];
     if (t==a[i])
     {
      tag=1;
      break;
     }
    } 
    else
    {
     break;
    }
    
   }
   if (tag==1)
   {
    break;
   }
  }
  cout<<endl;
  i++;
 }
 cout<<endl;
}

void test_five()
{
 int i,j,nCount,k=0,t=0;
 int x,y,m,n,l;
 int xNum,yNum;
 cout<<"please input the number:";
 cin>>x>>y>>m>>n>>l;
 if(x==y)
 {
  cout<<"开始起点时候相遇!"<<endl;

 }
 else
 {
  nCount = 1;
  while(1)
  {
   j=1;
   while(j<=m)
   {
    j++;
    k=(k+1)%l;
   }
   xNum=(x+k+1)%l;
   i=1;
   while (i<=n)
   {
    i++;
    t=(t+1)%l;    
   }
   yNum=(y+t+1)%l;
   if (xNum == yNum)
   {
    cout<<"相遇:"<<nCount;
    break;
   }
   if (xNum==x || yNum==y)
   {

    cout<<"不能相遇!"<<endl;
    break;
   }
   nCount++;
   
  }
 }
 cout<<endl;

}


int main(int argc, char* argv[])
{
 int n;
 while (1)
 {
  test_menu();
  cout<<endl<<"请选择:";
  cin>>n;
  if (n==0)
  {
   break;
  }
  switch(n)
  {
  case 1:
   test_one();
   break;
  case 2:
   test_two();
   break;
  case 3:
   test_three();
   break;
  case 4:
   test_four();
   break;
  case 5:
   test_five();
   break;
  default:cout<<endl<<"您的选择有误,请重新选择!";
   break;
  }

 }
 
 return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值