洛谷习题P1598垂直柱状图

本文介绍了在解决洛谷P1598问题时,如何使用`char`类型配合`gets()`函数以及`string`类型结合`str.getline()`方法进行输入处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

char使用gets(char)

string使用str.getline()

#include<bits/stdc++.h>

using namespace std;

int main(){
	char alph[26],ALPH;
	string p1,p2,p3,p4,text;
	int i,j,pos,count[26],maxtimes;
	
	i=0;
	for(ALPH='A';ALPH<='Z';ALPH++){
		alph[i]=ALPH;
		i++;
	}
	
	getline(cin,p1);//char用gets,string用getline 
	getline(cin,p2);
	getline(cin,p3);
	getline(cin,p4);
	text=p1+p2+p3+p4;
	
	for(i=0;i<26;i++){//统计每个字母出现的次数 
		count[i]=0;
		pos=text.find(alph[i]);//在text中查找alph[i] 
		while(pos!=string::npos){
			count[i]++;
			pos=text.find(alph[i],pos+1);
		}
	}
	
	//for(i=0;i<26;i++)cout<<count[i]<<' ';
	//cout<<endl;
	
	maxtimes=0;//最高的柱 
	for(i=0;i<26;i++)maxtimes=max(maxtimes,count[i]);
	//cout<<maxtimes<<endl;
		
	for(i=maxtimes;i>=1;i--){
		for(j=0;j<26;j++){
			if(count[j]>=i)cout<<"* ";else if(count[j]<i)cout<<"  ";
		}
		cout<<endl;
	}
	for(i=0;i<26;i++)cout<<alph[i]<<' ';
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值