博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
华为机考2
阅读量:7289 次
发布时间:2019-06-30

本文共 8833 字,大约阅读时间需要 29 分钟。

#include 
using namespace std;void func(int *p, int n, int k);void main(){ int a[]={
1,2,3,4,5}; int i; func(a,5,2); for(i=0;i<5;i++) cout<
<<" "; cout<
=0)//右移 { while(k) { temp=p[n-1]; for(i=n-1;i>0;i--) p[i]=p[i-1]; p[0]=temp; k--; //临时数组存放最后一个数据,然后依次后移,k--; } } else if(k<0) { k=k*(-1);//左移 while(k) { temp=p[0]; for(i=1;i
View Code

7、数组循环移位

10.将字符串中的所有字母都替换成该字母的下一个字母

 

#include 
using namespace std;#include
#include
#include
void func(char *p);void main(){ char str1[20]; printf("enter:"); gets(str1); func(str1); puts(str1); }void func(char *p){ char ch; while(*p) { ch=*p;//临时字符 //判断是否是字符,且不是z或者Z if(isalpha(*p)&&(*p!='z')&&(*p!='Z'))、、 *p=ch+1; else if(*p='z') *p='a'; else if(*p='Z') *p='A'; p++; }}

回文判断

#include
using namespace std;bool func(int m);void main(){ int m; cout<<"enter a number:"<
>m; cout<
<
View Code

字符串转成整数

#include 
#include
#include
using namespace std;int func(char a[]);void main(){ char a[]={
'1','2','3','4','\0'}; cout<
<
View Code

 

 

下面就说说为什么字符减'0'可以到相应的整数。现在比如我们要字符‘1’转换成数字1,就这么一个变化,我们看到了大家注意了字符型常量用''括起来的原因是,它们在计算机中都以各自的ASCII表示。而‘1’的对应编码是49的二进制码,但是我们的数字1,就等于1呀,所以为了由原来的‘1’实际上就是49的二进制变成现在的1对应的二进制1,只好用49-48=1了。但是在ASCII码里‘0’对应的刚好是48的二进制码,所以我们转换的时候只需要‘1’-‘0’=1;就可以了。而数字的ASCII码是按顺序规定的。所以其它字符要转换成数字都可以用减‘0’来表示。比如‘2’的ASCII是50,而我们要得到数字2,于是用‘2’-48=2了。看来当我们知道数据在计算机中的存储规则的时候,问题就迎刃而解了。

     大小写字母的转换:先看ASCII码:a~z是97~122的二进制,而A~Z是65~90的二进制编码,于是我们就得出:大写字母=小写字母-32 ;这个公式了。当然这里的32我也可以这么写‘Z’=‘z’-'空格'。因为空格的ASCII码是32对应的二进制编码。

#include 
#include
int main(void){ float n; char *str = "12345.67"; n = atoi(str); printf("string = %s integer = %f\n", str, n); return 0;}
View Code
#include 
#include
int main(){ char a[] = "-100" ; char b[] = "123" ; int c ; c = atoi( a ) + atoi( b ) ; printf("c = %d\n", c) ; return 0;}
View Code

int atoi(const char *nptr);

 

参数nptr字符串,如果
第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回 数。否则,返回零,头文件: #include < >

13.求一个二维数组每列的最小值

 

#include 
using namespace std;void fun(int input[3][4], const int m, const int n, int output[4]);int main(){ int input[3][4] = { {
21,48,86,92}, {
10,23,12,69}, {
46,78,49,13}}; int output[4]; fun(input, 3, 4, output); cout<<"原二维数组是:"<
input[j][i]) { output[i] = input[j][i];//依次比较得到每一列的最小值 } } }}
View Code

 

14.连续字符统计(如AABCCCD:A2B1C3D1)

 

#include 
#include
#include
using namespace std;void func(char str[],int len);void main(){ char str[20]; int len; cout<<"enter:"; gets(str); len=strlen(str); func(str,len);}void func(char str[], int len){ int count=1; int i; for(i=0;i
View Code

15.找出一个字符串中是否包含相同的子字符串(要求子串长度大于等于2)

 

包含文件:string.h函数名: strstr函数原型:extern char *strstr(char *str1, char *str2);功能:从字符串str1中查找是否有字符串str2,如果有,从str1中的str2位置起,返回str1中str2起始位置的指针,如果没有,返回null。返回值:返回该位置的指针,如找不到,返回空指针。例子:char str[]="1234 xyz";char* str1=strstr(str,"34");cout<
<

 

原型:char * strncpy(char *dest, char *src,size_tnum);功能:(c/c++)复制src中的内容(字符,数字、汉字....)到dest,复制多少由num的值决定,返回指向dest的指针。如果遇到null字符('\0'),且还没有到num个字符时,就用(num - n)(n是遇到null字符前已经有的非null字符个数)个null字符附加到destination。注意:并不是添加到destination的最后,而是紧跟着由source中复制而来的字符后面。下面举例说明[1]:char des[] = "Hello,i am!";char source[] = "abc\0def";strncpy(des,source,5);此时,des区域是这样的:a,b,c,\0,\0,逗号,i,空格,a,m,!注意:\0,\0并不是添加在!的后面。

没看懂

#include 
#include
using namespace std;int fun(char* str, int n){ char *temp = new char[n+2]; char *str2 = str; int sum = 0; int outsum = 0; char* t; for(int i = 0; i < n; i++) { for(int j = 2; j <= n-i; j++) { sum = 0; str2 = str;//赋 memset(temp,0,(n+2)*sizeof(char));//初始化0 strncpy(temp, str+i, j);// while((t = strstr(str2,temp)) != NULL) { sum++; str2 = t+j; } if( sum > outsum) { outsum = sum; } } } if(outsum == 1) return 0; return outsum; }int main(){ char strstr[1000]; memset(strstr,0,sizeof(strstr)); char *s = strstr; cin >> s; int n =strlen(s); int outsum; outsum = fun(s,n); cout << outsum << endl; system("pause"); return 0;}
View Code

16.已知:yi er san si wu liu qi ba jiu 分别对应123456789,对一段只含有这几种字符的字符串进行转换,转换成相应的数字

 

#include 
#include
using namespace std;char* sss[9] = {
"yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};int fun(char* str){ int i; int sum = 0; int d = 0; i = 0; int j; while(str[i] != '\0') { if(str[i] == 'y' || str[i] == 'e'|| str[i] == 'w' || str[i] == 'q' || str[i] == 'b') d = 2; else if(str[i] == 'l' || str[i] == 'j') d = 3; else if(str[i] == 's') { if(str[i+1] == 'a') d = 3; else d = 2; }//确定碰到字符时移动的距离d for(int k = 0; k < 9; k++) {
if(strncmp(str+i,sss[k],d) == 0)//匹配成功 j = k+1;//实际数字 sum = 10*sum + j; } i = i+d;//每比较一个字符串移动的距离 //不考虑字符串不匹配的情形 } return sum;}int main(){ char strstr[1000]; memset(strstr,0,sizeof(strstr)); char *s = strstr; cin >> s; int outsum; outsum = fun(s); cout << outsum << endl; system("pause"); return 0;}

17.删除字符串中字符个数最少的字符串

暂时没看懂

#include 
#include
using namespace std;char* fun(char* str, int n){ int i=0; int j=0; int hash[256] = {0}; char *result = new char[n+1]; memset(result,0,(n+1)*sizeof(char));//开辟字符指针并初始化 for( i = 0; i < n; i++) { hash[str[i]]++; }//每个字符的个数。用哈希值表示 int min = 0x7fffffff; for( i = 0; i < 256; i++) { if(hash[i] < min && hash[i] != 0) min = hash[i]; } for(i = 0,j = 0; i < n; i++) { if(hash[str[i]] != min) { result[j++] = str[i]; } } return result;}int main(){ char strstr[1000]; memset(strstr,0,sizeof(strstr)); char *s = strstr; cin >> s; char *re = NULL; re = fun(s,strlen(s)); cout << re << endl; system("pause"); return 0;}
View Code

 

C:#include "stdio.h" //这个头文件包含gets()函数int main(void){char str1[5];gets(str1);printf("%s\n", str1);return 0;}C++:#include
using namespace std;int main(){char str[100];gets(str);cout<
<

18.比较两个字符串,相等返回1,不等返回不等的位置

#include
#include
#include
using namespace std;void func(const char *str1,const char*str2); void main(){ char *str1,*str2; str1=new char[256]; str2=new char[256]; memset(str1,0,256*sizeof(char)); memset(str2,0,256*sizeof(char)); cout<<"enter:"<
View Code

19.比较两个字符串是否相等,大小写也算相等

#include
#include
#include
using namespace std;bool func(const char *str1,const char*str2); void main(){ char *str1,*str2; bool m; str1=new char[256]; str2=new char[256]; memset(str1,0,256*sizeof(char)); memset(str2,0,256*sizeof(char)); cout<<"enter:"<
View Code

20.将一句英文的每个单词的第一个字母大写

#include
#include
#include
using namespace std;void func(char *ptr); void main(){ char *ptr; ptr =new char[256]; memset(ptr, 0, sizeof(ptr)); cout<<"please input an English sentence:"<
View Code

 

 

swap(a,b)值交换的四种方法:
[cpp] 
void swap(int &a, int &b)  
{  
    //方法一:  
    int tmp = 0;  
    tmp = b;  
    b = a;  
    a = tmp;  
    //方法二:  
    //a = a+b;  
    //b = a-b;  
    //a = a -b;  
    //方法三:  
    //a ^= b ^= a ^= b;  
    //方法四:  
    //a = a+b-(b=a);  
}  
  
int main(void)  
{  
    int a = 3;  
    int b = 4;  
  
    printf("before swap: a = %d, b = %d\n", a, b);  
    swap(a, b);  
    printf("after swap: a = %d, b = %d\n", a, b);  
  
    return 0;  
}  
结果:
before swap: a = 3, b = 4
after swap: a = 4, b = 3
 
    关于传参方式有三种:
值传参、地址传参、引用传参(C++方法),上面使用的是第三种,引用传参,因为这种传参方式使得swap里面实现更为直观。当然,也可以使
用第二种传参方式地址传参,不过,值传参是不行的哦。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载地址:http://wsdjm.baihongyu.com/

你可能感兴趣的文章
敏捷AI | NLP技术在宜信业务中的实践【构建用户画像篇】
查看>>
python版亲戚关系计算器
查看>>
送给程序员们的经典电子书大礼包
查看>>
SQL注入关联分析
查看>>
应用Promise封装Ajax实践
查看>>
渗透&&探测 ( ICMP 篇)
查看>>
容器监控实践—Prometheus的配置与服务发现
查看>>
dubbo源码解析(三十九)集群——merger
查看>>
PAT A1022
查看>>
捋一捋React的生命周期
查看>>
【跃迁之路】【731天】程序员高效学习方法论探索系列(实验阶段488-2019.2.21)...
查看>>
HTTP中Accept与Content-Type区别
查看>>
RunC容器逃逸漏洞席卷业界,网易云如何做到实力修复?
查看>>
PAT A1043
查看>>
SAP S/4HANA生产订单的BAdI增强点之Initialize方法
查看>>
css加载会造成阻塞吗
查看>>
天天都在使用CSS,那么CSS的原理是什么呢?
查看>>
可视化开发脚手架
查看>>
springboot jar 启动脚本
查看>>
现代JS中的流程控制:详解Callbacks 、Promises 、Async/Await
查看>>