C++编程题

1、不用系统提供的字符串转int的功能,将一个字符串转换为对应的值

#include <iostream>
using namespace std;

static int StringToInt(string s)
{
	int sum;
	for(int i=0;i<s.length();i++)
	{
		sum = sum * 10 + (s[i]-'0');
	}
	return sum;
}

int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	int sum;
	string s = "hello";
	sum = StringToInt(s);
	cout<<sum<<endl;
	return 0;
}

 

2、 1*2*3*......*3000相乘结果的末尾有多少个0?

#include <iostream>
using namespace std;

int CountZeroDemo(int n){
	int count=0;
	int j;
	for(int i=1;i<=n;i++){
		j = i;
		while(j%5 == 0){
			count++;
			j/=5;
		}
	}
	return count;
};

int main() {
	cout << CountZeroDemo(3000) << endl;
	return 0;
}


/*int main(){
	int n=5,c=3000,i=0;
	while(n<=c){
		i += c/n;
		n = n*5;
	}
	cout<< i <<endl;
	return 0;
}*/
posted @ 2014-08-27 10:24  不止所见  阅读(236)  评论(0编辑  收藏  举报