To find sum of two numbers without using any operator. Only use of printf() is allowed.
来源:互联网 发布:淘宝手机充值漏洞 编辑:程序博客网 时间:2024/04/30 06:08
We can use printf() to find sum of two numbers as printf() returns the number of characters printed.
The width field in printf() can be used to find the sum of two numbers. We can use ‘*’ which indicates the minimum width of output. For example, in the statement “printf(“%*d”, width, num);”, the specified ‘width’ is substituted in place of *, and ‘num’ is printed within the minimum width specified. If number of digits in ‘num’ is smaller than the specified ‘wodth’, the output is padded with blank spaces(右对齐). If number of digits are more, the output is printed as it is (not truncated).
solution:
int
add(
int
x,
int
y)
{
return
printf
(
"%*c%*c"
, x,
' '
, y,
' '
);
}
int
main()
{
printf
(
"Sum = %d"
, add(3, 4));
return
0;
}
Output:
Sum = 7
注意:Sum前面还会输出7个空格,因为add函数里面还会输出。如果想不输出前面的空格,可以用'\r'代替' ','\r'是使光标移到一行的开头。
int
add(
int
x,
int
y)
{
return
printf
(
"%*c%*c"
, x,
'\r'
, y,
'\r'
);
}
int
main()
{
printf
(
"Sum = %d"
, add(3, 4));
return
0;
}
Output:
Sum = 7
0 0
- To find sum of two numbers without using any operator. Only use of printf() is allowed.
- [zz]Determine the maximum of two integers using macro without any operators.
- SUMO使用:Vehicle '**' is not allowed to depart on any lane of its first edge.
- The reversed sum of two reversed numbers
- Unable to find a value for "tStatus" in object of class org.entity.Passport using operator "."
- add two numbers without using +
- How to find size of array in C/C++ without using sizeof ?
- json error: Use of overloaded operator [] is ambiguous
- [LeetCode]371. Sum of Two Integers【&2. Add Two Numbers】
- Two things to remind of using const
- Find the number of subsets such that the sum of numbers in the subset is a prime number
- Any Substitute Of Cactus? How To Use Cactus?
- Unable to find a value for "dwmc" in object of class "java.lang.String" using operator "." (null
- Unable to find a value for "字段名" in object of class java.lang.String using operator "."
- Invalid use of AddressOf operator
- Write one line C function to find whether a no is power of two
- Which Numbers are the Sum of Two Squares?
- POJ 2739 Sum of Consecutive Prime Numbers(Two pointers)
- 泛型算法_快速排序源码
- JFreeChart 学习示例
- mac系统升级到了10.10.但是eclipse中运行不识别真机了
- 把程序集安装到全局程序集缓存中的步骤
- [leetcode]String to Integer (atoi)
- To find sum of two numbers without using any operator. Only use of printf() is allowed.
- Android:给图片加边框
- poj 2698
- JFreeChart 项目实例
- Android:图形图像之使用Canvas,Paint绘图
- SQL到NOSQL的思维转变
- java 内存模型
- 硬盘数据恢复与编程
- 10、黑马程序员-OC语言@property @synthesize和id