编写一个程序来查找正整数之和,而无需使用任何运算符。只允许使用printf()。不能使用其他库函数。
解决方案
这是一个技巧问题。我们可以使用printf()查找两个数字的和,因为printf()返回打印的字符数。 printf()中的width字段可用于查找两个数字的和。我们可以使用“ *”来表示输出的最小宽度。例如,在语句“ printf(“%* d”,width,num);”中,指定的“ width”代替*,并且在指定的最小宽度内打印“ num”。如果’num’中的位数小于指定的’width’,则在输出中填充空格。如果位数更多,则按原样打印输出(不截断)。在下面的程序中,add()返回x和y的和。它在使用x和y指定的宽度内打印2个空格。因此,打印的总字符等于x和y的总和。这就是为什么add()返回x + y的原因。
C
#include
int add(int x, int y)
{
return printf("%*c%*c", x, ' ', y, ' ');
}
// Driver code
int main()
{
printf("Sum = %d", add(3, 4));
return 0;
}
C
#include
int add(int x, int y)
{
return printf("%*c%*c", x, '\r', y, '\r');
}
// Driver code
int main()
{
printf("Sum = %d", add(3, 4));
return 0;
}
C++
#include
using namespace std;
int main()
{
int a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) { // when 'b' is negative
while (b < 0) {
a--;
b++;
}
}
cout << "Sum = " << a;
return 0;
}
// This code is contributed by SHUBHAMSINGH10
// This code is improved & fixed by Abhijeet Soni.
C
#include
int main()
{
int a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) { // when 'b' is negative
while (b < 0) {
a--;
b++;
}
}
printf("Sum = %d", a);
return 0;
}
// This code is contributed by Abhijeet Soni
Java
// Java code
class GfG {
public static void main(String[] args)
{
int a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) { // when 'b' is negative
while (b < 0) {
a--;
b++;
}
}
System.out.println("Sum is: " + a);
}
}
// This code is contributed by Abhijeet Soni
Python 3
# Python 3 Code
if __name__ == '__main__':
a = 10
b = 5
if b > 0:
while b > 0:
a = a + 1
b = b - 1
if b < 0:
while b < 0:
a = a - 1
b = b + 1
print("Sum is: ", a)
# This code is contributed by Akanksha Rai
# This code is improved & fixed by Abhijeet Soni
C#
// C# code
using System;
class GFG {
static public void Main()
{
int a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) { // when 'b' is negative
while (b < 0) {
a--;
b++;
}
}
Console.Write("Sum is: " + a);
}
}
// This code is contributed by Tushil
// This code is improved & fixed by Abhijeet Soni.
PHP
0) {
while($b > 0)
{
$a++;
$b--;
}
}
if ($b < 0) {
while($b < 0)
{
$a--;
$b++;
}
}
echo "Sum is: ", $a;
// This code is contributed by Dinesh
// This code is improved & fixed by Abhijeet Soni.
?>
Javascript
输出:
Sum = 7
输出为七个空格,后跟“ Sum = 7”。我们可以通过使用回车符来避免前导空格。感谢krazyCoder和Sandeep提出的建议。以下程序打印输出,不带任何前导空格。
C
#include
int add(int x, int y)
{
return printf("%*c%*c", x, '\r', y, '\r');
}
// Driver code
int main()
{
printf("Sum = %d", add(3, 4));
return 0;
}
输出:
Sum = 7
另一种方法:
C++
#include
using namespace std;
int main()
{
int a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) { // when 'b' is negative
while (b < 0) {
a--;
b++;
}
}
cout << "Sum = " << a;
return 0;
}
// This code is contributed by SHUBHAMSINGH10
// This code is improved & fixed by Abhijeet Soni.
C
#include
int main()
{
int a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) { // when 'b' is negative
while (b < 0) {
a--;
b++;
}
}
printf("Sum = %d", a);
return 0;
}
// This code is contributed by Abhijeet Soni
Java
// Java code
class GfG {
public static void main(String[] args)
{
int a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) { // when 'b' is negative
while (b < 0) {
a--;
b++;
}
}
System.out.println("Sum is: " + a);
}
}
// This code is contributed by Abhijeet Soni
的Python 3
# Python 3 Code
if __name__ == '__main__':
a = 10
b = 5
if b > 0:
while b > 0:
a = a + 1
b = b - 1
if b < 0:
while b < 0:
a = a - 1
b = b + 1
print("Sum is: ", a)
# This code is contributed by Akanksha Rai
# This code is improved & fixed by Abhijeet Soni
C#
// C# code
using System;
class GFG {
static public void Main()
{
int a = 10, b = 5;
if (b > 0) {
while (b > 0) {
a++;
b--;
}
}
if (b < 0) { // when 'b' is negative
while (b < 0) {
a--;
b++;
}
}
Console.Write("Sum is: " + a);
}
}
// This code is contributed by Tushil
// This code is improved & fixed by Abhijeet Soni.
的PHP
0) {
while($b > 0)
{
$a++;
$b--;
}
}
if ($b < 0) {
while($b < 0)
{
$a--;
$b++;
}
}
echo "Sum is: ", $a;
// This code is contributed by Dinesh
// This code is improved & fixed by Abhijeet Soni.
?>
Java脚本
输出:
sum = 15
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。