我输入一个整数, 如 254698, 程序能从这个数中找出最大的一个数字,比如这里就是 9。
我输入一个整数, 如 254698, 程序能从这个数中找出最大的一个数字,比如这里就是 9。
贴了一题,没人应,那我自己来给答案算了。解法可以有很多,我给出我的一种。
#include <algorithm> #include <iostream> #include <sstream> #include <string>
using namespace std;
int main() { int x; string s; stringstream sin;
cout << \"Number: \" << flush; cin >> x;
sin << x; s = sin.str();
sort(s.begin(), s.end());
cout << \"The max digit: \" << *(s.rbegin()) << endl; return 0; }
我在C-FREE上编译报错
第三行:sstream no such file or directory
二楼的那段代码没有错,如果你编译出错,或不能运行,那是你的编译器有问题,
我的做法是 VC 结合 Dev, 恳请大家也效仿我。我并没有意思针对任何公司,但是当今世界TC 确实已经没有市场,而国內还是很多人在使用, 这令我感到很疑惑, Borland 公司本身已经不再研发 TC, TC 已经彻底被淘汰.
为了使得这段代码在Dev上运行时能看到运行结果,我加了一行语句,并为此加了一个头文件.
下面 是该程序,
关于 sort 函数的解释,请看这个连接,http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfAlgorithmSort.asp
#include <algorithm> // for the template funtion sort(...); #include <iostream> #include <sstream> #include <string> #include <cstdlib>
using namespace std;
int main() { int x; string s; stringstream sin;
cout << \"Number: \" << flush; cin >> x;
sin << x; s = sin.str();
sort(s.begin(), s.end());
cout << \"The max digit: \" << *(s.rbegin()) << endl; system(\"pause\"); return 0; }
好学,
这段代码为 C++代码,如果你将他存为 *.c 这样的文件,那当然不对了,如果你用 c 编译器来编译 c++ 程序,那当然也是不行的。 c++ 包容 c, 但这句话不能反过来说, c 并不包容 c++.
如果连接不上那就自己查,
这个总是可以连接上的: http://msdn.microsoft.com
上去以后大家可以看到右上角有个搜索框, Search MSDN for, 把要搜索的內容打进去,然后按回车就可以了.
TC这个编译器的确是落后了,只是因为现在的系统是32位的,而TC是一个16位的编译器,但是不能否认TC是个优秀编译器。
main() { long i ; int a=0 ; scanf("%ld",&i); while(i)a=i%10>a?i%10:a,i/=10 ; printf("%d",a);
getch();
}
[此贴子已经被作者于2004-06-16 15:09:38编辑过]