UDBOJ 4 abba 1
JUDBO bbaa
思路:所谓的“左旋”k位,就是把前k个数放到字符串的末尾。注意K有可能大于n,所以要进行一次取余操作。
AC代码:
#include<stdio.h> #include<string.h> int main() { char s[1001]; int n, k, i; while(scanf("%s %d", s, &k) != EOF) { n = strlen(s); k = k % n; /*这里很关键*/ for(i = k; i < n; i++) { printf("%c", s[i]); } for(i = 0; i < k; i++) { printf("%c", s[i]); } printf("\n"); } return 0; } /************************************************************** Problem: 1362 User: wusuopuBUPT Language: C Result: Accepted Time:380 ms Memory:912 kb ****************************************************************/
------------------------------------------------------------------------
update:
如果借助一个字符数组来保存左旋操作后的字符串,那么就可以直接当做字符串打印了!
AC代码:
#include<stdio.h> #include<string.h> int main() { char s[1001]; char st[1001]; int n, k, i, j; while(scanf("%s %d", s, &k) != EOF) { n = strlen(s); k = k % n; for(i = k, j = 0; i < n; i++, j++) { st[j] = s[i]; } for(i = 0; i < k; i++, j++) { st[j] = s[i]; } *(st+j) = ‘\0‘; printf("%s\n", st); } return 0; } /************************************************************** Problem: 1362 User: wusuopuBUPT Language: C Result: Accepted Time:60 ms Memory:912 kb ****************************************************************/
原文:http://blog.csdn.net/wusuopubupt/article/details/18406001