我们在日常生活中可能会遇到各种棘手的程序。可能是在技术面试、编码测试或C/C++课堂上。
null
以下是此类项目的列表:-
- 打印双引号(“”)内的文本。 这似乎很容易,但初学者在打印双引号内的文本时可能会感到困惑。
C++
// CPP program to print double quotes #include<iostream> int main() { std::cout << ""geeksforgeeks"" ; return 0; } |
输出:
"geeksforgeeks"
- 在不使用算术运算符或比较运算符的情况下检查两个数字是否相等。 最简单的解决方案是使用按位异或运算符(^)。我们知道,对于两个相等的数,XOR运算符返回0。我们将使用这个技巧来解决这个问题。
C++
// C++ program to check if two numbers are equal // without using arithmetic operators or // comparison operators #include <iostream> using namespace std; int main() { int x = 10; int y = 10; if (!(x ^ y)) cout << " x is equal to y " ; else cout << " x is not equal to y " ; return 0; } // This code is contributed by shivani |
C
// C program to check if two numbers are equal // without using arithmetic operators or // comparison operators #include<stdio.h> int main() { int x = 10; int y = 10; if ( !(x ^ y) ) printf ( " x is equal to y " ); else printf ( " x is not equal to y " ); return 0; } |
输出:
x is equal to y
- 不使用分号打印N以内的所有自然数。 我们使用递归调用main函数的思想。
C++
// C++ program to print all natural numbers upto // N without using semi-colon #include<iostream> using namespace std; int N = 10; int main() { static int x = 1; if (cout << x << " " && x++ < N && main()) { } return 0; } |
输出 :
1 2 3 4 5 6 7 8 9 10
- 在不使用任何额外变量的情况下交换两个变量的值。
C++
// C++ program to check if two numbers are equal #include<iostream> int main() { int x = 10; int y = 70; x = x + y; y = x - y; x = x - y; cout << "X : " << x << "" ; cout << "Y : " << y << "" ; return 0; } |
输出:
X : 70Y : 10
- 在不使用任何循环或条件的情况下,找到两个数字的最大值和最小值的程序。 最简单的诀窍是-
C++
// CPP program to find maximum and minimum of // two numbers without using loop and any // condition. #include<bits/stdc++.h> int main () { int a = 15, b = 20; printf ( "max = %d" , ((a + b) + abs (a - b)) / 2); printf ( "min = %d" , ((a + b) - abs (a - b)) / 2); return 0; } |
输出:
max = 20min = 15
- 使用C中的补码(~)运算符打印无符号整数的最大值。 下面是一个技巧,可以使用补码运算符找到无符号int的最大值:
C
// C program to print maximum value of // unsigned int. #include<stdio.h> int main() { unsigned int max; max = 0; max = ~max; printf ( "Max value : %u " , max); return 0; } |
- 不使用“+”运算符求两个整数之和。 这是一个非常简单的数学技巧。 我们知道a+b=–(-a-b)。所以这对我们来说是个骗局。
C++
// CPP program to print sum of two integers // withtout + #include<iostream> using namespace std; int main() { int a = 5; int b = 5; int sum = -( -a-b ); cout << sum; return 0; } |
输出:
10
- 用于验证if块内部条件的程序。
C++
// CPP program to verifies the condition inside if block // It just verifies the condition inside if block, // i.e., cout << "geeks" which returns a non-zero value, // !(non-zero value) is false, hence it executes else // Hence technically it only executes else block #include<iostream> using namespace std; int main() { if (!(cout << "geeks" )) cout << " geeks " ; else cout << "forgeeks " ; return 0; } |
输出:
geeksforgeeks
- 不使用“/”运算符将整数除以4的程序。 将整数除以4最有效的方法之一是使用右移运算符(“>>”)。
C++
// CPP program to divide a number by 4 // without using '/' #include<iostream> using namespace std; int main() { int n = 4; n = n >> 2; cout << n; return 0; } |
输出:
1
- 检查计算机端部的程序。
C
// C program to find if machine is little // endian or big endian. #include <stdio.h> int main() { unsigned int n = 1; char *c = ( char *)&n; if (*c) printf ( "LITTLE ENDIAN" ); else printf ( "BIG ENDIAN" ); return 0; } |
本文由 史密斯·迪内斯·塞姆瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END