http://acm.uestc.edu.cn/problem.php?pid=1039
Description
小Q在课堂听了老师讲指针与字符串的关系,他很快就写了一个求字符串任意子串的函数。聪明的你,不利用库函数也能很快做到吗?
Input
第一行是一个长度不超过100的字符串。第二行是一个正整数n表示测试数据的组数。下面每一行有两个整数,用空格隔开,表示子串在字符串中的起始位置和取的长度。
Output
每组数据对应有一行子串输出。
Sample Input
abcdefghijklmn
2
3 4
12 4
Sample Output
defg
mn
我的解法,手工测试没问题,但通不过。。。
1 #include <stdio.h> 2 #include <string.h> 3 4 int t,a[1001],b[1001],i,j; 5 char str[110]; 6 7 int main() 8 { 9 gets(str); 10 scanf("%d",&t); 11 for (i=0;i<t;i++) scanf("%d %d",&a[i],&b[i]); 12 int k=0; 13 while (str[k]!=' '){ 14 k++; 15 } 16 for (i=0;i<t;i++) 17 { 18 if ((a[i]+b[i])<=k) 19 { 20 for (j=a[i];j<(a[i]+b[i]);j++) printf("%c",str[j]); 21 printf("\n"); 22 } 23 if ((a[i]+b[i])>k) 24 { 25 for (j=a[i];j<k;j++) printf("%c",str[j]); 26 printf("\n"); 27 } 28 } 29 return 0; 30 } 31