整数到以基数b表示的int n的字符串表示形式

时间:2016-04-20 05:06:56

标签: java string recursion

所以我创建了这个程序,它接受一个int并返回它的八进制数。 现在,如果我想创建一个递归函数,它返回以base b表示的整数n的字符串表示,我该怎么做。

static String integerToString(int n, int b) { //100, 8
   if(n > 0) {
      if(n/b > 0) {
       String s = integerToString(n%b, b) + integerToString(n/b, b)
   }
   else
     return integerToString(

这是迄今为止所拥有的。我离开了,我知道。

import sys

def print(*objects, sep=None, end=None, file=None, flush=False):
    """A Python translation of the C code for builtins.print()."""
    if sep is None:
        sep = ' '
    if end is None:
        end = '\n'
    if file is None:
        file = sys.stdout
    file.write(sep.join(map(str, objects)) + end)
    if flush:
        file.flush()

3 个答案:

答案 0 :(得分:0)

代码的逻辑不太有效。它应该是这样的:

  1. 如果 marriedRadioButton.setTag("married"); unmarriedRadioButton.setTag("unmarried") RadioGroup rdGroup = (RadioGroup) findViewById(R.id.rdbGp1); rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton selectedRadioButton = (RadioButton) group.findViewById(checkedId); String text = radioButton.getTag(); JSONObject jsonObject = new JSONObject(); jsonObject.put("maritalstatus", text); // Now you need to send the json to the server through an AsyncTask SendJsonTask sendJsonTask = new SendJsonTask(); sendJsonTask.execute(jsonObject.toString()); }}); private class SendJsonTask extends AsyncTask<String, Void, Void> { protected String doInBackground(String... params) { URL url = new URL("http://domaintoreceive.com/pagetoreceive"); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url.toURI()); httpPost.setEntity(new StringEntity(params[0], "UTF-8")); // Set up the header types needed to properly transfer JSON httpPost.setHeader("Content-Type", "application/json"); httpPost.setHeader("Accept-Encoding", "application/json"); httpPost.setHeader("Accept-Language", "en-US"); // Execute POST response = httpClient.execute(httpPost); }} ,则返回n < b
  2. 其他返回b + integertoString(n/b, b)
  3. 此外,您还必须添加一些内容来表示超过b的数字(即A,B,C ......)。例如,您可以拥有一个有效数字数组或方法。

    此代码可以使用:

    9

    例如,static String[] digits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; static String integerToString(int n, int b) { if(n < b) return digits[n]; else return integerToString(n/b, b) + digits[n%b]; } ,输出integerToString(255, 16)
    请注意,您应该使用FF来提高效率。

答案 1 :(得分:0)

如果你想使用递归,你必须

  • 计算你的停止条件,即你可以处理的最低限度。
  • 工作如何完成一项工作可以用更少的工作来称呼自己。

--create table t(id int, text nvarchar(max));
--insert into t values
--(1,'applicationName="App1" some text here applicationName="App2" text again applicationName="App2"'),
--(2,'some text here applicationName="App3" some text here'),
--(3,'some text here applicationName="App3"')

create table #temp (rowid int,id int, text nvarchar(max));
create table #result (id int, data nvarchar(255));
insert into #temp 
    select  row_number() over( order by Id desc) rowid, id,text from t;

declare @c int, @d int, @startpos int, @endpos int
declare @textpart nvarchar(255), @datapart nvarchar(255)
declare @len int
set @len =LEN('applicationName="')
select @c=count(1) from #temp

WHILE @c>0
BEGIN
    select @textpart=text from #temp where id=@c
    select @d= LEN(@textpart)-LEN(REPLACE(@textpart,'applicationName="','applicationName='))  
    set @startpos=0
    set @endpos=0

    WHILE @d>0
    BEGIN
        set @startpos=CHARINDEX('applicationName="',@textpart,@startpos)+@len
        set @endpos= CHARINDEX('"',@textpart,@startpos+1)
        insert into #result
            select @c,SUBSTRING(@textpart,@startpos,@endpos-@startpos) 
        set @startpos=@endpos
        set @d=@d-1
    END
    SET @c=@c-1
END
select * from #result order by id asc
drop table #temp,#result

这适用于2至36号基础。

答案 2 :(得分:0)

public static String convert(int number, int base)
{
    int quotient = number / base;
    int remainder = number % base;

    if(quotient == 0) // base case
    {
        return Integer.toString(remainder);      
    }
    else
    {
        return convert(quotient, base) + Integer.toString(remainder);
    }            
}