0

## 請以C# 語法設計一個公用GetNumString方法，依傳入參數回傳數字字串

Joe 2018-12-14 10:27:052176 瀏覽

GetNumString(6) 回傳 “0123456”
GetNumString(15) 回傳 “0123456789012345”
GetNumString(23) 回傳 “012345678901234567890123”

Joe iT邦新手 4 級 ‧ 2018-12-14 10:51:56 檢舉
class Program
{
static void Main(string[] args)
{

}

public static string GetNumString(int num)
{
int count = 0;
int newNum = 0;
int rNewNum = num;
string strNum = "";

if (num == 0)
{
return "0";
}
else
{
if (num % 10 == 0)
{
rNewNum = 0;
}

if (num > 10 && num < 20)
{
rNewNum -= 10;
}

if (num > 20)
{
rNewNum -= 20;
}

return GetNumString(num - 1) + rNewNum;
}
}
}

...

### 8 個回答

7
㊣浩瀚星空㊣
iT邦超人 1 級 ‧ 2018-12-14 13:45:44

``````
<?php
echo GetNumString(10);//01234567890

function GetNumString(\$n){
\$str = '';
for(\$i=0;\$i<=\$n;\$i++){
\$str .= \$i%10;
}
return \$str;
}
?>

``````

Joe iT邦新手 4 級 ‧ 2018-12-14 15:28:14 檢舉

+1 是想讓下面的 for 多繞一位

froce iT邦大師 1 級 ‧ 2018-12-14 17:32:59 檢舉

Joe iT邦新手 4 級 ‧ 2018-12-17 11:03:41 檢舉

``````static void Main(string[] args)
{
string inputNum = "";
Regex NumberPattern = new Regex("^[0-9]*\$");

Console.WriteLine("輸入值請輸入數字，輸入非數字則自動關閉。。。");
Console.WriteLine();

do
{
Console.Write("輸入值：");

if (NumberPattern.IsMatch(inputNum))
{
GetNumString(Convert.ToInt32(inputNum));
Console.WriteLine(GetNumString(Convert.ToInt32(inputNum)));
Console.WriteLine();
}
} while (NumberPattern.IsMatch(inputNum));
}

private static string GetNumString(int num)
{
string outputVal = "";

for (int i = 0; i <= num; i++)
{
outputVal += i % 10;
}
return outputVal;
}
``````
1
IT@lantis
iT邦新手 5 級 ‧ 2018-12-14 10:50:57
``````static string[] arr = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
void Main()
{
GetNumString(6).Dump(); // “0123456”
GetNumString(15).Dump();
GetNumString(23).Dump();
}

string GetNumString(int num)
{
var Result = string.Empty;
var counter = num;
var index = 0;
while (counter > 0)
{
if (index == 0)
{
Result += arr[index];
index++;
}
else
{
if (index >= arr.Length)
index = 0;
Result += arr[index++];
counter--;
}
}
return Result;
}
``````

Joe iT邦新手 4 級 ‧ 2018-12-14 10:53:42 檢舉

IT@lantis iT邦新手 5 級 ‧ 2018-12-14 10:56:04 檢舉

IT@lantis iT邦新手 5 級 ‧ 2018-12-14 11:00:44 檢舉
``````string GetNumString(int num)
{
var Result = string.Empty;
var counter = num;

int printNum = 0;
while (counter > 0)
{
if (printNum == 0)
{
Result += printNum;
printNum++;
}
else
{
if (printNum > 9)
printNum = 0;
Result += printNum++;
counter--;
}
}
return Result;
}
``````
Joe iT邦新手 4 級 ‧ 2018-12-14 11:08:12 檢舉

``````public static string GetNumString(int num)
{
int count = 0;
int newNum = 0;
int rNewNum = num;
string strNum = "";

for (; count <= num; count++)
{
strNum += newNum.ToString();

newNum += 1;

if (newNum > 9)
{
newNum = 0;
}
}

while (count <= num)
{
strNum += newNum.ToString();
newNum += 1;
count++;

if (newNum > 9)
{
newNum = 0;
}
}

return strNum;
}
``````

3
``````public static string GetNumString(int num)
{
var result = new StringBuilder();

//商數
var quotient = num / 10;

for (var i = 0; i < quotient; i++)
{
result.Append("0123456789");
}

//餘數，加1是因為循環第一位是0
//更新說明:
//+1 是為了餘0還是要進迴圈取出0
var remainder = num % 10 + 1;

for (var i = 0; i < remainder; i++)
{
result.Append(i.ToString());
}

return result.ToString();
}
``````

``````public static string GetNumString(int num)
{
var template = "0123456789";

var result = new StringBuilder();
for (var i = 0; i < num / template.Length; i++)
{
result.Append(template);
}
result.Append(template.Substring(0, num % template.Length + 1));

return result.ToString();
}
``````

Joe iT邦新手 4 級 ‧ 2018-12-14 13:05:59 檢舉

``````using System;
public class Program
{
public static void Main()
{
Console.WriteLine(GetNumString(6));
Console.WriteLine(GetNumString(15));
Console.WriteLine(GetNumString(23));
}
static string GetNumString(int num) {
var s = "012345678901234567890123";
return s.Substring(0, num+1);
}
}
``````

4

iT邦大神 1 級 ‧ 2018-12-14 12:08:19

``````using System;
public class Program
{
public static void Main()
{
Console.WriteLine(GetNumString(6));
Console.WriteLine(GetNumString(15));
Console.WriteLine(GetNumString(23));
}
static string GetNumString(int num) {
var s = "0";
for (int i = 0; i <= num/10; i++)
{
s += "1234567890";
}
return s.Substring(0, num+1);
}
}
``````
``````0123456
0123456789012345
012345678901234567890123
``````

froce iT邦大師 1 級 ‧ 2018-12-14 13:00:26 檢舉

``````def GetNumString(num):
return "".join(str(i)[-1] for i in range(num+1))
``````
Joe iT邦新手 4 級 ‧ 2018-12-14 13:12:31 檢舉

froce iT邦大師 1 級 ‧ 2018-12-14 13:14:08 檢舉

``````	static string GetNumString(int num) {
var s = "";
for (int i = 0; i <= num; i++)
{
var tmp = i.ToString();
s += tmp[tmp.Length - 1];
}
return s;
}
``````

``````const GetNumString = num => Array.call(0, num + 1).fill(0).map((v, i) => i % 10).join('')
``````
2

iT邦大師 1 級 ‧ 2018-12-14 13:16:36

``````static string GetNumString(int num, int total)
{
if (num <= 0)
return "";
else
return ((total - num) % 10).ToString() + GetNumString(num - 1, total);
}
``````

``````int num = 23;
string text = GetNumString(num+1, num+1);
Console.WriteLine(text);
``````

2

iT邦大師 1 級 ‧ 2018-12-14 14:20:26

``````static string GetNumString(int num)=> string.Join("",Enumerable.Range(0,num+1).Select(s=>s%10));
``````

Homura iT邦高手 1 級 ‧ 2018-12-14 14:26:38 檢舉

IT邦幫你跳行了

((筆記

Enumerable.Range跟Enumerable.Repeat很好用的方法

Joe iT邦新手 4 級 ‧ 2018-12-14 15:33:44 檢舉

froce iT邦大師 1 級 ‧ 2018-12-14 16:19:26 檢舉

froce python還是比較少代碼

Luke iT邦新手 3 級 ‧ 2018-12-14 16:25:47 檢舉

1

iT邦新手 2 級 ‧ 2018-12-14 19:50:06
``````create or replace function ithelp181214(n integer)
returns text
as \$\$
with t1 as (
select (x % 10)::text as ch
from generate_series(0, n) as g(x)
)
select string_agg(ch, '')
from t1
;
\$\$
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;

// -- test

with t1 as
(
select x
from generate_series(-2, 11) as g(x)
union all
select unnest(ARRAY[15, 23])
)
select x
, ithelp181214(x)
from t1;

x  |       ithelp181214
----+--------------------------
-2 | [null]
-1 | [null]
0 | 0
1 | 01
2 | 012
3 | 0123
4 | 01234
5 | 012345
6 | 0123456
7 | 01234567
8 | 012345678
9 | 0123456789
10 | 01234567890
11 | 012345678901
15 | 0123456789012345
23 | 012345678901234567890123

``````
0
evali9876
iT邦新手 5 級 ‧ 2018-12-17 09:30:10

``````    protected string GetNumString(int Num)
{
string result = "";
/*example:
GetNumString(6) =0123456
GetNumString(15) =0123456789012345
GetNumString(23) =012345678901234567890123
*/
int i = Num / 10;
if (i>0)
{
for (int k = 0; k < i; k++)
{
result += "0123456789";
if (k == i - 1)
{
for (int j = 0; j <=( Num-(i*10)); j++)
{
result += j;
}
break;
}
}
}
else
{
for (int j = 0; j <=Num; j++)
{
result += j;
}
}
return result;
}
``````
Joe iT邦新手 4 級 ‧ 2018-12-17 09:32:38 檢舉