sdut java lab 7.6

7-6 String Builder

分数 10

全屏浏览

切换布局

作者 翁恺

单位 浙江大学

You are going to read four numbers: n, a, b and c, like this:

12 2 5 3

First, n is used to build up a string from 0 to n, like this:

0123456789101112

is a string build up for n=12.

Then, in all the digits from index a to index b, count the appearence of digit c.

For the string above, 8 13 is:

891011

Thus the appearence of 1 is 4.

Input Format:

Four positive numbers, nab and c, where a<b<n<10000, and 0<=c<=9..

Output Format:

One number represnets the length of the generated string.
One number represents the apprence of c.
There is a space between the two numbers.

Sample Input:

12 2 5 3

Sample Output:

16 1

代码长度限制

4 KB

时间限制

400 ms

内存限制

64 MB

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
         String arr = "";
         int n = sc.nextInt();
         int temp;
         StringBuilder abc = new StringBuilder("");
         String pp;
         for(int i = 0 ; i <=n;i++ ){
              temp = i;
              pp = String.valueOf(temp);
              abc.append(pp);
              
         }
         arr = abc.toString();
        
        int x = sc.nextInt();
        int y = sc.nextInt();
        char z = sc.next().charAt(0);
        int len = arr.length();
        int max = 0;
        for(int i = x;i<=y;i++){
          if(arr.charAt(i) == z){
            max+=1;
          }
        }
        System.out.print(len+" ");
        System.out.println(max);
        System.exit(0);
        

        
       
}
}

这段Java代码的目的是根据用户输入创建一个字符串,然后统计该字符串中指定范围内出现特定字符的次数。以下是对每一行代码的解释:

 

import java.util.Scanner;

导入Java的Scanner类,用于读取用户的输入。

public class Main {

定义了一个名为Main的公共类。

public static void main(String[] args) {

定义了程序的主入口点main方法。

Scanner sc = new Scanner(System.in);

创建了Scanner类的一个实例sc,用于从标准输入读取数据。

String arr = "";

初始化一个空字符串arr,它将用于存储从0到n的连续整数转换为字符串的结果。

int n = sc.nextInt();

从用户那里读取一个整数n

int temp;

声明一个临时变量temp,用于在循环中存储当前的数值。

StringBuilder abc = new StringBuilder("");

创建了一个StringBuilder对象abc,用于高效地构建字符串。

String pp;

声明一个String类型的变量pp,用于存储转换后的字符串形式的数字。

 

for (int i = 0; i <= n; i++) {

开始一个从0到n的for循环。

temp = i;

将循环变量i的值赋给临时变量temp

pp = String.valueOf(temp);

temp的值转换为字符串,并存储在变量pp中。

abc.append(pp);

pp字符串追加到StringBuilder对象abc中。

}

结束for循环。

arr = abc.toString();

StringBuilder对象abc中的内容转换为字符串,并赋值给变量arr

int x = sc.nextInt();

从用户那里读取另一个整数x

int y = sc.nextInt();

从用户那里读取第三个整数y

char z = sc.next().charAt(0);

从用户那里读取一个字符,将其转换为字符串,然后取第一个字符(即整个字符串,因为只有一个字符),并将其存储在变量z中。

int len = arr.length();

获取字符串arr的长度,并将其存储在变量len中。

 

int max = 0;

初始化一个计数器max,用于计数字符z在特定范围内出现的次数。

 

for (int i = x; i <= y; i++) {

开始一个从xy的for循环。

 

if (arr.charAt(i) == z) {

检查字符串arr中索引为i的字符是否与字符z相同。

 

max += 1;

如果相同,将计数器max的值增加1。

 

}

结束if语句。

 

}

结束for循环。

 

System.out.print(len + " ");

打印字符串arr的长度,后面跟一个空格。

 

System.out.println(max);

打印变量max的值,即字符z在指定范围内出现的次数。

 

System.exit(0);

正常退出程序。

 

}

结束main方法。

 

}

结束Main类。

这个程序首先读取一个整数n,然后创建一个字符串arr,其中包含从0到n的连续整数。接着,程序读取三个值xy和一个字符z,然后统计在字符串arr的索引从xy的范围内字符z出现的次数,并将字符串的长度和这个计数结果打印出来。

  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值