A1082.Read Number in Chinese(25)

参考代码

//将一个有符号的数字部分不大于9位的数字按照中文的读的方式输出 
#include <cstdio>
#include <cstring>
char num[10][5]={
	"ling","yi","er","san","si","wu","liu","qi","ba","jiu" 
};                                                        
char wei[5][5]={"Shi","Bai","Qian","Wan","Yi"}; 
int main(){
	char str[15];
	gets(str);//按字符串方式输入数字
	int len=strlen(str);//字符串长度
	int left=0,right=len-1;
	if(str[0]=='-'){//如果是负数,则输出"Fu",并把left右移一位
		printf("Fu");
		left++; 
	} 
	while(left+4<=right){
		right-=4;//将right每次左移4位,知道left与right在同一节 
	}
	while(left<len){//循环每次处理数字的一节(4位或小于4位)
		bool flag=false;//flag==false表示没有积累的0
		bool isPrint=false;//isPrint==false表示该节没有输出过其中的位
		while(left<=right){//从左至右处理数字中某节的一位
			if(left>0&&str[left]=='0'){//如果当前位为0 
				flag=true;//令标记flag为true 
			}else{//如果当前位不为0 
				if(flag==true){//如果存在积累的0
					printf(" ling");
					flag=false; 
				} 
				//只要不是首位(包括负号),后面的每一位前都要输出空格
				if(left>0) printf(" ");
				printf("%s",num[str[left]-'0']);//输出当前位数字
				isPrint=true;//该节至少有一位被输出
				if(left!=right){//某节除了个位外,都需要输出十百千
					printf(" %s",wei[right-left-1]); 
				} 
			} 
			left++;//left右移1位 
		}
		if(isPrint==true&&right!=len-1){//只要不是个位,就输出万或亿 
			printf(" %s",wei[(len-1-right)/4+2]);
		}
		right+=4;//right右移4位,输出下1节 
	}
	return 0; 
}        
               

emm,这题确实有点难度,不想自己写了,直接参考书上的代码。在代码的处理上确实没有什么太值得借鉴的,对逻辑处理的要求比较高。

 

以下是Matlab代码,可以实现基本数据分析,包括统计各门课程的平均分,分别统计各门课程的及格人数和及格率,以及绘制各分数段人数占比图: ```Matlab % 读取Excel文件中的数据 data = readtable('numdata1.xlsx'); % 统计各门课程的平均分 mean_chinese = mean(data.Chinese); mean_math = mean(data.Math); mean_english = mean(data.English); fprintf('The average score of Chinese is %.2f\n', mean_chinese); fprintf('The average score of Math is %.2f\n', mean_math); fprintf('The average score of English is %.2f\n', mean_english); % 分别统计各门课程的及格人数和及格率 pass_chinese = sum(data.Chinese >= 60); pass_math = sum(data.Math >= 60); pass_english = sum(data.English >= 60); pass_rate_chinese = pass_chinese / length(data.Chinese); pass_rate_math = pass_math / length(data.Math); pass_rate_english = pass_english / length(data.English); fprintf('The number of students passed in Chinese is %d, and the pass rate is %.2f\n', pass_chinese, pass_rate_chinese); fprintf('The number of students passed in Math is %d, and the pass rate is %.2f\n', pass_math, pass_rate_math); fprintf('The number of students passed in English is %d, and the pass rate is %.2f\n', pass_english, pass_rate_english); % 绘制各分数段人数占比图 edges = 0:10:100; chinese_counts = histcounts(data.Chinese, edges); math_counts = histcounts(data.Math, edges); english_counts = histcounts(data.English, edges); total_counts = chinese_counts + math_counts + english_counts; chinese_ratio = chinese_counts / length(data.Chinese); math_ratio = math_counts / length(data.Math); english_ratio = english_counts / length(data.English); total_ratio = total_counts / length(data.Chinese); figure; bar(edges(1:end-1), [chinese_ratio' math_ratio' english_ratio' total_ratio'], 'stacked'); xlabel('Score'); ylabel('Ratio'); legend('Chinese', 'Math', 'English', 'Total'); ``` 以上代码中,`readtable`函数用于读取Excel文件中的数据,然后分别统计各门课程的平均分和及格人数及及格率。接着使用`histcounts`函数统计各分数段人数占比,最后使用`bar`函数绘制各分数段人数占比图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值