C++基本操作:读入、数组、指针、Vector

9/23第一次数据结构上机,9/24写完三题程序并上传了作业。
是第一次使用C++

  • 学习C++类定义和类函数
  • 优化时间复杂度
最大无重复字符子串的长度

输入格式
一个字符串
输出格式
一个整数
样例
输入”abdaoecas”,最大无重复字符子串为”bdaoec”,输出6

#include<iostream> 
#include<string>
using namespace std;

int main()
{
	string s1;
	getline(cin,s1);
	
	char *q,*p=&s1[0],*pp=p+1;
	int x=0,max=0;
	while(*p)
	{
		while(*pp)
		{   
			for(q=p;q<pp;q++)
			 if(*q==*pp) 
			 {
			 	x=pp-p;
			 	if(x>max) max=x;
			 	p++;pp=p;
			 	break;
			 } 
            pp++;
            if(!*pp)
            {
            	x=pp-p;
			 	if(x>max) max=x;
			 	p++;pp=p;
			}
		}
	}
	cout<<"最长无重复子串的长度:"<<max<<endl;
}

初次理解有误,认为去掉重复字符即可。
主要思路是:亦步亦趋的比较,用工作指针确认当前字符不与之前任何一个字符相同后,pp前进。


简单排序

给定一组含有个整数的无序数列,将前大的数按照由大到小的顺序输出。
输入格式
n k a1 a2 … an (取值有特殊要求)
输出格式
前k大的数,以空格为间隔符:ax ay … az
输入样例
10 4 19999 47 -5 -65536 89 9 -214 43
输出样例
19999 89 77 64

#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
	int n,k,i=0;
	while(1) 
	{
	cout<<"输入整数个数:"<<endl;
	cin>>n;
	if(0<n&&pow(10,4)>n) break;
	else cout<<"n不符合要求,重新输入"<<endl;
	}
	vector<long long int> num;
	cout<<"输入"<<n<<"个整数:"<<endl; 
	for(;i<n;i++)
	{
		while(1)
		{long long temp;
		cin>>temp;
		if(-pow(10,10)<temp&&pow(10,10)>temp)
		{num.push_back(temp);break;}
		else {cout<<"请重新输入:"<<endl;}
	    }
    }
	sort(num.begin(),num.end());
	while(1)
	{
		cout<<"输入所需输出整数个数:"<<endl;
		cin>>k;
		if(0<k&&pow(10,4)>k&&k<=n) break;
		else cout<<"k不符合要求,重新输入"<<endl;
	}
	cout<<"输出结果:"<<endl;
	for(i=n-1;i>n-1-k;i--)
	cout<<num.at(i)<<' ';
} 

使用Vector,需进一步熟悉
对输入数字是否符合条件的判断
sort函数的使用需进一步熟悉(升序)


单词拼写

给定一个词汇表words 和一张字母表 chars, 返回words中能被 chars拼写出的单词总长度。注意对于每个单词word, 给定chars中的每个字符只能用一次,例如 ‘abca’ 不能被 ‘abc’ 拼写出来。

输入格式
word_1 word_2 word_3 word_4 … word_n
chars
第一行为单词表,单词个数不超过100个,第二行为字符串。单词表和字符串均由小写字母构成。
输出格式
整数k. k 为所有能被拼写出来的单词长度之和。
输入样例
cat bt hat tree
attach
输出样例
6

#include<iostream>
#include<string>
#include<string.h>
#include<vector>
using namespace std;
int main()
{   string save;
    cout<<"输入词汇表单词word:"<<endl;
    getline(cin,save);
    cout<<save<<endl;
    cout<<"输入单词组成字母表chars:"<<endl;
    int alpha[26]={0},i=0;
    while(1)
    {
    	char temp=getchar();
    	if(temp=='\n') break;
    	if(96<temp&&123>temp&&alpha[temp-97]==0) alpha[temp-97]=1;
    	if(temp==' ') i++;
    	if(i>=100) break;
	 } 
    int x=0,num=0,k=0,test[100]={0}; 
    char *temp=&save[0];
	while(1)
	{   
		if(test[*temp-97]==1)
		{while(*temp!=' '&&*temp!=0)
		temp++;
		if(*temp!=0) temp++;
		else break;}
		if(!*temp) 
		{if(k==x) num+=x;
		break;}
		if(96<*temp&&123>*temp&&test[*temp-97]==0)
		{k++;test[*temp-97]=1;}
		if(alpha[*temp-97]==1) x++;
		if(*temp==' ') 
		{if(k==x) num+=x;
		k=0;x=0;
		for(i=0;i<100;i++)
		test[i]=0;}
		temp++;}
		
	cout<<"能被拼写出来的单词长度之和:"<<num<<endl;
 } 

这题删删改改写了挺多次的
此处用了数组和ASCii码直接判断,对词汇的遍历使用指针
要注意指针到达字符串末尾时的处理方式
熟悉C++中读入输入字符的方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值