太长的话

时间:2019-02-08 05:39:47

标签: java eclipse

这是我对代码部队中“单词太长词”问题的解决方案。 即使我得到正确的输出,但仍然被编解码器报告为对问题的错误答案。

https://codeforces.com/problemset/problem/71/A (链接到问题)

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {

        //charAt() is an inbuilt method which can read a string letter by letter
        Scanner input=new Scanner(System.in);
        int n;
        //System.out.println("how many words do you wish to enter?");
        n=input.nextInt();
        String[] words=new String[n+1];
        for(int i=0;i<n+1;i++)
        {
            words[i]=input.nextLine();
        }

        for(int j=0;j<n+1;j++)
        {
            if(words[j].length()<10)
            {
                System.out.print(words[j]);
            }

            else
            {
                System.out.print(words[j].charAt(0));
                System.out.print(words[j].length()-2);
                System.out.print(words[j].charAt(words[j].length()-1));
            }

            System.out.print("\n");
        }

    }

}

5 个答案:

答案 0 :(得分:0)

问题在于条件,您仅跳过长度小于10的单词,而不考虑长度恰好为10的单词。

 if(words[j].length()<=10)
 {
        System.out.print(words[j]);
 }

更改它应该工作的条件。

答案 1 :(得分:0)

在int j的for循环内执行以下操作-

if(words[j].length()<=10) //boundary check
        {
            System.out.print(words[j]);
        }

        else
        {
            System.out.println(words[j].charAt(0).toString() + words[j].length()-2 + words[j].charAt(words[j].length()-1).toString());
        }

答案 2 :(得分:0)

Because where r you entering string in your program. Once you run, you will get to know.
Btw this is the solution of actual problem.
 public static void main(String[] args){
String str=null;
        int count=0;
        Scanner scanner= new Scanner(System.in);
        System.out.println("enter string :");
        str=scanner.nextLine();
        for(int i=0; i<str.length(); i++) {
            count ++;
        }
        char first=str.charAt(0);
        char last=str.charAt(count-1);
        int num=count-2;
        System.out.println("abbreviation is :" +first+num+last); 
}

答案 3 :(得分:0)

#include<iostream>
#include<string>
#include<sstream> 
using namespace std;

string change_abb(string str)
{
    string result;
  if(str.length()>10)
  {
      int k=str.length()-2;
      stringstream ss;  
      ss<<k;  
      string s;  
      ss>>s;
    result+=str[0];
    result+=s;
    result+=str[str.length()-1];
  }
  else
  {
      result=str;
  }
  return result;
}

int main()
{
  string in_str;
  int n;
  cin>>n;
  for(int i=0;i<n;i++)
  {
    cin>>in_str;
    cout<<change_abb(in_str)<<endl;
  }
}

答案 4 :(得分:0)

这是我在 Python 3 (Answer accepted by Codeforces) 中的答案

n = int(input())
p=""
for i in range(n):
    s = input()
    if len(s)>10:
        p = s[0]+str((len(s)-2))+s[len(s)-1]
    else:
        p=s
    print(p)