zoukankan      html  css  js  c++  java
  • 一些笔试题(C/C++)

    1.there are two variables, don't use if.. else or ?: or switch or other judgement statements,find out the biggest number of the two numbers.

       返回2个数中较大的数,不使用if else "?:" 或switch 语句

        

     1 // return the biggest number without any judgement statements.
     2 // create by Young 
     3 // date 20130707
     4  #include <iostream>
     5  #include<math.h>
     6  using namespace std; 
     7  int max(int first_number, int second_number)
     8  {
     9         int max_number=(abs(first_number-second_number)+first_number+second_number)/2;  //use abs avoid use if else 
    10         }
    11 
    12  int main()
    13  {
    14     int result=max(13,6);
    15     cout<<"the max number is:"<<result<<endl;
    16      return 0;
    17  }

    2. swap two numbers without another variable, only use the two number。

          交换2个数,只能使用这两个数,不能使用第三个变量。

    int a=1,b=3;
    a=a+b;
    b=a-b;
    a=a-b;

    这样做有可能a+b比较大的时候溢出,overflow 不建议这样 可以使用:

    int a=1,b=2;
    a=a^b;
    b=a^b;
    a=a^b;

    ^ 按位取反

     3.字符串逆序

     1  #include <stdio.h>
     2  #include <string.h>
     3  #include <conio.h>
     4  int main()
     5  {
     6      char  str[]="abcdefg";
     7      char temp;
     8      int len=strlen(str);
     9      for(int i=0;i<len/2;i++)
    10      {
    11              temp=str[i] ;
    12              str[i]=str[len-1-i];
    13              str[len-1-i]=temp;
    14              }
    15              printf("%s
    ",str);
    16              getch();
    17      return 0;
    18 
    19 }

    把字符串第一位和最后一位交换,第二位和倒数第二位交换

  • 相关阅读:
    request内置对象(上)1
    康拓展开-----两个排列的位置之差
    判断一个数的质因子个数
    学生信息管理系统----(顺序表)
    学生信息管理系统----(链表)
    二分图的最大匹配--匈牙利算法
    hdu-1285拓扑排序
    文件的压缩与解压
    树莓派安装QT(全部库包括)
    Win10 + CLion + 树莓派 + QT 远程开发调用Python
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/3176287.html
Copyright © 2011-2022 走看看