首页 > 试题广场 >

最后一词

[编程题]最后一词
  • 热度指数:1915 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定一个可能由任意数量的字母和空格组成的字符串序列,序列中每个只包含字母,不包含任何空格的子序列称为一个单词。请输出一个序列中最后一个单词的长度。

输入描述:
一个由字母和空格组成的字符串


输出描述:
字符串中最后一个单词的长度
示例1

输入

carpe diem

输出

4
print(len(input().split()[-1]))
发表于 2019-08-02 10:56:51 回复(0)
直接输出最后一个字符串长度。
#include<iostream>
#include<string>
usingnamespacestd;
intmain()
{
    string s;
    while(cin>>s) ;
    cout<<s.length()<<endl;
     
}

发表于 2019-07-02 23:10:53 回复(0)
defmain():
    s=input()
    ls=s.split()
    print(len(ls[-1]))

if__name__=='__main__':
    main()
编辑于 2019-07-10 21:24:51 回复(0)
直接分割字符串计算最后一个词的长度
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while((line = br.readLine()) != null){
            String[] words = line.split(" ");
            System.out.println(words[words.length - 1].length());
        }
    }
}


发表于 2020-12-03 22:28:09 回复(0)
#include <bits/stdc++.h>
usingnamespacestd;
intmain(void){
    intcount=0;
    string s;
    getline(cin,s);
    for(inti=0;i<s.size();++i){
        if(s[i] == ' '){
            count=0;
            continue;
        }
        count++;
    }
    cout<<count<<endl;
 
    return1;
}

发表于 2019-06-26 11:21:09 回复(2)
str=input()
print(len(str.split()[-1]))
编辑于 2019-07-15 14:08:24 回复(0)
import java.util.Scanner;

/**
 * 给定一个可能由任意数量的字母和空格组成的字符串序列,
 * 序列中每个只包含字母,不包含任何空格的子序列称为一个单词。
 * 请输出一个序列中最后一个单词的长度。
 * @author Administrator
 *
 */
public class IHandy {
    /**
     * 思路:任意输入一个字符串;将字符串转化为字符数组;查找字符数组中的空格,并记录最后一个空格的位置;
     * 判断最后一个空格位置是不是字符数组的末尾;不是,输出后面的单词;是,输出前面的单词。
     * @param args
     */
    
    public static void main(String[] args) {
        
        Scanner str = new Scanner(System.in);
        System.out.println("请输入字符串序列:");
        /**
         * next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,
         * next()方***自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符。
         * 简单地说,next()查找并返回来自此扫描器的下一个完整标记。完整标记的前后是与分隔模式匹配的输入信息,
         * 所以next方法不能得到带空格的字符串而nextLine()方法的结束符只是Enter键,即nextLine()方法返回的是Enter键之前的所有字符,
         * 它是可以得到带空格的字符串的。
         */
        String string = str.nextLine();
        //将字符串序列转化为字符数组
        char[] ch = string.toCharArray();
        //遍历字符数组,查找空格
        int index = 0;
        int length = 0;
        for(int i = 0; i < ch.length; i++) {
            
            if((ch[i] == ' ') && (i != ch.length-1)) {
                index++;
                length = 0;
                System.out.println();
                System.out.println("第"+index+"个空格。");
            }else if((ch[i] == ' ') && (i == ch.length-1)){
                index++;
                System.out.println();
                System.out.println("第"+index+"个空格。");
            }else {
                length++;
                System.out.print(ch[i]);
            }
        }
        System.out.println();
        System.out.println("最后一个字符串长度为:"+length);
    }

}

发表于 2019-06-27 14:04:22 回复(0)
python程序:
instr = input()
instr= instr.split(" ")   # 按空格分割单词
last_word_len = len(instr[-1])   # 最后一个单词的长度
print(last_word_len)
发表于 2023-10-21 12:43:01 回复(0)
前端开发和Java开发后端开发大数据开发
发表于 2022-08-07 15:09:15 回复(0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入字符串");
            string s1 = Console.ReadLine();
            string s2 = string.Empty;
            int index = s1.LastIndexOf(" ");
            s2 = s1.Substring(index + 1);
            int n = s2.Length;
            Console.WriteLine("最后一个字符串的长度为{0}", n);
            Console.ReadLine();

        }
    }
}

发表于 2020-01-15 09:51:21 回复(0)
import java.util.Scanner;
public class Main{
    public static void main (String[] args){
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] s = line.split(" ");
        System.out.println(s[s.length-1].length());
    }
}

发表于 2019-09-20 10:02:28 回复(0)
function ol(str){
let s = str.split(' ');
let s1 = s.filter(j => j.length>1);
if(s1.length>0){
return s1[s1.length-1].length
}else {return -1}
}
console.log(ol('my heart will go on'))

发表于 2019-07-31 16:02:06 回复(0)
#encoding:utf-8
 
st = input()
strs = st.strip().split(" ")
print(len(strs[-1]))


发表于 2019-07-22 11:53:47 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] ch = s.split(" ");
        int index = 0;
        for (int i = ch.length-1; i >= 0; i --) {
            index = ch[i].length();
            System.out.print(index);
            break;
        }
    }
}

基础题,可以做出来!

发表于 2019-07-17 09:58:47 回复(0)

public class ClassMain{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
String str = scan.nextLine();
System.out.println(getResult(str));
}
}
public static int getResult(String str){
int length = str.length();
return str.substring(str.lastIndexOf(" ")+1 , length).length();
}

}

发表于 2019-07-15 17:07:00 回复(0)
#include<stdio.h>
#include<string.h>

int main(){
    char input[1000] = "\0";
    gets(input);
    int str_len = strlen(input);
    int temp = str_len - 1;
    while(input[temp] != 32 && temp >= 0){
        temp--;
    }
    int last_len = str_len - temp - 1;
    printf("%d\n",last_len);
}
发表于 2019-07-11 17:50:19 回复(0)

这些题考的是什么?

是算法吗?

还是你们的输入输出技巧。。。

真的让我瞠目结舌

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int get_length(const string& s)
{
    if(s.empty())
        return 0;
    int cnt=0;
    int i=s.size()-1;
    while(i>=0 && s[i]==' ')
        --i;
    if(i<0)  return 0;
    for(;i>=0 && s[i]!=' ';--i)
        cnt++;   
    return cnt;
}

int main()
{
    string s;
    int i=0;
    getline(cin,s);
    cout<<get_length(s)<<endl;
    return 0;
}
编辑于 2019-07-20 21:40:43 回复(0)
public class LastWordLengthExam {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String next = sc.nextLine();
        System.out.println(next.length() - next.lastIndexOf(' ') - 1);
    }
} 
发表于 2019-07-09 14:27:03 回复(0)

解题思路

  1. 从后向前遍历字符串,去除字符串最后的空格,并记录字符开始的位置。
  2. 利用前一个操作记录的字符位置开始从后向前遍历,直到遇到空格后停止循环,记录遍历的次数。
    import java.util.*;
    public class Main{
        public static void main(String[] args){
            Scanner sc=newScanner(System.in);
            String s=sc.nextLine();
            System.out.println(cal(s));
        }
        private static int cal(String s){
            if(s==null||s.length()==0){
                return0;
            }
            int res=0;
            int index=s.length()-1;
            while(s.charAt(index)==' '){
                index--;
            }
            for(int i=index;i>=0;i--){
                if(s.charAt(i)==' '){
                    break;
                }
                else{
                    res++;
                }
            }
            return res;
        }
    }
发表于 2019-07-04 17:40:59 回复(0)
import java.util.Scanner;
 
public class Main {
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String [] arr = str.split("\\s+");
        int m = arr.length-1;
        char[] chars = arr[m].toCharArray();
        System.out.println(chars.length);
    }
}
发表于 2019-07-01 13:28:05 回复(0)