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 ...
随机推荐
- HDU 4122 Alice's mooncake shop (单调队列/线段树)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4122 题意:好难读懂,读懂了也好难描述,亲们就自己凑合看看题意把 题解:开始计算每个日期到2000/1/ ...
- 【Struts2】新建一个Struts2工程,初步体验MVC
实现目标 地址栏输入http://localhost:88/Struts2HelloWorld/helloworld.jsp 输入用户名,交由http://localhost:88/Struts2He ...
- [Python][自己写的杀病毒脚本]
电脑里的HTML都插入了一段VB病毒代码..只能自己手动清除了..发现Python确实好用 import os import re; Root = ["H:"]; for root ...
- T-SQL和PL/SQL 区别
结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据库系统:同时也是数据库 ...
- Dreamwaver 使用root用户连接不上远程服务器
我用dreamweaver连接远程服务,开始用的是root用户登录的,但是连接不上.网上查了一下,解决教程非常复杂,我就不列出来了. 后来我想了一下,之前我有连接过.我感觉可能是用户的问题,于是我在远 ...
- iOS指纹识别
#import "ViewController.h" #import <LocalAuthentication/LocalAuthentication.h> @inte ...
- QT5.5实现串口通信
QT5.1以上版本自带QtSerialPort集成库,只要在头文件中集成 #include <QtSerialPort/QSerialPort> #include <QtSerial ...
- Reward HDU
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32 ...
- transient 做个标记
import java.io.*; import java.util.*; public class Logon implements Serializable { /** * */ private ...
- vim 常用快捷键 二[转]
键盘移动 (Move) 一切都从键盘的移动k -> 上 upj -> 下 downh -> 左 leftl -> 右 rightz -> 重画屏幕,当前光标变成屏幕的第一 ...