プログラマーの必須面接問題


1、
  main     ,         ,          ,     “-a”      ,“-s”      ,“-m”      ,“-d”      ,          。   :  test.exe -a 1 2   1+2  3
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv [])
{
   int num1 = atoi( argv[2]);
   int num2 = atoi( argv[3]);
   if (strcmp( "-a", argv [1]) == 0)
   {
      printf( "%d
", num1 + num2);    }    else if (strcmp("-s" , argv[1]) == 0)    {       printf( "%d
", num1 - num2);    }    else if (strcmp("-m" , argv[1]) == 0)    {       printf( "%d
", num1 * num2);    }    else if(strcmp("-d" , argv[1]) == 0)    {       printf( "%d
", num1 / num2);    }    system( "pause");    return 0; }

2、関数を作成して現在の機械の大端と小端を判断する.
#include
int check_system()
{
   int a = 1;
   char*p = ( char*)&a;
   if (*p == 1)
       return 0;
   else
       return 1;
}
int main()
{
   if (check_system())
printf(「大端」);
   else
printf(「小端」);
   system( "pause");
   return 0;
}
 
3、ある文字列が別の文字列の回転後の文字列であるかどうかを判断する.例えば、s 1=AABCDおよびs 2=BCDAAが、1を返し、s 1=abcdおよびs 2=ACBDが、0を返す.AABCD左回り1文字ABCDA AABCD左回り2文字BCDAA AABCD右回り1文字DAABC AABCD右回り2文字CDAAB
#include
#include
#include
#include
int is_move_str(char arr[], char *p )
{
           int n = strlen(arr );
           int m = strlen(p );
           if(m != n)
          {
                    return 0;
          }
          strncat( arr, arr , n);
           if(strstr(arr , p) == NULL)
          {
                    return 0;
          }
           else
                    return 1;
}
int main()
{
           char arr[20] = "abcdef";
           char *p = "efabcd";
           int ret = is_move_str(arr, p);
           if(ret == 1)
          {
                   printf( "ok");
          }
           else if (ret == 0)
          {
                   printf( "no");
          }
          system( "pause");
           return 0;
}


     
本文は“1091544”のブログから出て、転載して作者と連絡してください!