1.7编程基础之字符串_01统计数字字符个数
1129:统计数字字符个数
总时间限制:
1000ms
内存限制:
65536kB
描述
输入一行字符,统计出其中数字字符的个数。
输入
一行字符串,总长度不超过255。
输出
输出为1行,输出字符串里面数字字符的个数。
样例输入
Peking University is set up at 1898.
样例输出
4
来源
习题(7-1) 医学部 2010 期末试题 樊波
C++参考代码一:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int count=0,len;
string str;
//char ch[256];
getline(cin,str);
//cin>>str;
//gets(ch);
len=str.size();
//cout<<"len="<<len<<endl;
//len=strlen(ch);
for(int i=0;i<len;i++)
{
if(str[i]>='0' && str[i]<='9') count++;
}
cout<<count<<endl;
return 0;
}
C++参考代码二:
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[260];
int num=0,i=1,j;
while( ( s[i]=getchar() ) != '\n')
{
if(s[i]>='0'&&s[i]<='9') num++;
i++;
}
cout<<num;
return 0;
}
C++参考代码三:
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[260];
int num=0,i=1,j;
//cin>>s;
s[1]=getchar();
while(s[i]!='\n'){
i++;
s[i]=getchar();
}
//cout<<s+1<<endl;
//cout<<s<<endl;
/*
for(int k=1;k<i;k++)
{
cout<<s[k];
}
cout<<endl;
cout<<"i="<<i<<endl;
*/
for(int j=1;j<i;j++)
{
if( s[j]>='0' && s[j]<='9')
{
num++;
}
}
/*
cout<<s+1<<endl;
for(int j=1;j<=i;j++){
if(s[j]>='0'&&s[j]<='9') num++;
}
*/
cout<<num;
return 0;
}
python3代码:
"""
1.7编程基础之字符串 01统计数字字符个数
http://noi.openjudge.cn/ch0107/01/
"""
n = input()
c = 0
for i in n:
if i.isnumeric():
c += 1
print(c)
电子学会 C语言 2级 17 、统计数字字符个数
scratch学习、Scratch算法、蓝桥杯scratch、电子学会scratch、程序猿的数学:scratch篇
scratch学习、Scratch算法、蓝桥杯scratch、电子学会scratch、程序猿的数学:scratch篇_dllglvzhenfeng的博客-CSDN博客
python画图、python小游戏、python刷题、python算法、python编程与数学
python画图、python小游戏、python刷题、python算法、python编程与数学_dllglvzhenfeng的博客-CSDN博客
中小学信息学相关比赛汇总(C++类)
信息学竞赛有什么好的比赛网站?
信息学竞赛有什么好的比赛网站?_信息学奥赛一本通(c++版)在线评测系统_dllglvzhenfeng的博客-CSDN博客
高含金量国际计算机编程竞赛
【国际竞赛-计算机篇】2022年高含金量的计算机竞赛有哪些
【国际竞赛-计算机篇】2022年高含金量的计算机竞赛有哪些_计算机竞赛含金量排名_dllglvzhenfeng的博客-CSDN博客