UDP广播协议叫吃饭


私信TA

用户名:Mustenaka

访问量:133987

签 名:

个人博客www.mustenaka.cn

等  级
排  名 12
经  验 23607
参赛次数 8
文章发表 196
年  龄 3
在职情况 学生
学  校 Sky_box
专  业 NE

  自我简介:

欢迎光临我的博客www.mustenaka.cn,Python,C#,U3D,C/C++开发合作可以找我

解题思路:
按照ASCII码表对应的数进行编写,统计各个数字出现的次数。

ASCII表.jpg

注意事项:

scanf见到空格会吞掉数组,所以使用gets来进行字符串的存储。
参考代码:

其中<bits/stdc++.h>(万能头文件)可以修改为<iostream><cstring>两个头文件

#include<bits/stdc++.h>
#define max 1000
using namespace std;
int main() {
	int a=0,b=0,c=0,d=0;
	char arr[max];
	gets(arr);
	int len=strlen(arr);
	for(int i=0; i<len; i++) {
		if((arr[i]>='A'&&arr[i]<='Z')||(arr[i]>='a'&&arr[i]<='z'))
			a++;
		else if(arr[i]>='0'&&arr[i]<='9')
			b++;
		else if(arr[i]==32)
			c++;
		else
			d++;
	}
	cout<<a<<' '<<b<<' '<<c<<' '<<d<<endl;
	return 0;
}


 

0.0分

17 人评分

  评论区

#include<stdio.h>
#include<string.h>
int fw(char a[]){
	
	int b=0,c=0,d=0,e=0;
	for(int i=0;i<strlen(a);i++){
		if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<='z'))
		b++;
	else if(a[i]>='0'&&a[i]<='9')
		c++;
	else if(a[i]==' ')
		d++;
	else
	e++;	
	}
	printf("%d %d %d %d",b,c,d,e);
	
}
int main()
{char a[50];
	gets(a);
	fw(a);
}
这个为什么百分之五十???
2020-12-04 20:24:17
sorry,这是函数修正版的
 #include <cstdio>
 #include <cstring>
 #include <cctype> 
 void Number(char str[])
 {
 	int alpha=0,digit=0,space=0,other=0;
 	for(int i=0; i<strlen(str); i++)
 	{
 		if(isalpha(str[i]))
			++alpha;
 		else if(isdigit(str[i]))
 			++digit;
 		else if(str[i]==32)
 			++space;
 		else 
 			++other;
	 }
	printf("%d %d %d %d",alpha,digit,space,other);
 }
 int main()
 {
 	char str[100];
 	gets(str);
 	Number(str);
 	return 0;
 }
2018-12-21 23:11:03
方法很好,但是没有满足题目要求,题目要求要写函数传递参数,其他同学可以参考下哈
 #include <cstdio>
 #include <iostream>
 #include <cstring>
 #include <cctype> 
 using namespace std;
 void Number(char str[])
 {
 	int alpha=0,digit=0,space=0,other=0;
 	for(int i=0; i<strlen(str); i++)
 	{
 		if(isalpha(str[i]))
			++alpha;
 		else if(isdigit(str[i]))
 			++digit;
 		else if(str[i]==32)
 			++space;
 		else 
 			++other;
	 }
	 cout<<alpha<<" "<<digit<<" "<<space<<" "<<other<<endl;
printf("%d %d %d %d",alpha,digit,space,other);

 }
 int main()
 {
 	char str[100];
 	gets(str);
 	Number(str);
 	return 0;
 }
2018-12-21 23:07:42
  • «
  • 1
  • »