当前位置: 代码迷 >> C语言 >> [求助]我个人认为难..我都开始郁闷了...
  详细解决方案

[求助]我个人认为难..我都开始郁闷了...

热度:170   发布时间:2007-08-23 01:36:44.0
[求助]我个人认为难..我都开始郁闷了...
输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。然后对这些分隔的数排序,有小到大排.

我个人认为难...我都郁闷了...
搜索更多相关的解决方案: 数字  

----------------解决方案--------------------------------------------------------
sorting is not a problem --- the key is to break up the numbers.

----------------解决方案--------------------------------------------------------

/*---------------------------------------------------------------------------
File name: 5=space.cpp
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 8/22/2007 12:57:23
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762


Modification history:
===========================================================================


Problem statement:
---------------------------------------------------------------------------
http://bbs.bc-cn.net/viewthread.php?tid=164729

输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。然后对这些分隔的数排序,有小到大排.


Sample output:
---------------------------------------------------------------------------
555123554758955555
555123554758955555
47 89 123
Press any key to continue . . .

12345678
12345678
678 1234
Press any key to continue . . .

0012350046700
0012350046700
123 46700
Press any key to continue . . .
*/

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;

int main()
{
string s;
int i;
vector<int> vi;
istringstream iss;

// get input
getline(cin, s);
cout<<s<<endl;

// replace '5' by ' '
for(i=0; i<s.size(); ++i)
{
if(s[i]=='5')
{
s[i]=' ';
}
}

// break up numbers
iss.str(s);
while(iss>>i)
{
vi.push_back(i);
}

// sort
sort(vi.begin(), vi.end());

// print
for(vector<int>::const_iterator it=vi.begin(); it!=vi.end(); ++it)
cout<<*it<<" ";
cout<<endl;

return 0;
}


----------------解决方案--------------------------------------------------------
斑竹的代码果然厉害,不过int i; 应该改成 unsigned int i; 的吧,否则类型不匹配了。
----------------解决方案--------------------------------------------------------
555123554758955555
555123554758955555
47 89 123
Press any key to continue . . .

12345678
12345678
678 1234
Press any key to continue . . .

0012350046700
0012350046700
123 46700
Press any key to continue . . .
这里怎么要输入两行才输出啊...而且我用vc运行输入两行相同的数字,他在输出一行与我输入一样的数字,然后才输出我想要的答案...这是什么原因????


----------------解决方案--------------------------------------------------------

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;

int main()
{
string s;
int i;
vector<int> vi;
istringstream iss;

// get input
getline(cin, s); //让你输入一行数字,并存放在字符串s中
cout<<s<<endl; //在屏幕上输出字符串s

// replace '5' by ' '
for(i=0; i<s.size(); ++i)
{
if(s[i]=='5')
{
s[i]=' ';
}
}

// break up numbers
iss.str(s);
while(iss>>i)
{
vi.push_back(i);
}

// sort
sort(vi.begin(), vi.end());

// print
for(vector<int>::const_iterator it=vi.begin(); it!=vi.end(); ++it)
cout<<*it<<" ";
cout<<endl;

return 0;
}


----------------解决方案--------------------------------------------------------
  相关解决方案