注册 登录
编程论坛 C++教室

两道小题

abc888 发布于 2007-04-24 22:53, 1348 次点击

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编辑过]

29 回复
#2
fish_2007-04-25 21:08
漂亮!
第一个算法:
#include<stdio.h>
#include<iostream.h>
int main(void)
{
int a=0;
int b=0;
cout<<"put anumber:\n"<<endl;
cin>>a;
for(int i=5;i<=a;i+=5)
{
b=b+1;
}
cout<<b<<endl;
return 0;
}
我觉得这个考察的是5的倍数的个数,一个就一个0,、
这个程序只是测试,
过几天把程序改进一下!·
第二个我还没想好!

[此贴子已经被作者于2007-4-25 21:13:22编辑过]

#3
smartwind2007-04-26 14:22

第一个:
#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");
}

#4
TenY2007-04-27 11:15
LZ是不是写错了?
200的时候应该是49才对吧?
#5
abc8882007-04-27 21:38
TenY说说你的算法,为什么200应该是49
#6
TenY2007-04-27 22:41

#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编辑过]

#7
aipb20072007-04-27 23:29

晕,有个溢出问题!

[此贴子已经被作者于2007-4-27 23:39:28编辑过]

#8
aipb20072007-04-28 09:57
[CODE]int count0(int ival){
int count = 0;
for (;ival >= 5;--ival){
for (int temp = ival;(temp % 5) == 0;temp /= 5)
++count;
}
return count;
}[/CODE]

第一个!

200的时候是49!
#9
fantsical2007-04-28 10:06
#10
yucarl2007-04-28 12:31
fish的算法我觉得有bug

比如25的时候就应该算2个0了。。。
#11
fish_2007-04-28 13:40

对 没考虑 不好意思!再试一试!

#12
fish_2007-04-28 14:25
再试一试!

[此贴子已经被作者于2007-4-28 15:11:33编辑过]

#13
海子2007-04-28 18:55
#include<iostream.h>
void main()
{
int i,a,b,n = 0;
cout<<"enter the num to test:"<<endl;
cin>>a;
for(i = 1;i < a; i++ )
b = b*i;
for(; b/10 = b %10 ;)
{
n ++;
}
cout<<" zero numbers:"<<n<<endl;
}
简单的一个数时的情况,可以相应的改进
#14
海子2007-04-28 18:57
第二个问题是数据结构中的模式匹配问题,可以参考一下数据结构数
#15
fish_2007-04-29 13:34


#include<stdio.h>
#include<iostream.h>
int c;
int static t=0;
int static y=0;
int main(void)
{
cout<<"please enter a number:"<<endl;
cin>>c;
int w=c;
while(w/5)
{
t=t+1;
w=w/5;
}

for (int i=5;i<=c;i=i+5)
{
int a=i;
int l=1;
for(int u=1;u<=t;u++)
{

l=l*5;
if(a%(l)==0)
{y=y+1; }
else
{}
}

}
cout<<y<<endl;

return 0;
}
学校垃圾!老是停电,搞得都没法弄了!
刚搞出来,呵呵!

#16
nuciewth2007-04-29 15:45
有循环除.
count=1;
for(i=5;i<=n;i+=5)
{
num=i;
while(num%5==0)
{
count++;
num/=5;
}
}
if(n<5)count--;
#17
abc8882007-04-29 23:01
fish_对了!
#18
abc8882007-04-29 23:14

我的算法
#include <iostream.h>
void main()
{
int a,count;
cout<<"please input a number:"<<endl;
cin>>a;
count=0;
while(a/5)
{
count=count+a/5;
a=a/5;
}
cout<<count<<endl;
}

#19
fish_2007-04-30 22:49

后面一题有点复杂 我用vb。net写过但是c++就有点麻烦了!
这几天抽空写写

#20
fish_2007-05-03 13:02

闲来没有事,试着用C++写了写!
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string a,beg,max;
cout<<"enter a string:";
cin>>a;
int b=a.length() ;

for(string::size_type i=0;i<b;i++)
{

if(max.length ()<beg.length ())
{
max=beg;
}
else
{
max=max;
}

for(string::size_type dong=i+1;dong<=b;dong++)
{
if(max.length ()<beg.length ())
{
max=beg;
}
else
{

max=max;
}
if(a==a[dong])
{

beg=a;
for(string::size_type dan=1;dan<dong-i;dan++)

{
if(a[i+dan]==a[dong+dan])
{
beg+=a[i+dan];
}
else

{
goto begin;
}
}
}
else
{
}
begin:
int o=o+1;
}
}


cout<<max<<'\n';
return 0;
}
在vc++6.0上通过!没有写注视,不好意思了 !

#21
fish_2007-05-04 20:56

每人顶了?

#22
abc8882007-05-04 21:37

if(a==a[dong])出错
error C2676: binary '==' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a conversion to a type acceptable to the predefin

#23
suwein2007-05-04 21:51
回复:(abc888)两道小题
第一题:
void main()
{
int m,n;
cin>>m;
n=log(m)/log(5);
int k=0,l=5;
while(n--)
{
k+=m/l;
l*=5;
}
cout<<k;
}
//这道题大概就是a/5+a/25+a/125+..... 200有49个0
#24
fish_2007-05-05 12:44

#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string a,beg,max;
cout<<"enter a string:";
cin>>a;
int b=a.length() ;

for(string::size_type i=0;i<b;i++)
{

if(max.length ()<beg.length ())
{
max=beg;
}
else
{
max=max;
}

for(string::size_type dong=i+1;dong<=b;dong++)
{
if(max.length ()<beg.length ())
{
max=beg;
}
else
{

max=max;
}
if(a[i]==a[dong])
{

beg=a[i];
for(string::size_type dan=1;dan<dong-i;dan++)

{
if(a[i+dan]==a[dong+dan])
{
beg+=a[i+dan];
}
else

{
goto begin;
}
}
}
else
{
}
begin:
int o=o+1;
}
}


cout<<max<<'\n';
return 0;
}
不好意思!复制错了!

#25
fish_2007-05-05 12:45
应该是a[i]==a[dong]
#26
abc8882007-05-10 18:27
不对呀!看清题目要求:
2.输入n个字符串,输出这些字符串的最长匹配子串;
如:
输入:abcdefgfgf
a39dacn,cdefgeIo?
a3 dcnn,wwcdefg.
输出:cdefg
#27
leeco2007-05-10 18:51

18楼abc888对的,23楼suwein也比较靠谱,就是两个log()调用多余了


#include<stdio.h>


int main()
{
    int n,res=0;
    scanf(\"%d\",&n);
    for(int i=5;i<=n;i*=5)
        res+=n/i;
    printf(\"%d\n\",res);
}

#28
fish_2007-05-12 21:15
日了  n个字符串    我当成 n个字符了
#29
amdam230002007-05-13 16:53
对于第一题:
#include<iostream.h>
void main()
{
int a;
cout<<"请输入阶数:"<<endl;
cin>>a;
int aa=a/4;
if(a==aa*4)cout<<"阶乘后得数0的个数是:"<<aa-1<<endl;
else cout<<"阶乘后得数0的个数是:"<<aa<<endl;
}


这是找到它的规律做出来的~~~~~很简单

[此贴子已经被作者于2007-5-13 16:58:34编辑过]

#30
fish_2007-05-15 17:26
a=8 cout 1
a=9 cout 2 ??
1