鱼C论坛

 找回密码
 立即注册
查看: 2213|回复: 3

[已解决]字符串:判断是否旋转(大家来练练)

[复制链接]
发表于 2021-6-30 17:21:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
【问题描述】

字符串的旋转是指:把字符串前面的若干字符移动到字符串的尾部,比如:把字符串"abcdef"前2位字符移动到后面得到字符串为“cdefab“,可以把字符串看成两段,分别即为XY,左旋转相当于把字符串XY变成YX。编写一个函数,判断字符串s1是否由字符串s2旋转而来,函数头:is_rotation(s1,s2)

【输入形式】

分行输入两个字符串
【输出形式】

成立输出True,不成立输出False
【样例输入】

stringbook

bookstring
【样例输出】

True
最佳答案
2021-6-30 17:42:58
本帖最后由 连帅帅 于 2021-6-30 17:46 编辑

java版本的:
  1. package com.lian.controller;

  2. import java.util.Scanner;

  3. /**
  4. * @author :LSS
  5. * @description: String rotation
  6. * @date :2021/6/30 17:28
  7. */
  8. public class Test {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         String next = scanner.next();
  12.         String next1 = scanner.next();
  13.         int length = next.length();
  14.         int i1 = length / 2;
  15.         if (length % 2 == 0) {
  16.             next = next.substring(i1) + next.substring(0, i1);
  17.         } else {
  18.             next = next.substring(i1 + 1) + next.substring(i1, i1 + 1) + next.substring(0, i1);
  19.         }
  20.         if (next.equals(next1)){
  21.             System.out.println("True");
  22.         }else{
  23.             System.out.println("False");
  24.         }
  25.     }
  26. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-30 17:42:58 | 显示全部楼层    本楼为最佳答案   
本帖最后由 连帅帅 于 2021-6-30 17:46 编辑

java版本的:
  1. package com.lian.controller;

  2. import java.util.Scanner;

  3. /**
  4. * @author :LSS
  5. * @description: String rotation
  6. * @date :2021/6/30 17:28
  7. */
  8. public class Test {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         String next = scanner.next();
  12.         String next1 = scanner.next();
  13.         int length = next.length();
  14.         int i1 = length / 2;
  15.         if (length % 2 == 0) {
  16.             next = next.substring(i1) + next.substring(0, i1);
  17.         } else {
  18.             next = next.substring(i1 + 1) + next.substring(i1, i1 + 1) + next.substring(0, i1);
  19.         }
  20.         if (next.equals(next1)){
  21.             System.out.println("True");
  22.         }else{
  23.             System.out.println("False");
  24.         }
  25.     }
  26. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-30 20:48:11 | 显示全部楼层


。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-1 08:48:47 | 显示全部楼层

只会java啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 01:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表