编程珠玑(1):将一个n元一维向量向左旋转i个位置。例如当n=8且i=3时,向量abcdefgh 旋转为defghabc
2014-04-29 17:33
525 查看
问题:
将一个n元一维向量向左旋转i个位置。例如当n=8且i=3时,向量abcdefgh旋转为defghabc。
在有限的资源内解决问题的方法:
实际上是一种循环左移。我写的代码如下:
将一个n元一维向量向左旋转i个位置。例如当n=8且i=3时,向量abcdefgh旋转为defghabc。
在有限的资源内解决问题的方法:
实际上是一种循环左移。我写的代码如下:
#include<stdio.h> #include<malloc.h> int main(void) { int i, n, k = 0, j; int m = 0; int num = 0; //每一个字符都会移动记录移动的个数 char *p; char temp; printf("please input the number n and i\n"); scanf("%d%d", &n, &i); //字符串的字符数以及循环左移个数 p =(char*)malloc(sizeof(char)*(n+1)); printf("please input the string\n"); scanf("%s", p); //输入字符 temp = p[0]; while(num != n ) { j = 1; while((k + i * j) % n != m) { p[(k+i*(j-1))%n] = p[(k+i*j)%n]; j++; num++; } m++; p[(k+i*(j-1))%n] = temp; k++; num++; temp = p[k]; } printf("%s", p); }
相关文章推荐
- 用c++编一个3对乒乓球比赛名单的程序
- Struts2_访问Web元素
- C++虚函数和纯虚函数的区别
- Qt多线程编程中的对象线程与函数执行线程
- 在C++ MFC中Single document和Dialog based有什么具体的区别?
- JAVA字符串格式化-String.format()的使用(转)
- 【转载】实用VC++6.0插件
- VB.NET小结——再聊面向对象
- VB.NET小结——再聊面向对象
- PHP获取IP地址及根据IP判断城市实现城市切换或跳转
- Matlab编程和其他语言的区别
- Windows编程之非模态对话框
- 《C#图解教程》读书笔记之二:存储、类型和变量
- 【Python】Eclipse和pydev搭建Python开发环境
- 利用QT_DEBUG_PLUGINS检查Qt的插件问题
- Eclipse安装Maven插件
- mysql数据库无法连接(JDBC)java.net.ConnectException: Connection timed out
- 黑客编程技术学习之-自动关机
- Spring的AOP简单介绍和常用配置(3)-事务配置
- Java 中线程安全的类