C/C++棘手程序集锦和详细介绍

2021年3月21日16:51:39 发表评论 578 次浏览

在我们的日常生活中, 我们可能会遇到各种棘手的程序。可能在技术面试, 编码测试中或在C / C ++教室中。

这是此类程序的列表:

用双引号("")打印文本。

这似乎很容易, 但是初学者在打印双引号内的文本时可能会感到困惑。

// CPP program to print double quotes
#include<iostream>
  
int main()
{
    std::cout << "\"lsbin\"" ;
    return 0;
}

输出如下:

"lsbin"

在不使用算术运算符或比较运算符的情况下检查两个数字是否相等。

最简单的解决方案是使用按位XOR运算符(^)。我们知道, 对于两个相等的数字, XOR运算符将返回0。我们将使用此技巧来解决此问题。

// 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函数的想法。

// CPP 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++ 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 << "\n" ;
    cout << "Y : " << y << "\n" ; 
     
    return 0;
}

输出如下:

X : 70
Y : 10

程序无需使用任何循环或条件即可查找两个数字的最大值和最小值。

最简单的技巧是-

// 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\n" , ((a + b) + abs (a - b)) / 2);
    printf ( "min = %d" , ((a + b) - abs (a - b)) / 2);
    return 0;
}

输出如下:

max = 20
min = 15

使用C语言中的"恭维(〜)"运算符打印无符号整数的最大值。

这是一个使用恭维运算符查找无符号int最大值的技巧:

// 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)。因此, 这将对我们有用。

// 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块中的条件。

// 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;
}

输出如下:

lsbin

程序不使用" /"运算符将整数除以4。

将整数除以4的最有效方法之一是使用右移运算符(" >>")。

// 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 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;
}

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: