您的位置:首页 > 编程语言

微软编程一小时 题目1 : Arithmetic Expression

2014-04-05 20:11 387 查看
好久没写code了,bug好多%>_<%。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<string>
#include<cstring>
#include <cmath>
#include<algorithm>
using namespace std;

int N;
double a,b;
char c;
double mx=0x3f3f3f3f3f;
int ans=0;
double res=0;
int idx=0;
void cal()
{
switch(c)
{
case '+':
res=a+b;
break;
case '-':
res=a-b;
break;
case '*':
res=a*b;
break;
case '/':
res=a/b;
break;

}
}
int main()
{
// double s=3,d=3;
// double f=0;
// f=s*d;
// cout<<f;
//freopen("input.txt","r",stdin);
scanf("%d",&N);
// cout<<N<<endl;
for(int i=1;i<=N;i++)
{
//scanf("%d %c %d",&a,&c,&b);
cin>>a>>c>>b;
// cout<<a<<" "<<c<<" "<<b<<endl;
cal();
double tmp=abs(9-res);
// cout<<res<<" "<<mx<<" "<<tmp<<endl;
if(tmp<mx)
{
idx=i;
mx=tmp;
}
}
printf("%d\n",idx);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: