编写一个程序,不使用任何运算符求正整数和。只允许使用printf()。不能使用其他库函数。
null
解决方案 这是个骗人的问题。我们可以使用printf()查找两个数字的和,因为printf()返回打印的字符数。这个 printf()中的宽度字段 可以用来求两个数的和。我们可以使用“*”表示输出的最小宽度。例如,在语句“printf(“%*d”,width,num);”中,指定的“宽度”替换为*,并且“num”在指定的最小宽度内打印。如果“num”中的位数小于指定的“width”,则输出将填充空格。如果位数更多,输出按原样打印(不截断)。在下面的程序中,add()返回x和y之和。它在使用x和y指定的宽度内打印2个空格。所以打印的字符总数等于x和y之和。这就是为什么add()返回x+y。
C++
#include <iostream> using namespace std; int add( int x, int y) { return printf ( "%*c%*c" , x, ' ' , y, ' ' ); } // Driver code int main() { printf ( "Sum = %d" , add(3, 4)); return 0; } // This code is contributed by shubhamsingh10 |
C
#include <stdio.h> int add( int x, int y) { return printf ( "%*c%*c" , x, ' ' , y, ' ' ); } // Driver code int main() { printf ( "Sum = %d" , add(3, 4)); return 0; } |
输出:
Sum = 7
时间复杂性: O(1)
辅助空间: O(1)
输出为七个空格,后跟“Sum=7”。我们可以使用回车来避免前导空格。幸亏 克拉齐科德 和 桑迪普 谢谢你的建议。以下程序打印输出时不带任何前导空格。
C++
#include <iostream> using namespace std; int add( int x, int y) { return printf ( "%*c%*c" , x, ' , y, ' ); } // Driver code int main() { printf ( "Sum = %d" , add(3, 4)); return 0; } // This code is contributed by shubhamsingh10 |
C
#include <stdio.h> int add( int x, int y) { return printf ( "%*c%*c" , x, ' , y, ' ); } // Driver code int main() { printf ( "Sum = %d" , add(3, 4)); return 0; } |
JAVA
class GFG { static int add( int x, int y) { return (x + y); } // Driver code public static void main(String[] args) { System.out.printf( "Sum = %d" , add( 3 , 4 )); } } // This code is contributed by Rajput-Ji |
C#
// C# program for the above approach using System; public class GFG { static int add( int x, int y) { return (x + y); } // Driver Code public static void Main(String[] args) { Console.WriteLine( "Sum = " + add(3, 4)); } } // This code is contributed by code_hunt. |
输出:
Sum = 7
时间复杂性: O(1)
辅助空间: O(1)
另一种方法:
C++
#include <iostream> 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 <stdio.h> 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 |
Python3
# 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
<?php // PHP Code $a = 10; $b = 5; if ( $b > 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
<script> // Javascript program for the above approach // Driver Code let a = 10, b = 5; if (b > 0) { while (b > 0) { a++; b--; } } if (b < 0) { // when 'b' is negative while (b < 0) { a--; b++; } } document.write( "Sum = " + a); </script> |
输出:
sum = 15
时间复杂性: O(b)
辅助空间: O(1)
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END