尾单词长度、1 3 9 27 81 实现1-121任意算法、去除重复字符并排序、拼音转数字、按要求分解字符串

来源:互联网 发布:逆战塔防挂机宏数据 编辑:程序博客网 时间:2024/05/11 12:04

尾单词长度、1 3 9 27 81 实现1-121任意算法、去除重复字符并排序、拼音转数字、按要求分解字符串。

1:尾单词长度

输入一行,获得单词,并且输出最后一个单词的长度(这里考虑了最后一个单词后有空格的情况)

//最后一个单词的长度  ok
#include<string>
void FunLastWordLength(void){char str[128];cin.getline(str,128);char word[20][20];     //最多存放20个长度为20的单词int i,j,k;i=0;j=0;while(str[i]!='\0'){k=0;    //当前单词定位到下标0if(str[i]==' ')i++;while((str[i]!='\0')&&(str[i]!=' '))word[j][k++]=str[i++];word[j][k]='\0';j++;}int n=0;int m;for(m=j-1;m>=0;m--){if(strlen(word[m])!=0){n=strlen(word[m]);break;}}cout<<n<<endl;}
2、1 3 9 27 81 实现1-121任意算法:

void funcal(){int a[3]={0,1,-1},b[3]={0,3,-3},c[3]={0,9,-9},d[3]={0,27,-27},e[3]={0,81,-81};int i,j,k,m,n,num,l,h=0;int p[5],q[5];cin>>numfor(i=0;i<3;i++)for(j=0;j<3;j++)for(k=0;k<3;k++)for(m=0;m<3;m++)for(n=0;n<3;n++){if (num==(a[i]+b[j]+c[k]+d[m]+e[n]))break;}p[0]=a[i];p[1]=b[j];p[2]=c[k];p[3]=d[m];p[4]=e[n];for(i=0;i<5;i++){    if(p[i]!=0)    q[h++]=p[i];  //新数组里面不包含0的数}for(i=0;i<h;i++)   //按照输出的格式输出if(p[i]>0){if(i==0)    cout<<q[i];else    cout<<"+"<<q[i];}else    cout<<q[i];}

3、去除重复字符并排序

//去除重复字符并排序void delandsort(char *input){char *temp=new char[100];int i=0,len;cout<<"原始字符串为:"<<input<<endl;while(*input){temp[i++]=input[0];len=strlen(input);for(int j=1; j < len; j++){if(input[0]==input[j]){for(int k=j;k<len;k++)input[k]=input[k+1];}}input++;}temp[i]='\0';cout<<"删除重复字符后的字符串为:"<<temp<<endl;char t;int l=strlen(temp);for(int m=0;m<l;m++){for(int n=m+1;n<l;n++){if(temp[m]>temp[n]){t=temp[m];temp[m]=temp[n];temp[n]=t;}}}cout<<"排序后的字符串为:"<<temp<<endl;}

4、拼音转数字

/*拼音转数字输入是一个只包含拼音的字符串,请输出对应的数字序列。转换关系如下:描述:      拼音        yi  er  san  si  wu  liu  qi  ba  jiu  ling      阿拉伯数字        1   2   3    4   5   6    7   8   9    0输入字符只包含小写字母,所有字符都可以正好匹配*/void pinyintonum(char *input,char *num){int j=0;while(*input){switch(*input){    case 'y':if(input[1]=='i')num[j++]='1';break;case 'e':                if(input[1]=='r')num[j++]='2';break;case 's':                if((input[1]=='a') && (input[2]=='n'))num[j++]='3';elseif(input[1]=='i')   num[j++]='4';break;           case 'w':                if(input[1]=='u')num[j++]='5';break;   case 'l':                if((input[1]=='i') && (input[2]=='u'))num[j++]='6';elseif((input[1]=='i') && (input[2]=='n') && (input[3]=='g'))    num[j++]='0';break;   case 'q':                if(input[1]=='i')num[j++]='7';break;   case 'b':                if(input[1]=='a')num[j++]='8';break;   case 'j':                if((input[1]=='i') && (input[2]=='u'))num[j++]='9';break;   default :   break;            }input++;}num[j]='\0';cout<<num<<endl;}

5、按要求分解字符串。

/*按要求分解字符串,输入两个数M,N;M代表输入的M串字符串,N代表输出的每串字符串的位数,不够补0。例如:输入2,8, “abc” ,“123456789”,则输出为“abc00000”,“12345678“,”90000000”*/void cut(int h,int l,char *s[]){int len;char *temp;char *final[100];int a=0;for(int i=0;i<h;i++){temp=s[i];len=strlen(temp);if(len < l){int j=len;while(j<l)temp[j++]='0';temp[j]='\0';cout<<temp<<endl;final[a++]=temp;}else if(len == l)    {    cout<<temp<<endl;final[a++]=temp;}    else          //len>l{ char *t; int k=len/l; int p,q;     for(p=0;p<k;p++){t=new char[l];for(q=0;q<l;q++){    t[q]=temp[q+p*l];}t[q]='\0';    cout<<t<<endl;final[a++]=t;}            if(len%l != 0){                t=new char[l];    int n=0,y=len%l;        while(n < y)         t[n++]=temp[k*l+n];    while(y < l)       t[y++]='0';        t[y]='\0';    cout<<t<<endl;             final[a++]=t;}}}for(int j=0;j<a;j++)cout<<final[j]<<endl;}



0 0
原创粉丝点击