我正在構建的程式要求我輸入一個數字,例如 1234546。該程式需要添加尚未添加的最左邊和最右邊的數字,并將其和放在結果的末尾。
static void Main(string[] args)
{
int num, sum = 0, firstDigit, lastDigit;
// Reading number
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
lastDigit = num % 10;
firstDigit = num;
while (num >= 10)
{
num = num / 10;
}
firstDigit = num;
//Finding sum of first and last digit
sum = firstDigit lastDigit;
Console.WriteLine("Sum of first and last digit: " sum);
Console.ReadLine();
}
這對我到目前為止有什么幫助嗎?
uj5u.com熱心網友回復:
將您的數字保存在字串中可能會更容易。它看起來像家庭作業,所以我不會解決整個問題,但是:
c# 中的字符可以被視為數字。
int r = '9' - '0'; //r is 9
從數字 char 中減去 char '0' 會給出您期望的結果的 int 值。因此,數字字串 str 中第一個和最后一個字符的數學總和可以由下式給出:
int sum = (str.First() - '0') str.Last() - '0');
對于此作業,如果字串長于 1 個字符,則應執行此操作
如果您使用最新版本的 .net (8 ),您還可以獲得第一個字符str[0]和最后一個字符,str[^1]而不是 LINQ 的 First 和 Last
在較舊的 c# 中,最后一個有點羅嗦:str[str.Length-1]
子字串可用于切掉第一個和最后一個字符
str = str.Substring(1, str.Length-2);
在 c# 8 中也可以使用索引和范圍,這很容易推理:
str = str[1..^1];
當您將 與一個為字串的運算元和另一個為整數的運算元一起使用時,整數將附加到字串的末尾,而不是以數字形式添加到它
string str = "123";
int sum = str[0] str[^1] - '0' - '0';
string s = "999" sum; //s is 9994
因此,一些重復使用
- “第一個加最后一個字符給出一個數字總和結果”,
- "將總和結果連接到一個字串",
- “將第一個和最后一個字符切掉并重復上述操作,直到你找到任何一個
- 剩余 1 個字符 - 只需連接它或
- 剩余 0 個字符 - 使您構建的新字串成為您首先/最后/切碎的字串并再次重復該程序(直到構建的字串長度為 2 個字符)
應該讓你走向你想要的。我建議你先在評論中寫出演算法,然后翻譯成 c#。在我看來,它是這樣作業的:
//while the result is longer than two chars
//make a temporary empty string
//while true
//take the first and last chars, sum them, append to the temp
//chop the result
//if the result is one char append to the temp and break the loop
//if the result is 0 chars break the loop
//make the result equal to the temp
uj5u.com熱心網友回復:
我建議你使用字串,并測驗數字是否有奇數或偶數:
var number = "5659679";
// test if number has number of digits > 2 or no
var result = number.Length > 2 ? "" : number;
while (number.Length > 2)
{
var l = number.Length;
result = "";
for (var i = 0; i < l / 2; i )
{
//add the both start left and end right digits
result = (number[i] number[l - 1 - i] - 2 * '0').ToString();
}
//check if number of digit is odd or event
if (l % 2 != 0)
result = number[l / 2];
number = result;
Console.WriteLine(number);
}
Console.WriteLine(result);
這行代碼將 char 添加為 int :0 = '0' - '0', 1 = '1' - '0' 等等......
result = (number[i] number[l - 1 - i] - 2 * '0').ToString()
在您使用的 c# 版本之后,您可以撰寫而不是:
number[l - 1 - i] ->number[^(i 1)]
測驗結果小提琴
uj5u.com熱心網友回復:
你的意思是基本上是這樣的?(這是 awk,但您可以輕松地將其調整為您選擇的任何語言:
echo 12345678909999333333333336543234569999998765432101010101 \
\
| mawk '{ __=length(_=$(_=___=""))
_______=(______=(\
____ = ____) __^!__)^____ !!____;
print _="__"_;
while(-__< __) {
print \
_=substr((_=substr(_,______))_,__,__--),\
___=(___)"|"((_____= \
substr(_,__^!__,____))%_______ \
int(_____/_______))
__-- } }'
__12345678909999333333333336543234569999998765432101010101
11234567890999933333333333654323456999999876543210101010 |2
023456789099993333333333365432345699999987654321010101 |2|2
1345678909999333333333336543234569999998765432101010 |2|2|4
04567890999933333333333654323456999999876543210101 |2|2|4|4
156789099993333333333365432345699999987654321010 |2|2|4|4|6
0678909999333333333336543234569999998765432101 |2|2|4|4|6|6
17890999933333333333654323456999999876543210 |2|2|4|4|6|6|8
089099993333333333365432345699999987654321 |2|2|4|4|6|6|8|8
1909999333333333336543234569999998765432 |2|2|4|4|6|6|8|8|10
20999933333333333654323456999999876543 |2|2|4|4|6|6|8|8|10|2
399993333333333365432345699999987654 |2|2|4|4|6|6|8|8|10|2|12
4999333333333336543234569999998765 |2|2|4|4|6|6|8|8|10|2|12|13
59933333333333654323456999999876 |2|2|4|4|6|6|8|8|10|2|12|13|14
693333333333365432345699999987 |2|2|4|4|6|6|8|8|10|2|12|13|14|15
7333333333336543234569999998 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10
83333333333654323456999999 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11
933333333365432345699999 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12
9333333336543234569999 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12
93333333654323456999 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12
933333365432345699 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12
9333336543234569 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12
93333654323456 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12
633365432345 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9
5336543234 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9|8
43654323 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9|8|7
365432 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9|8|7|9
2543 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9|8|7|9|7
34 |2|2|4|4|6|6|8|8|10|2|12|13|14|15|10|11|12|12|12|12|12|12|9|8|7|9|7|7
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442258.html
標籤:C#
下一篇:批量重命名檔案C#
