Re: [问题] 判断输出问题

楼主: pziyout (pziyout)   2014-08-18 11:28:31
※ 引述《oklp1415 (天生我材)》之铭言:
: 开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
: Dev-C++
: 额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
: 问题(Question):
: 喂入的资料(Input):
: 预期的正确结果(Expected Output):
: 错误结果(Wrong Output):
: 程式码(Code):(请善用置底文网页, 记得排版)
: ....先前一些零粹的code就不PO上来了
: 以下是我要判断的条件输出程式码
: int q[5];
: for(c=0,d=0;c<5;c++){
: for(e=0;e<5;e++){
: if(a[c]!=a[e]){
: q[c]=a[c];
: }
: for(int j=0;j<5;j++){
: printf("%d",q[j]);
: }
: }
: }
: }
: 比如说输入一串数字 1 2 3 4 5 1 2 3 5 6
: 输出会是 1 2 3 4 5 6
: 意思把前面重复的数字删除
: 我做法是每往下一个,判断后重头再跑一次判断,额外存入另一个变量内
: 最后依序输出不知道这样做法哪里错误了~"~一直弄不出来
: 求解~.~thanks
: 补充说明(Supplement):
这种问题若以 C++ 的作法,我会这样做:
string foo("1 2 3 4 5 1 2 3 5 6") ;
istringstream istr(foo) ;
int n ;
set<int> seen ;
while ( istr >> n ) {
if ( seen.find(n) == seen.end() ) {
cout << n << " " ;
seen.insert(n) ;
}
}
cout << endl ;
以上假设输入的值已经存到一个字串内,此字串当然可用 getline 取得。
执行效率上可能稍慢,不过程式易懂。

Links booklink

Contact Us: admin [ a t ] ucptt.com