微軟十五道面試題(待續)

來源:互聯網
上載者:User

閑的實在是無聊  找不到成就感  故此做些面試題   希望大家 多多指教  糾正錯誤

1、有一個整數數組,請求出兩兩之差絕對值最小的值,
記住,只要得出最小值即可,不需要求出是哪兩個數。

/*  Name: hondely  Copyright: hondely      Author: hondely  Date: 27/03/12 20:07  Description: */#include <iostream>using namespace std;#include <math.h>#include <vector>int main(){    int len,min=2147483647;    while (cin>>len&&len)    {          min=2147483647;          vector<int> vec(len);          int i,a;          for (i=0; i<len; ++i)              cin>>a,vec.push_back(a);          for (i=1; i<len; ++i)          {              if (abs(vec[i]-vec[i-1])<min)                  min=abs(vec[i]-vec[i-1]);          }          cout<<min<<endl;    }    return 0;}

 

2、寫一個函數,檢查字元是否是整數,如果是,返回其整數值。
(或者:怎樣只用4行代碼編寫出一個從字串到長整形的函數?)

不說了 這道題目  很簡單 代碼就不貼了下面是判斷這個字串是否為整數的代碼 比較簡單

 

/*  Name: hondely  Copyright: hondely  Author: hondely  Date: 27/03/12 20:56  Description: */#include <iostream>using namespace std;#include <string.h>int Check(char *ch){    int cnt=0;    for (int i=0; i<strlen(ch); ++i)     if (ch[i]>=48&&ch[i]<=57)         cnt++;     return cnt;}int main(){    char *ch;    while (cin>>ch)    {          if (Check(ch)==strlen(ch))              cout<<ch<<endl;          else               cout<<"輸入的非數字!!"<<endl;    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.