某公司门禁密码使用动态口令技术。初始密码为字符串 password
,密码更新均遵循以下步骤:
- 设定一个正整数目标值
target
- 将
password
前target
个字符按原顺序移动至字符串末尾
请返回更新后的密码字符串。
char* dynamicPassword(char* password, int target) {int n = strlen(password);char* ans = (char*)calloc(1, sizeof(char) * (n + 1));strncpy(ans, password + target, n - target);strncpy(ans + n - target, password, target);return ans;
}
//思路:1.先把password里的字符存起来
// 2.再利用target把其中的前几个存起来并减去
// 3.把2存起来的放到减去的后面
总结:遇到字符型的 可以存储起来 利用数据结构