hdu1106 字符串水题strtok()&&strchr()&&sscanf()+atoi()使用
字符串的题目 用库函数往往能大大简化代码量
以hdu1106为例
函数介绍
strtok()
原型: char
*strtok(char s[], const char *delim);
功能:
说明
strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0
字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
#include<stdio.h>
int main(void)
{
charinput[16]="abc,d";
char*p;
p=strtok(input,",");
if(p) printf("%s\n",p);
p=strtok(NULL,",");
if(p) printf("%s\n",p);
return 0;
}
atoi()
说明
strtod() strtold() strtol() 这在文章结束后介绍
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int num[1010];
char temp[1010];
int Qsort(int s,int t)
{
int i=s,j=t,x=num[s];
while(i<j)
{
while(i<j&&num[j]>x) j--;
if(i<j) num[i]=num[j],i++;
while(i<j&&num[i]<x) i++;
if(i<j) num[j]=num[i],j--;
}
num[i]=x;
if(s<i-1) Qsort(s,i-1);
if(i+1<t) Qsort(i+1,t);
return 0;
}
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
char *k;
int len,i;
while(gets(temp)!=NULL)
{
k=NULL;len=0;
for(k=strtok(temp,"5");k!=NULL;k=strtok(NULL,"5"))
num[len++]=atoi(k);
Qsort(0,len-1); for(i=0;i<=len-1;i++)
{
printf("%d",num[i]);
if(i!=len-1) printf(" ");
}
printf("\n");
}
return 0;
}
strchr()
说明
sscanf()
说明
1
2
3
|
charbuf[512]; sscanf ( "123456" , "%s" ,buf); //此处buf是数组名,它的意思是将123456以%s的形式存入buf中! printf ( "%s\n" ,buf); |
1
2
|
sscanf ( "123456" , "%4s" ,buf); printf ( "%s\n" ,buf); |
1
2
|
sscanf ( "123456abcdedf" , "%[^]" ,buf); printf ( "%s\n" ,buf); |
1
2
|
sscanf ( "123456abcdedfBCDEF" , "%[1-9a-z]" ,buf); printf ( "%s\n" ,buf); |
1
|
printf ( "%s\n" ,buf); |
1
2
|
sscanf ( "123456abcdedfBCDEF" , "%[^A-Z]" ,buf); printf ( "%s\n" ,buf); |
1
2
|
sscanf ( "iios/12DDWDFF@122" , "%*[^/]/%[^@]" ,buf); printf ( "%s\n" ,buf); |
1
2
|
sscanf (“hello,world”, "%*s%s" ,buf); printf ( "%s\n" ,buf); |
hdu1106 字符串水题strtok()&&strchr()&&sscanf()+atoi()使用的更多相关文章
- 1222: FJ的字符串 [水题]
1222: FJ的字符串 [水题] 时间限制: 1 Sec 内存限制: 128 MB 提交: 92 解决: 20 统计 题目描述 FJ在沙盘上写了这样一些字符串: A1 = “A” A2 = ...
- 1001 字符串“水”题(二进制,map,哈希)
1001: 字符串“水”题 时间限制: 1 Sec 内存限制: 128 MB提交: 210 解决: 39[提交][状态][讨论版] 题目描述 给出一个长度为 n 的字符串(1<=n<= ...
- 第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题
B - 大还是小? Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Description 输入两个实数,判断第一个数大 ...
- HDU ACM 1073 Online Judge ->字符串水题
分析:水题. #include<iostream> using namespace std; #define N 5050 char a[N],b[N],tmp[N]; void Read ...
- HDU4891_The Great Pan_字符串水题
2014多校第五题,当时题面上的10^5写成105,我们大家都wa了几发,改正后我和一血就差几秒…不能忍 题目:http://acm.hdu.edu.cn/showproblem.php?pid=48 ...
- Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题
B. Ohana Cleans Up Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/554/pr ...
- Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks 字符串水题
A. Kyoya and Photobooks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- uva 10252 - Common Permutation 字符串水题
题意:給定兩個小寫的字串a與b,請印出皆出現在兩字串中的字母,出現的字母由a~z的順序印出,若同字母出現不只一次,請重複印出但不能超過任一字串中出現的次數.(from Ruby兔) 很水,直接比较输出 ...
- 字符串水题(hdoj1049)
Problem Description Password security is a tricky thing. Users prefer simple passwords that are easy ...
随机推荐
- jQuery 事件 - error() 方法
实例 如果图像不存在,则用一段预定义的文本取代它: $("img").error(function(){ $("img").replaceWith(" ...
- C#使用seleium实现一个自动登录器
1.http://docs.seleniumhq.org/ 下载seleium包 2.新建一个C#项目,比如控制台,引用seleium包中的dll using System; using System ...
- CSS网页元素居中
1.水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: text-align: center 适用元素:文字,链接,及其 ...
- php常用mysql函数
mysql_affected_rows: 得到 MySQL 最后操作影响的列数目. mysql_close: 关闭 MySQL 伺服器连线. mysql_connect: 开启 MySQL 伺服器连线 ...
- sql 列设置默认值,语法查询知识点积累
一.修改字段默认值 alter table 表名 drop constraint 约束名字 ------说明:删除表的字段的原有约束 alter table 表名 add constraint 约 ...
- 【solr基础教程之九】客户端
一.Java Script 1.由于Solr本身可以返回Json格式的结果,而JavaScript对于处理Json数据具有天然的优势,因此使用JavaScript实现Solr客户端是一个很好的选择. ...
- solr源码导入eclipse
转载自:http://blog.csdn.net/vltic/article/details/19917377 (1)相应的开发环境准备 (1)jdk1.6+的安装和环境变量配置(命 ...
- css3的滤镜模糊的效果
最近在做一个css3的滤镜模糊的效果,但是我发现,有些浏览器一点效果都没有,这是浏览器兼容性导致的,怕今后会忘记所以就先写下来,也希望可以帮到需要的小伙伴. 代码如下: div{//设置半透明滤镜效果 ...
- python快速搭建WebServer
#!/usr/bin/python import SimpleHTTPServer import SocketServer import os PORT = 7777 WEBDIR = "/ ...
- python运维开发(二十二)---JSONP、瀑布流、组合搜索、多级评论、tornado框架简介
内容目录: JSONP应用 瀑布流布局 组合搜索 多级评论 tornado框架简介 JSONP应用 由于浏览器存在同源策略机制,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性. ...