1.输入n个数 输出每个数的阶乘的末尾连续0的个数?
如:
输入:3 6 27 200
输出:0 1 6 49
2.输入n个字符串,输出这些字符串的最长匹配子串;
如:
输入:abcdefgfgf
a39dacn,cdefgeIo?
a3 dcnn,wwcdefg.
输出:cdefg
[此贴子已经被作者于2007-5-10 18:28:15编辑过]
第一个:
#include <iostream>
using namespace std;
#define N 20
#define M 1000
int num[N],j=0;
int f(int n,int a=0)
{
if(n%5==0)
f(n/5,a+1);
else
return a;
}
bool get()
{
char a[M];
cin.getline(a,M);
int i=0;
while(a[i]!='\0')
{
if(a[i]!=' ')
{
if(a[i]<'0'||a[i]>'9')
{
cout<<"wrong"<<endl;
return false;
}
else
num[j]=num[j]*10+(a[i]-'0');
}
else
j++;
i++;
}
j++;
return true;
}
int main()
{
int t=0,i;
if(get())
{
for(int n=0;n<j;n++)
{
t=0;
for(i=5;i<=num[n];i+=5)
t+=f(i);
cout<<t<<' ';
}
cout<<endl;
}
system("pause");
}
#include <iostream>
#include <cmath>
using namespace std ;
// To get the size of the largest number.
int InspectSize( int &Nbr ){
int size = 0 ;
int CompareNbr = 1 ;
while ( Nbr/CompareNbr != 0 ){
++size ;
CompareNbr = CompareNbr * 10 ;
}
return size ;
}
int main(){
int Nbr ;
cout << "Please type in " << endl ;
cin >> Nbr ;
// Get the numbersize
int size = InspectSize( Nbr ) ;
// To know how many fives.
int fives = 5 ;
// To be clear about when to stop
int MaxNbr = pow ( 10, size ) ;
// Record the number of zeros.
int Num = 0 ;
while( fives < MaxNbr ){
Num = Num + Nbr / fives ;
fives = 5 * fives ;
}
cout << "There's " << Num << " zeros" << endl ;
return 0;
}
有多少个5就有多少个0,
同时注意它们是阶乘..
[此贴子已经被作者于2007-4-27 22:46:10编辑过]