HDU 1106 排序(atoi函数和strtok函数)
来源:互联网 发布:windows 10不能玩魔兽 编辑:程序博客网 时间:2024/06/06 18:13
排序
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 48472 Accepted Submission(s): 13987
Problem Description
输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。
你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。
Input
输入包含多组测试用例,每组输入数据只有一行数字(数字之间没有空格),这行数字的长度不大于1000。
输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。
Output
对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。
Sample Input
0051231232050775
Sample Output
0 77 12312320
Source
POJ
题解:使用atoi()函数和strtok()函数搞定。。。。
strtok函数解析:
Syntax:
#include < string.h>
char *strtok( char *str1, const char *str2 );
The strtok() function returns a pointer to the next “token” in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL.
For example:
char str[] = “now # is the time for all # good men to come to the # aid of their country”;
char delims[] = “#”;
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( “result is \”%s\”\n”, result );
result = strtok( NULL, delims );
}
The above code will display the following output:
result is “now ”
result is ” is the time for all ”
result is ” good men to come to the ”
result is ” aid of their country”
AC代码:
#include<cstdio>#include<cstdlib>#include<string>#include<cstring> char s[1005];int a[1005];char * temp;int cmp(const void *a,const void *b){ return *(int *)a-*(int *)b;}int main(){ int c; while(gets(s)) { a[0]=atoi(strtok(s,"5")); c=1; while(temp=strtok(NULL,"5")) { a[c++]=atoi(temp); } qsort(a,c,sizeof a[0],cmp); printf("%d",a[0]); for(int i=1;i<c;i++) { printf(" %d",a[i]); } printf("\n"); } return 0;}
orAC2代码:
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<cstdlib>using namespace std;const int maxx = 1001;char a[maxx][9]; //字符串 long b[maxx]; //数字 int cmp(const void *a,const void *b){ return *(int*)a-*(int*)b;}int main(){ string s; while(cin>>s){ int i=0,j=0,k=0; for(i=0,j=0,k=0;s[i]!='\0';i++,k++){ a[j][k]=s[i]; if(s[i]=='5'){ a[j][k]='\0'; if(strlen(a[j])==0){ k=-1;continue; } b[j]=atoi(a[j]); //将字符串转换成数字 j++;k=-1; } } a[j][k]='\0'; b[j]=atoi(a[j]); if(s[i-1]=='5') j--; qsort(b,j+1,sizeof(long),cmp); for(i=0;i<=j;i++) { cout<<b[i]; if(i<j) cout<<" "; } cout<<endl; } return 0;}
- HDU 1106 排序(atoi函数和strtok函数)
- atoi函数和strtok函数
- Hdu 1106 排序 (atoi函数与 strtok函数的应用)
- HDU 1106 排序 atoi函数
- strtok 函数功能及其应用 HDU 1106 排序
- 杭电-1106,strtok、atoi函数的应用
- strtok函数的简单应用 hdu 1106
- strtok和strtok_r函数
- STRTOK函数和STRTOK_R函数
- STRTOK函数和STRTOK_R函数
- strtok函数和strtok_r函数
- STRTOK函数和STRTOK_R函数
- strtok函数和strtok_r函数
- strtok函数和strtok_r函数
- STRTOK函数和STRTOK_R函数
- strtok函数和strtok_r函数
- strtok()函数和getopt()函数
- HDU1106黑科技-strtok函数与atoi函数
- java中的char和boolean
- TextView实现带链接文字事件监听三种常用方式
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- C++:google style 强制类型转换
- Linux设备驱动之——I2C总线
- HDU 1106 排序(atoi函数和strtok函数)
- ZOJ1649Rescue
- Activity生命周期和启动模式
- java编译基础总结
- Mts视频丢失损坏的恢复修复
- Xcode6中怎么添加空工程模板
- 一位数相乘
- java基础知识(四)
- VC/MFC 定时器设置及OnTimer使用