Fork me on GitHub

HDU 2017 (水)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2017

题目大意:给你段字符串,求出字符串中含有数字字符的个数

解题思路:

字符串输入输出的基本应用:http://c.biancheng.net/cpp/html/3106.html     https://blog.csdn.net/qq_29924041/article/details/54351384

scanf() 只吃实体字符串,空格回车不吃

gets() 空格回车都吃,吃到回车为止

getchar() 啥都吃,一次吃一个

 

PS: 本题用到的不太多,所以只是肤浅介绍的写一下本题相关,有什么更好的以后再更新

代码:

 1 #include<iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<algorithm>
 7 using namespace std;
 8 int main()
 9 {
10     int n;
11     char str[105];
12     while(cin >> n)
13     {
14         getchar();
15         while(n --)
16         {
17             int q = 0;
18             gets(str);
19             int len = strlen(str);
20             for(int i = 0; i < len; i ++)
21             {
22                 if(str[i] >= '0' && str[i] <= '9')
23                     q ++;
24             }
25             cout << q << endl;
26         }
27     }
28 }

                       ***************更新*************

 

 

 

 

思路扩展:

23行 cin 也可以输入字符串(除了 scanf() ,gets(() )

4行 不用开数组法

5行 isdiget() 函数

1 char c;
2 char str[20];
3 cin >> str;
4 for (int i = 0 ; (c = getchar()) != '\n'; i ++){
5 ...isdigit() // 判断是数字的函数
6 }

scanf格式

1 char str[...];
2 scanf(.. ,str);
3 while(st[i]!='\0'){
4 ...
5 }

几个不太常用:

1.cin.getline(str,M); 取前 M 个字符:https://www.cnblogs.com/flatfoosie/archive/2010/12/22/1914055.html
2.http://www.voidcn.com/article/p-qchoewcs-kc.html ,其中 cin.ignore(); 不带参数默认为(1,EOF)就是去掉头一个字符 :https://www.cnblogs.com/ranjiewen/p/5582601.html

3.char *s = str; 表示 s 指针指向 str 数组:https://blog.csdn.net/qq_21794823/article/details/53316853,其中while(*s)表示当 s 数组不为空

4.学到 string https://blog.csdn.net/tengfei461807914/article/details/52203202

 

1 string a;
2 cin>>a; 

 

不吃空格回车

posted @ 2018-07-27 20:07  GerJCS  阅读(205)  评论(0编辑  收藏  举报