微软十五道面试题(待续)

闲的实在是无聊  找不到成就感  故此做些面试题   希望大家 多多指教  纠正错误

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值