首页 \ 问答 \ 为什么需要重复减去1?(Why does quot need to be repeatedly subtracted by 1?)

为什么需要重复减去1?(Why does quot need to be repeatedly subtracted by 1?)

这个问题是基于这个线程编程之谜:你如何将Excel列名转换为数字?

以下是将该列号转换为excel列名称的问题的代码

public String getColName (int colNum) {

   String res = "";
   int quot = colNum;
   int rem;        
    /*1. Subtract one from number.
    *2. Save the mod 26 value.
   *3. Divide the number by 26, save result.
   *4. Convert the remainder to a letter.
   *5. Repeat until the number is zero.
   *6. Return that bitch...
   */
    while(quot > 0)
    {
        quot = quot - 1;
        rem = quot % 26;
        quot = quot / 26;

        //cast to a char and add to the beginning of the string
        //add 97 to convert to the correct ascii number
        res = (char)(rem+97) + res;            
    }   
    return res;
}

我彻底测试了这段代码并且它可以正常工作,但是我有一个问题,为什么这条线需要重复才能工作

            quot = quot - 1;

根据我的理解,需要使用quot来将col编号映射到远离'a'的距离。 这意味着1应该映射到距离'a'的距离为0,距离'a'的距离为2到1,依此类推。 但是你不需要减去这一次来解释这一点吗? 不是循环,我的意思是最终,

            quot = quot / 26;

将停止循环。


This question is based off this thread Programming Riddle: How might you translate an Excel column name to a number?

Here is code from that question to translate a column number to an excel column name

public String getColName (int colNum) {

   String res = "";
   int quot = colNum;
   int rem;        
    /*1. Subtract one from number.
    *2. Save the mod 26 value.
   *3. Divide the number by 26, save result.
   *4. Convert the remainder to a letter.
   *5. Repeat until the number is zero.
   *6. Return that bitch...
   */
    while(quot > 0)
    {
        quot = quot - 1;
        rem = quot % 26;
        quot = quot / 26;

        //cast to a char and add to the beginning of the string
        //add 97 to convert to the correct ascii number
        res = (char)(rem+97) + res;            
    }   
    return res;
}

I tested this code thoroughly and it works but I have a question about what this line needs to be repeated for this to work

            quot = quot - 1;

From my understanding the quot is needed to map the col number to distance away from 'a'. That means 1 should map to 0 distance from 'a', 2 to 1 distance away from 'a' and so on. But don't you need to subtract this one once to account for this? Not in a loop I mean eventually,

            quot = quot / 26;

will stop the loop.


原文:https://stackoverflow.com/questions/28119064
更新时间:2023-10-28 12:10

最满意答案

我在构造函数中添加到下面的volatile列表中的项目可能对其他线程不可见是否正确

如果列表为非null,则对列表的所有写入都将可见。 存在一种竞争条件,其中默认的write(null)对于看到非null ProductPrice的线程可能是可见的。 如果该字段是最终的,则不是这样。

在这种情况下,我可以让变量final,并获得让所有项目对其他线程可见的相同效果。

是的,这是最好的解决方案。


Is it correct that the items that I added in my constructor to my volatile linked list below may not be visible to other threads

If the list is non-null, then all the writes to the list will be visible. There is a race condition in which the default write (null) may be visible to a thread seeing a non-null ProductPrice. This isn't true if the field is final.

and in this case could i just make the variable final and get the same effect of having all items visible to other threads.

Yes, this is the best solution.

相关问答

更多
  • 详细步骤如下:    安装JDK      从 http://Java.sun.com/下载jdk-1_5_0_04-windows-i586-p.exe      安装到指定路径,我选择D:\jdk1.5.0      配置环境变量:      Java_HOME: D:\jdk1.5.0   PATH: D:\jdk1.5.0\bin;   CLASSPATH: .;D:\jdk1.5.0\lib\tools.jar;   D:\jdk1.5.0\jre\lib\rt.jar;   安装Eclipse ...
  • Java语言是比较好学的,当然可以从零开始学,不过没有英语基础和面向对象基础学起来可能会困难一点。如果有学过C++或者C的话就容易多了。书要找本基础点的。你的那本书不知道怎么样,不过估计你一点基础没有是很难看懂的。所以我认为最好是看视频。去 www.verycd.com搜索尚学堂吧。马士兵老师讲的Java简直棒极了。 Java语言是基础,在这基础之上可以学习web开发。刚开始学的Java叫Java SE----标准版本。web开发的那个叫Java EE,企业版。
  • 从我所了解的情况看,总是看起来好像高速缓存在写入后被刷新,并且总是以读取时从内存直接读取的方式出现。 其效果是一个线程总是会看到来自另一个线程的写入结果,并且(根据Java内存模型)永远不会有缓存的值。 然而,实际的实现和CPU指令会因架构而异。 如果你在多个线程中增加变量,或者检查它的值并采取一些行动,它并不能保证正确性,因为显然没有实际的同步。 如果只有线程写入变量而其他人全部读取,则通常只能保证正确执行。 另请注意,一个64位非易失性变量可以作为两个32位变量读取/写入,因此32位变量在写入时是原子性 ...
  • 在这个非常具体的例子中,我认为你可以没有锁定变量重新分配。 一般来说,我认为你最好使用AtomicReference而不是volatile变量,因为内存一致性效果相同,意图更清晰。 In this very specific example, I think you would be OK with no locking on the variable reassignment. In general, I think you are better off using an AtomicReference ...
  • 在Java中声明一个静态变量,意味着只有一个副本,无论创建了多少个类的对象。 即使没有创建Objects ,变量也可以访问。 但是,线程可能具有本地缓存的值。 当变量是volatile而不是静态时 ,每个Object都有一个变量。 所以,表面看来,与正常变量没有区别,但与静态完全不同。 然而,即使使用Object字段,线程也可能在本地缓存变量值。 这意味着如果两个线程同时更新同一个对象的变量,并且该变量未被声明为volatile,则可能存在一个线程在缓存中具有旧值的情况。 即使您通过多个线程访问静态值,每个 ...
  • 首先,volatile不会强制线程使用主内存。 它强制线程的行为就好像它们被迫使用主内存一样。 这很重要,因为实际使用主内存会减慢现代系统的速度。 幸运的是,这绝对不会发生。 其次,CPU拥有多少实际内核并不重要。 单核CPU可以完美地模拟双核CPU,如果需要,JVM实现可以自由地执行。 进行某种形式的同步的要求是Java语言要求,并且与硬件无关 - Java的全部要点之一就是您不必担心这一点。 First, volatile does not force threads to use main memor ...
  • 是的,他们会是一个很好的比赛,在Dobbs博士这里有一篇很好的文章。 简而言之,有序的原子变量可以安全地在多个线程上同时读写,而不需要进行任何明确的锁定,因为它们提供了两个保证:它们的读写保证按照它们出现在程序源代码中的顺序执行; 并且每次读或写都保证是原子的,全有或全无。 Java提供这种类型的变量作为volatile ,C ++作为std::atomic 。 Yes, they would be a good match, there is a good article on this at Dr Do ...
  • 我在构造函数中添加到下面的volatile列表中的项目可能对其他线程不可见是否正确 如果列表为非null,则对列表的所有写入都将可见。 存在一种竞争条件,其中默认的write(null)对于看到非null ProductPrice的线程可能是可见的。 如果该字段是最终的,则不是这样。 在这种情况下,我可以让变量final,并获得让所有项目对其他线程可见的相同效果。 是的,这是最好的解决方案。 Is it correct that the items that I added in my constructo ...
  • 有几个原因导致您无法观察到非易失volatile变量的缺失更新。 正如评论中其他人所指出的那样,你不能依靠失败发生。 在这个例子中,你的程序运行得太短,所以优化器不会在这里做任何努力。 使用-server选项运行程序将改变它。 此外,您正在执行System.err.println(…); 内部synchronized的循环内的语句。 因此,堆变量将在每次迭代中被重新读取,除非优化器决定放大synchronized代码块以覆盖整个循环(这是不太可能的,因为这意味着永远持有锁)。 因此,在堆值更改之后,第一个线 ...
  • 更新 因为你似乎想要解释“2 3 2”的具体情况 X递增i (现在为1) Y递增i (现在2) X读取i (X载入2) Y读取i (Y负载2) Y打印先前加载的值(打印2) Z增加i ,读取i ,打印i (打印3) X打印先前加载的值(打印2) 重点是: System.out.print(" " + count)不是原子的。 在执行易失性读取之后并在打印值之前,该线程可被抢占。 如果您想防止打印重复值,则必须在锁内执行易失性读取操作: public void increment() { int lo ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)