编写程序,输入一个正三位数,输出各位数字的和。如,输入123,输出6。

要求:分别编写函数int sum(int n)和主函数来实现、

第1个回答  2011-10-26
Excel VBA的列子:
Public Function QiuHe(LookupRange As Range)
Dim InputValue, ReturnValue As Integer

InputValue = LookupRange.Value
ReturnValue = 0

If Len(LookupRange) <> 3 Or TypeName(InputValue) = "String" Then
MsgBox "not 3 digits integer, program stop!"
Exit Function
End If
ReturnValue = Application.Sum(Int(InputValue / 100), (Int(InputValue / 10) - Int(InputValue / 100) * 10), (InputValue - Int(InputValue / 10) * 10))

QiuHe = ReturnValue
End Function本回答被提问者采纳
第2个回答  2011-10-26
不知你要用什么语言编写?
其实在Excel里,不需编程:
A1放入 123,B1写公式:
=LEFT(A1,1)+MID(A1,2,1)+RIGHT(A1,1)
即可得到你要的结果: 6追问

C语言

相似回答