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 ...
随机推荐
- 阿里云部署Docker(5)----管理和公布您的镜像
出到这节,我在百度搜索了一下"阿里云部署Docker",突然发现怎么会有人跟我写的一样呢?哦,原来是其它博客系统的爬虫来抓取,然后也不会写转载自什么什么的.所以,我最终明确为什么那 ...
- mvc 5 的过滤器和webapi 过滤器 对应实现的action过滤器区别
asp.net webapi Action过滤器实现这个: #region 程序集 System.Web.Http, Version=5.2.3.0, Culture=neutral, Publi ...
- 玩转Nodejs日志管理log4js(转)
转自:http://blog.fens.me/nodejs-log4js/ 前言 日志对任何的应用来说都是至关重要的.在Nodejs中使用express框架并没有自带的日志模块,我们可以选择log4j ...
- php 二维码生成类
<?php /** * BarcodeQR - Code QR Barcode Image Generator (PNG) * @package BarcodeQR * @category Ba ...
- Python 线程(threading) 进程(multiprocessing)
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- ios禁用多按钮同时点下的效果
只需要把那些不能同时点下的按钮或者视图设置一下即可. [view setExclusiveTouch:YES]; 避免view上多个button同时按下,则可设置每个button的setExclusi ...
- (原+转)ubuntu16中安装opencv2.4.11
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638117.html 参考网址: http://www.cnblogs.com/jeakon/arch ...
- cherry-pick,revert和rebase使用的3-way合并策略
git中的cherry-pick,revert和rebase都使用的是3-way合并策略,下面就来看看这3个方法使用的merge-base,ours和theirs分别是什么. cherry-pick ...
- PHP图片加文字水印和图片水印方法
文字水印 $dst_path = 'dst.jpg'; //创建图片的实例$dst = imagecreatefromstring(file_get_contents($dst_path)); //打 ...
- C# 对类中的保护成员进行写操作(邀请大家拍砖)
假如我有一个类库 Lib,提供一个类 ClassA 对外服务,ClassA 中有若干只读属性 PropA, PropB 等等,外部调用者无法对 ClassA 中的 PropA 和 PropB 进行写操 ...