首页 \ 问答 \ 打印连续数字(Printing consecutive numbers)

打印连续数字(Printing consecutive numbers)

我已经对整数数组{1,2,3,7,9,24,25,26,78}进行了排序,并希望连续打印以打印{1-3,7,9,24-26,78}。 也就是说,每次在阵列中出现一组连续数字时,我想打印出从最小数字到最大数字的范围。

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Consecutive{

public static void main(String[] args){
    int[] a={1,2,3,7,9,24,25,26,78};

    for(int i=0;i<a.length;i++){
        int count=0;
        int first=0;

    /*  System.out.println(i);*/
        first=a[i];

        if(a[i+1]-a[i]==1){

            count++;

            int last=a[i]+count;
            i++;
            System.out.println(first + " " + last);

                }else{


                System.out.println(a[i]);

                }

    }
}
}

I have sorted integer array {1,2,3,7,9,24,25,26,78} and would like to print consecutive to print {1-3,7,9,24-26,78}. That is, every time there is a set of consecutive numbers occurring in the array, I would like to print out the range from the minimum number to the maximum number.

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Consecutive{

public static void main(String[] args){
    int[] a={1,2,3,7,9,24,25,26,78};

    for(int i=0;i<a.length;i++){
        int count=0;
        int first=0;

    /*  System.out.println(i);*/
        first=a[i];

        if(a[i+1]-a[i]==1){

            count++;

            int last=a[i]+count;
            i++;
            System.out.println(first + " " + last);

                }else{


                System.out.println(a[i]);

                }

    }
}
}

原文:https://stackoverflow.com/questions/36963019
更新时间:2023-04-04 09:04

最满意答案

简短回答:使用大括号$ \ alpha_ {mod} $

更长的版本:Matplotlib使用LaTeX格式的字符串。 在LaTeX中,如果您希望下标包含多个字母,则必须使用大括号。

import matplotlib.pyplot as plt
ax = plt.gca()
ax.text(2, 2, r'$\alpha_{mod}$', fontsize=10)
y = [2,3,4,5]
x = [1,2,3,4]
plt.plot(x,y)
plt.show()

Short answer: Use curly brackets $\alpha_{mod}$

Longer version: Matplotlib uses LaTeX format for strings. In LaTeX, if you want a subscript to include more than one letter you have to use curly brackets.

import matplotlib.pyplot as plt
ax = plt.gca()
ax.text(2, 2, r'$\alpha_{mod}$', fontsize=10)
y = [2,3,4,5]
x = [1,2,3,4]
plt.plot(x,y)
plt.show()

相关问答

更多
  • 轴是情节的轴心,即获得刻度和刻度标签的东西。 坐标轴是您的绘图出现的区域。 Axis is the axis of the plot, the thing that gets ticks and tick labels. The axes is the area your plot appears in.
  • Matplotlib中的坐标轴位置相似。 您可以使用轴的get_position和set_position方法。 import matplotlib.pyplot as plt ax = plt.subplot(111) pos1 = ax.get_position() # get the original position pos2 = [pos1.x0 + 0.3, pos1.y0 + 0.3, pos1.width / 2.0, pos1.height / 2.0] ax.set_positi ...
  • 简短回答:使用大括号$ \ alpha_ {mod} $ 更长的版本:Matplotlib使用LaTeX格式的字符串。 在LaTeX中,如果您希望下标包含多个字母,则必须使用大括号。 import matplotlib.pyplot as plt ax = plt.gca() ax.text(2, 2, r'$\alpha_{mod}$', fontsize=10) y = [2,3,4,5] x = [1,2,3,4] plt.plot(x,y) plt.show() Short answer: Use ...
  • 您可以从网格获取轴手柄,这只是一个带有ax=grid[3]的列表,然后使用xticks = ax.xaxis.get_major_ticks()和xticks[1].label1.set_visible(False) 。 作为一个最小的例子, import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import AxesGrid import numpy as np from matplotlib.cbook import get_sampl ...
  • 我想你几乎在那里。 不知道你的绘图功能是什么,我只是做了一个虚拟的插图。 import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec def dummy_plots(): """ Return a 1d array of dummy plots. """ _, ax_arr = plt.subplots(1, 9) for ax in ax_arr.flat: ax ...
  • 抱歉,而不是xlabels你需要创建一个轴,我们ax.set_xticklabels: from matplotlib.pyplot import figure fig = figure() ax = fig.add_subplot(111) def mk_labels(vals): labels = [] for i in vals: if i % 60 == 0: labels.append("Some new special label") ...
  • 正如我在评论中所说,阅读如何将pyplot函数附加到图形实例? 有关Op和matplotlib状态机接口之间差异的解释。 你应该修改你的绘图功能 def plot(..., ax=None, **kwargs): if ax is None: ax = gca() ax.plot(..., **kwargs) As I said in the comment, read How can I attach a pyplot function to a figure insta ...
  • 轴bbox不包括轴的“内部”之外的任何内容(例如,它不包括刻度标签,标题等) 一个快速解决这个问题的方法就是在你进行blitting时抓住整个区域。 (例如background = canvas.copy_from_bbox(fig.bbox) ) 如果您有多个子图并且只想为其中一个子图设置动画,则可能会出现问题。 在这种情况下,您可以按照background = canvas.copy_from_bbox(ax.bbox.expanded(1.1, 1.2)) 。 但是你必须猜测你需要的比率。 如果你需要 ...
  • 以下两件事对我有用(注意你的大写): from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d import axes3d 但是我为你所写的内容得到了同样的错误(用小写的d表示d)。 请注意,两个有效的导入不相同。 The following two things work for me (watch your capitalization): from mpl_toolkits.mplot3d import Axes3D from ...
  • 您需要告诉netwkorkx要绘制哪些轴,如果不这样,它将绘制到当前活动的轴( plt.gca()返回( doc )。 nx.drawing.nx_pylab.draw_networkx_nodes(..., ax=Axes[0]) nx.drawing.nx_pylab.draw_networkx_nodes(..., ax=Axes[1]) 作为旁注,你不应该使用驼峰式实例变量( pep8 ),它可以倾向于与类名冲突(在本例中为matplotlib.axes.Axes )。 You need to t ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)