STEP5::Problem 0068 : Ch6-3.二三四問題

http://web2.ck.tp.edu.tw/~step5/probdisp.php?pid=0068
單純字串處理,只是要注意數字尾巴直接接負號的情況(123-123),為了方便我一次讀一行,抓完一次數字就把指標往回推一格。

排序直接用 std::sort(array,array+x); ,會從小排到大。也可以塞函示進去,頗方便。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int nu[20001];
char tt[10001];
bool nn(char a){
return (a-'0'>=0&&a-'0'<=9);
}
int main(int argc,char *argv[])
{
int t,st=0,ss=1;
scanf("%d",&t);
for(int i=0;i<t;i++){
scanf("%s",tt);
for(int j=0;j<strlen(tt);j++){
if(tt[j]=='-'&&nn(tt[j+1])){
j++;
ss=-1;
}
if(nn(tt[j])){
int tmp=0;
while(nn(tt[j])){
tmp=tmp*10+tt[j]-'0';
j++;
}
nu[st++]=tmp*ss;
j--;
ss=1;
}
}
}
sort(nu,nu+st);
printf("%d\n",st);
for(int i=0;i<st;i++)printf("%d ",nu[i]);
puts("");
//system("pause");
return 0;
}