闲的实在是无聊 找不到成就感 故此做些面试题 希望大家 多多指教 纠正错误
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;
}