华为2013校园招聘上机笔试题-数字转化为字符串,并判断有无重复字符串

2013华为校园招聘机试题9月10日题

题目及参考解答来源:http://blog.csdn.net/caollcool/article/details/7972382

5.【题目】2013华为校园招聘机试题目题5:将一个int类型的数字转化为字符串,并判断有无重复字符串(长度应大于2)有返回1,无返回2  

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define MAX 200
 5 
 6 int ItoAandRepeat(unsigned int theNum,char *s)
 7 {
 8     char str[MAX];
 9     int i,j,k;
10     char *p, *q;
11 
12     itoa(theNum, str, 10);
13     strcpy(s,str);
14 
15     for(i=0;i<strlen(str)-2;i++)
16     {
17         p = s+i;
18         for(j=i+2;j<strlen(str)-1;j++)
19         {
20             q = s+j;
21             for(k=0;k<strlen(str)-j;k++)
22                 if(*(p+k)!=*(q+k))
23                     break;
24             if(k>1)
25                 return 1;
26         }
27     }
28 
29     return 2;
30 }
31  //测试主函数
32 void main()
33 {
34   int number,temp1,re;
35   char s[MAX];
36   printf("请输入一个整数!\n");
37   temp1=scanf("%d",&number);
38   while(temp1==1)
39   {
40     re=ItoAandRepeat(number,s);
41     printf("re=%d\n",re);
42     printf("\n请输入下一个整数!\n");
43     temp1=scanf("%d",&number);
44   }
45 }

思路:

对字符串或数组的指针需要严格控制边界。不同于原帖提供的方法,本法只输出最后的判断,并为保存重复子字符串的功能实现。

posted on 2012-09-22 15:12  Raphael Lou  阅读(243)  评论(0编辑  收藏  举报

导航