11、(已验证!)约瑟夫问题:n个小孩围成一圈做游戏,给定一个数m,现从s个小孩开始,顺时计数,每数到m,该小孩出列,然后从下个小孩重新数数,数到m时,该小孩出列,如果反复,直到所以小孩出列。(太复杂不会考的!)#include<iostream>using namespace std;int main() int N,M; cout<<"请输入N,M"<<endl; cin>>N>>M; int *a=new int[N]; for(int i=0;i<N;i++) { a[i]=i+1; } int countN=0; int countM=0; for(i=0;;i++) { if(a[i%N]!=-1) { countM++; } if(countM==M) { cout<<a[i%N]<<"\t"; a[i%N]=-1; countN++; countM=0; } if(countN==N) { break; } } delete a; return 0;字符串编程:1、 (已验证!)输入一个字符串,反向输出其字符,如输入是"abcdef",则反向输出"fedcba".(1)、利用string流:#include<iostream>#include <string>using namespace std;string rstring(string s)if(s.length()==1)return s;elsechar c=s[s.length()-1];return c+rstring(s.substr(0,s.length()-1));void main()string str;cout<<"输入一个字符串:"<<endl;cin>>str;cout<<str<<"的反向字符串是"<<rstring(str)<<endl;(2)、数组倒序输出(很简单):#include<iostream>using namespace std;void main()char str[180];int l;cout<<"输入一个字符串:"<<endl;cin>>str;l=strlen(str);cout<<str<<"的反向字符串是:";for (int i=l-1;i>=0;i--)cout<<str[i];cout<<endl;2、(已验证!)将输入字符串中所有的'C'去掉,输出其他字符。#include<iostream>using namespace std;void main()char str[180];int l;cout<<"输入一个字符串:"<<endl;cin>>str;l=strlen(str);cout<<"过滤后为:";for (int i=0;i<l;i++)if (str[i]!='c' && str[i]!='C')cout<<str[i];cout<<endl;3、(已验证!)输入字符串,判断此字符串是否为回文,如"abcd",正数为"abcd",反数为"dcba",不是回文,而"aba",正数是"aba",反数也是"aba",是回文。#include <iostream>using namespace std;void main( ) char str[180],l; int i,t=0; cout<<"输入一个字符串:"; cin>>str; l=strlen(str); for(i=0;i<l/2;i++) if(str[i]==str[l-i-1]) { t=1; break; } if(t==0) cout<<str<<"不是回文!"<<endl; else cout<<str<<"是回文!"<<endl;http://www.yhcyks.com/
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货