CodeM Qualifying Match Q5

问题描述:

给定两个整数 l 和 r ,对于所有满足1 ≤ l ≤ x ≤ r ≤ 10^9 的 x ,把 x 的所有约数全部写下来。
对于每个写下来的数,只保留最高位的那个数码。求1~9每个数码出现的次数。

输入描述:
一行,两个整数 l 和 r (1 ≤ l ≤ r ≤ 10^9)。

输出描述:
输出9行。
第 i 行,输出数码 i 出现的次数。

输入例子:
1 4

输出例子:
4
2
1
1
0
0
0
0
0

运行超时:您的程序未能在规定时间内运行结束,请检查是否循环有错或算法复杂度过大。
case通过率为50.00%

String.substring(0,1) 直接提取字符串第一个字符版

 1 import java.util.Scanner;
 2
 3 public class Main
 4 {
 5     public static void main(String arg[])
 6     {
 7         Scanner sc = new Scanner(System.in);
 8         int L = sc.nextInt();
 9         int R = sc.nextInt();
10         int x=L;
11         String str=null;
12         int high=0;
13         int num[] = new int[9];
14
15         while (x<=R && x<=1000000000)
16         {
17             for(int div=1;div<=x;div++)
18             {
19                 if(x % div == 0)
20                 {
21                     str=String.valueOf(div);
22                     high=Integer.parseInt(str.substring(0,1));    //substring 截止从0到1位置
23                     num[high-1]++;
24                 }
25             }
26             x++;
27         }
28         for(int k=0;k<9;k++)
29         {
30             System.out.println(num[k]);
31         }
32     }
33 }

str.charAt(0) 直接提取字符串第一个字符版,得到char值

import java.util.Scanner;

public class Main_2
{
    public static void main(String arg[])
    {
        Scanner sc = new Scanner(System.in);
        int L = sc.nextInt();
        int R = sc.nextInt();
        int x=L;
        String str=null;
        int high=0;
        int num[] = new int[9];

        while (x<=R && x<=1000000000)
        {
            for(int div=1;div<=x;div++)
            {
                if(x % div == 0)
                {
                    str=String.valueOf(div);
                    high=Integer.parseInt(Character.toString(str.charAt(0)));             //charAT返回的是char,不是string,需要再转化为string
                    num[high-1]++;
                }
            }
            x++;
        }
        for(int k=0;k<9;k++)
        {
            System.out.println(num[k]);
        }
    }
}

Math.pow(10,^)直接求最高位再Math.floor()取整得到doubule值,(int)强制转换为int值

 1 import java.util.Scanner;
 2
 3 public class Main_1
 4 {
 5     public static void main(String arg[])
 6     {
 7         Scanner sc = new Scanner(System.in);
 8         int L = sc.nextInt();
 9         int R = sc.nextInt();
10         int x=L;
11         int len=0;
12         int high=0;
13         int num[] = new int[9];
14
15         while (x<=R && x<=1000000000)
16         {
17             for(int div=1;div<=x;div++)
18             {
19                 if(x % div == 0)
20                 {
21                     len=String.valueOf(div).length();
22                     high=(int)Math.floor(div / Math.pow(10,len-1));                //math.pow(基底,幂)次方,math.floor取整得到doubule值,(int)强制缩进为int值
23                     num[high-1]++;
24                 }
25             }
26             x++;
27         }
28         for(int k=0;k<9;k++)
29         {
30             System.out.println(num[k]);
31         }
32     }
33 }
时间: 2024-08-10 20:34:35

CodeM Qualifying Match Q5的相关文章

CodeM Qualifying Match Q1

问题描述: 具体地说,就是在第二段音频中找到一个长度和第一段音频相等且是连续的子序列,使得它们的 difference 最小.两段等长音频的 difference 定义为: difference = SUM(a[i] - b[i])2 (1 ≤ i ≤ n),其中SUM()表示求和 其中 n 表示序列长度,a[i], b[i]分别表示两段音频的音高. 现在袋鼠先生想要知道,difference的最小值是多少?数据保证第一段音频的长度小于等于第二段音频的长度. 输入描述: 第一行一个整数n(1 ≤

maven -- 问题解决(三)Java compiler level does not match the version of the installed Java project facet

问题: Java compiler level does not match the version of the installed Java project facet 解决方法如下: properties->Java Compiler,修改JDK版本,然后Apply

-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.chan

第一次使用Maven ,在eclipse中执行pom.xml文件的时候报错. -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match. 参考网上的解决方案: 设一个环境变量M2_HOME指向你的maven安装目录 M2_HOME=D:\Apps\apache-maven-3.3.1 然后在Window->Prefe

HDU3081Marriage Match II(二分答案+并查集+最大流SAP)经典

Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2507    Accepted Submission(s): 856 Problem Description Presumably, you all have known the question of stable marriage match. A

eBay搜索排名规则Best Match

对于电商卖家而言,站内搜索时最重要的流量来源,因此作为卖家必须要了解平台的搜索排名规则,只有店铺的操作符合搜索排名规则,才能有效地提升商品的搜索曝光率.对于eBay卖家而言最重要搜索排名规则就是"最佳配/BestMatch".今天就带大家了解什么是ebay Best Match? 什么是Best Match? 第一,Best Match来源: 为了提高买家用户体验,卖家需要迎合买家需求和习惯. 第二,买家和卖家的匹配: 通过搜索排名规则,引导卖家不断提高买家的购物体验.Best Mat

Python学习笔记——基础篇【第五周】——re.match与re.search的区别

正则表达式 语法: import re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0-9]代表匹配0至9的任意一个数字, 所以这里的意思是对传进来的字符串进行匹配,如果这个字符串的开头第一个字符是数字,就代表匹配上了 m = p.match('14534Abc') #按上面生成的正则对象 去匹配 字符串, 如果能匹配成功,这个m就会有值, 否则m为None if m: #不为空代表匹配上了 print(m.group

5.Python里面match()和search()的区别?

Python里面match()和search()的区别? '''match()从第一个字符开始找,如果第一个就不匹配,会直接返回None,不继续匹配.主要用于匹配开头''' #coding=utf-8 import re result = re.match("www","www.cn") result.group() >>'www' # 否则返回None '''search()会在整个字符串中查找,知道找到一个匹配的''' #coding=utf-8 i

Mybatis mapper must match错误

Mybatis mapper must match ((cache-ref|cache|resultMap*|parameterMap*|sql*|insert*|update*|delete*|select*) 出现此问题是由于自己的xml内的mybatis标签未按照指定的顺序来编写. 顺序如下: cache-ref cache resultMap* parameterMap* sql* insert* update* delete* select

js正则表达式的match test exec replace函数

js正则表达式的方法:一种正则在前,一种正则在后: 使用: 1.exec var res = /\-[a-z]/g .exec("font-size"); console.log(res); 得到的结果: 所以返回的是一个数组,第一个为匹配值,第二个是匹配的位置,第三个是输入的数 2.test var res = /\-[a-z]/g .test("font-size");console.log(res); 返回为一个布尔值 3.match var res =(&q