首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请迷津!在线恭候.

2012-03-15 
请高手指点迷津!急!在线恭候...ItisreallyamazingthatthegreathistorianDr.Khasrecentlyfoundthatabout10M

请高手指点迷津!急!在线恭候...
It   is   really   amazing   that   the   great   historian   Dr.K   has   recently   found   that   about   10   Million   years   ago,   in   the   area   where   is   now   called   China,   lived   an   ancient   people.   They   may   be   considered   as   the   first   intelligent   creature   existed   on   the   earth.

As   Dr.K 's   investigation   advance,   he   found   unbelievably   that   they   even   developed   some   so-called   mathematics   during   their   evolvement.   Dr.K   spent   his   half   life   to   understand   this   ancient   people 's   counting   system.   Finally   he   got   to   know   that:
1、They   use   only   7   digits   to   represent   numbers,   and   numbers   are   as   follow:
|   ->   1
||   ->   2
|||   ->   3
||||   ->   4
|||||   ->   5
||||||   ->   6
|||||||   ->   7
It   is   a   pity   that   they   have   not   developed   "0 "   before   these   people   disappeared.
2、If   a   number   is   greater   than   7,   they   use   digit   place   just   as   we   do   today.   Between   each   two   digit   places,   there   is   a   ", ".
eg:
|||||,||   ->   42   (5x8+2)
|||,||,||||   ->   212   (3*64+2*8+4)

In   order   to   further   his   study,   Dr.K   wants   to   know   what   the   sequences   found   from   stones   mean   in   today 's   counting   system.   He   turns   to   you   for   help,   and   smart   as   you   are,   you   should   help   this   great   historian,should   not   you?

Input

The   first   line   of   standard   input   contains   an   integer   N,   the   number   of   test   cases.   N   lines   followed.
In   each   line   a   sequence   is   given,   it   is   guaranteed   that   the   length   of   the   sequence   will   not   exceed   1024   characters   and   the   outcome   number   will   not   be   greater   than   1000000.

Output

For   each   case   you   are   to   output   a   line   giving   the   number   in   today 's   decimal   counting   system.

Sample   Input

3
|||||,||
|||,||,||||
||,|,|,|,||||


Sample   Output

42
212
8780  


[解决办法]
不做输入的正确性校验,
(即假设用户输入都是符合 输入数据标准的)
参考代码如下:
[解决办法]
#include <cmath>
#include <string>
#include <iostream>
#include <cstdlib>



int main()
{
int i, n;
cout < < "请输入行数: ";
cin> > n;

string line, tmp;
int num, index, power;
for(i=1; i <n+1; i++)
{
num=0, power=0, index=0;
cout < < "请输入第 " < <i < < " 行: ";
cin> > line;

index = line.find( ', ', 0);
while(index != string::npos)
{
power++;
index = line.find( ', ', index+1);
}

tmp = line;
while(power> 0)
{
index = tmp.find( ', ', 0);
num += (index)*pow(8.0, power);
tmp = tmp.substr(index+1);
power --;
}
num += tmp.length();
cout < <line < < " ~ " < <num < <endl;
}
system( "pause ");
return 0;
}
[解决办法]
如果需要做 输入正确性 校验,
使用 string 的 find_first_not_of 方法即可。
[解决办法]
这个很简单啊.
把输入存入char *buf[BUFFSIZE]中, 用for循环从头至尾分离buf的内容.
剩下的就是计算问题了.
[解决办法]
我不是高手:)
#include <iostream>
#include <string>
#include <queue>
#include <math.h>

using namespace std;

int main(int, char*[])
{
int nLineCount;
char input[1024];
cout < < "input:\n ";
cin> > nLineCount;
queue <string> v;

for (int i = 0; i < nLineCount; ++i)
{
cin> > input;
int start = 0;
int result = 0;
int j;

for (j = 0; input[j] != 0; ++j)
{
if (input[j] == ', ')
{
v.push(string(input + start, input + j));
start = j + 1;
}
}

if (j > start)
{
v.push(string(input + start, input + j));
}

while(v.size() > 0)
{
result += (v.front().size() * pow(8.f, (int)v.size() - 1));
v.pop();
}
cout < <result < <endl;
}

return 0;
}

热点排行
Bad Request.