OJ题目部分答案.docx
- 文档编号:6736444
- 上传时间:2023-01-09
- 格式:DOCX
- 页数:27
- 大小:19.86KB
OJ题目部分答案.docx
《OJ题目部分答案.docx》由会员分享,可在线阅读,更多相关《OJ题目部分答案.docx(27页珍藏版)》请在冰豆网上搜索。
OJ题目部分答案
//统计字符串中某字符个数
#include
#include
usingnamespacestd;
intNum(stringa,charb)
{inti,num=0;
for(i=0;i if(a[i]==b)num++; returnnum; } intmain() {strings;chard; inti;intc; getline(cin,s); cin>>d; for(i=0;i if(s[i]==''||isalnum(s[i])) c=Num(s,d); cout< return0; } /////////////////////////////// //每行输出8个,不够在后补0 #include #include usingnamespacestd; intmain() {chars[100];inti,j,b; gets(s); inta; a=strlen(s); if((a%8)! =0)//字符串的字符个数不是8的整数倍,在后补8-s.length()%8个零 { b=a+8-(a%8); for(i=a;i } for(j=0;i cout< return0; } for(inti=1;i<=s.length();i++) {if(i%8==0)cout< cout< return0; }//////// #include usingnamespacestd; intmain() { inti,j,k,m; cin>>m; for(i=0;i<=20;i++) for(j=0;j<34;j++) for(k=100-i-j;k<=300;k++) {if(5*i+3*j+k/3==100&&(i+j+k)==100&&k%3==0)cout< return0; } /////////////// //华为题库中简单密码破解 #include #include usingnamespacestd; intmain() { strings;inti; cin>>s; charreplace(); for(i=0;i { if(s[i]>='A'&&s[i]<'Z')s[i]=s[i]+33; elseif(s[i]=='Z')s[i]+=6; elseif(s[i]>='a'&&s[i]<='z') { if(s[i]>='a'&&s[i]<='c')s[i]='2'; if(s[i]>='d'&&s[i]<='f')s[i]='3'; if(s[i]>='g'&&s[i]<='i')s[i]='4'; if(s[i]>='j'&&s[i]<='l')s[i]='5'; if(s[i]>='m'&&s[i]<='o')s[i]='6'; if(s[i]>='p'&&s[i]<='s')s[i]='7'; if(s[i]>='t'&&s[i]<='v')s[i]='8'; if(s[i]>='w'&&s[i]<='z')s[i]='9'; } } for(i=0;i cout< return0; } ///////////////// //字符串中字母个数 #include #include usingnamespacestd; intmain() { strings; inti,num=0; getline(cin,s); if(s==""){cout< for(i=0;i if(s[i]>='A'&&s[i]<='Z') num=num+1; cout< return0; } /////////////// #include usingnamespacestd; intmain() {intsum=0,i,n; inta[200]; cin>>n; a[0]=2; for(i=0;i { sum=sum+a[i]; a[i+1]=a[i]+3; } cout< return0;} //合并字符串.cpp #include usingnamespacestd; intmain() { charstr1[20]={"Hello"}; charstr2[20]={"World"}; char*p=str1; char*q=str2; while(*p) { p++; } while(*q) { *p=*q; q++; p++; } *p='\0'; cout< return0; } //////////////字符串排序 #include #include usingnamespacestd; intmain() { strings; chartemp; inti,j,k,m; getline(cin,s); for(i=0;i { for(j=0;j { if(toupper(s[j])<'A'||toupper(s[j])>'Z') { continue; } k=j+1; while((toupper(s[k])<'A'||toupper(s[k])>'Z')&&(k { k++; } if(k==s.length()) {for(m=0;m cout< } if(toupper(s[j])>toupper(s[k])) { temp=s[j]; s[j]=s[k]; s[k]=temp; } } } for(i=0;i return0; } /////////////////// //课本练习题 #include usingnamespacestd; classstu { public: stu(inta,intb) { num=a; score=b; } voidvol() {cout< voidmax(stu*pt); private: intnum,score; }; voidstu: : max(stu*pt) {intda=0,no1=0; for(inti=0;i<5;i++) if((pt+i)->score>da) {da=(*(pt+i)).score;no1=(*(pt+i)).num;} cout< } intmain() { stus[5]={stu(1,23),stu(2,45),stu(3,89),stu(4,90),stu(5,84)}; stu*p; p=&s[0]; p->max(p); return0; } #include #include usingnamespacestd; intmain() { strings; inti; getline(cin,s); for(i=s.length()-1;i>=0;i--) cout< return0; } //华为单词倒排最后输出会多个空格,怎么解决? #include #include //#include usingnamespacestd; intmain() { inti,j,m;//,k; strings; //charq[20]; getline(cin,s); for(i=s.length()-1;i>=0;i--) { if(isalpha(s[i])&&(! isalpha(s[i+1]))) m=i; if(isalpha(s[i])&&(! isalpha(s[i-1]))) {for(j=i;j<=m;j++)cout< s[m+1]='';cout< } //{for(j=i;j<=m;j++)q[s.length()-m-i+j]=s[j]; //s[m+1]='';q[s.length()-m+1-i+j]=s[m+1]; //cout< } //for(k=0;k return0; } ////// #include #include usingnamespacestd; intmain() {inti,j,m; strings,out=""; getline(cin,s); i=s.length()-1; if(i==s.length()-1&&isalpha(s[i]))m=i; for(i=s.length()-2;i>0;i--) { if(isalpha(s[i])&&(! isalpha(s[i+1]))) m=i; if(isalpha(s[i])&&(! isalpha(s[i-1]))) { out+=s.substr(i,m-i+1); out+=""; } } if(i==0&&isalpha(s[i])out+=s.substr(i,m-i+1); for(i=1;i return0; } #include #include usingnamespacestd; intmain() {inti,j,m; strings,out=""; getline(cin,s); for(i=s.length()-1;i>=0;i--) { if(isalpha(s[i])) {m=i; while(i>=0&&isalpha(s[i])) i--;out+=s; } } //华为单词倒排byme #include #include usingnamespacestd; intmain() {inti,j,m; strings,out=""; getline(cin,s); i=s.length()-1; if(i==s.length()-1&&isalpha(s[i]))m=i;//为避免越界将最后一个和第一个字符单独进行判断处理 for(i=s.length()-2;i>0;i--) { if(isalpha(s[i])&&(! isalpha(s[i+1]))) m=i; if(isalpha(s[i])&&(! isalpha(s[i-1]))) { out+=s.substr(i,m-i+1); out+=""; } } if(i==0&&isalpha(s[i])) {if(! isalpha(s[i+1])){out+=s[i];out+="";} else{out+=s.substr(i,m-i+1);out+="";} } for(i=1;i return0; } //方法2 #include #include #include #include #include usingnamespacestd; intfindFirstAlpha(stringstr) { for(inti=0;i { if(isalpha(str[i])) { returni; } } returnstr.size(); } intfindFirstNotAlpha(stringstr) { for(inti=0;i { if(! isalpha(str[i])) { returni; } } returnstr.size(); } intmain(intargc,char*argv[]) { stringstr,tmp,outStr; intl=0,r=0; vector getline(cin,str); l=0; r=findFirstAlpha(str); str.erase(l,r-l); while(str.size()! =0) { l=0; r=findFirstNotAlpha(str); tmp=str.substr(l,r-l); results.push_back(tmp); str.erase(0,r); l=0; r=findFirstAlpha(str); str.erase(l,r); } reverse(results.begin(),results.end()); for(inti=0;i { cout< } cout< system("pause"); return0; } //字符串分隔 #include #include usingnamespacestd; intpart8(stringq) {inta,b,i; a=q.length(); if(a==0)return-1; b=a+8-(a%8); if((a%8)! =0)//字符串的字符个数不是8的整数倍,在后补8-s.length()%8个零 { for(i=a;i q+="0"; } cout< for(i=1;i {if(i%8==0)cout< cout< } } intmain() {strings; intnum,i; cin>>num; for(i=0;i getline(cin,s); for(i=0;i {part8(s); cout< } return0; } ///////// #include #include usingnamespacestd; intpart8(stringq) {inta,b,i; a=q.length(); if(a==0)return-1; b=a+8-(a%8); if((a%8)! =0) { for(i=a;i q+="0"; } cout< for(i=1;i {if(i%8==0)cout< cout< } } intmain() { intnum,i; strings; stringp[10]; cin>>num; cin.ignore(); for(i=0;i {getline(cin,s); p[i]=s;} for(i=0;i {part8(p[i]); cout< } return0; } /////华为字符串反转 #include #include usingnamespacestd; intmain() {strings; getline(cin,s); for(inti=s.length()-1;i>=0;i--) cout< cout< return0; } //huawei兔子总数 #include #include usingnamespacestd; intsum(intn) {intno[100]; if(n==1||n==2)no[n]=1; else no[n]=sum(n-1)+sum(n-2); returnno[n]; } intmain() {intm,i; cin>>i; m=sum(i); cout< return0; } //线性插值未看懂题目要求 #include #include usingnamespacestd; intmain() {intm,n; cin>>m>>''>>n; if(m>=n)return-1; for(inti=1;i<(n-m);i++) } //简单错误记录.cpp //2014.7.4 #include #include usingnamespacestd; constintmaxn=1000; chars[maxn]; intmain() { charstore[8][17]={0}; intcounts[8]={0}; introws[8]={0}; introw,kase=0; while(cin>>s>>row) { intlen=strlen(s); inti; for(i=0;i<16;i++) { if(s[len-1-i]=='\\') break; } i--; intequal=0; for(intj=0;j { if(strcmp(store[j],&s[len-1-i])==0&&row==rows[j]) { equal=1; counts[j]++; break; } } if(! equal) { strcpy(store[kase],&s[len-1-i]); rows[kase]=row; counts[kase]++; kase++; } for(j=0;j { cout< } } return0; } * //挑7 #include usingnamespacestd; boolcontain7(intm) { intn=0; while(m) { if(m%10==7) returntrue; elsem=m/10; } returnfalse; } intmain() {intnum1=0,num2=0; inti,n,num; cin>>n; for(i=7;i<=n;i++) {if(i%7==0)num1++; if((i%7)! =0&&contain7(i))num2++; } num=num1+num2; cout< return0; } //DNA #include #include usingnamespacestd; intlocation(strings,inta) {inti,j,k,num=0,max=0; for(i=0;i<=(s.length()-a);i++) {num=0; for(j=i;j if(s[j]=='G'||s[j]=='C') num++; if(num>max){max=num;k=i;} } returnk; } intmain() {intm,n,r; strings; getline(cin,s); cin>>n; m=location(s,n); for(r=m;r cout< return0; } //数字颠倒需考虑输入为0的情况; #include usingnamespacestd; intmain() {intn,temp=1; cin>>n; if(n==0)cout<<'0'; temp=n; while(temp) {cout< temp=temp/10; } return0; } // #include
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- OJ 题目 部分 答案
![提示](https://static.bdocx.com/images/bang_tan.gif)