題目連結 http://zerojudge.tw/ShowProblem?problemid=a244。
此題要用 long long int 型態,其他依照題目要求即可。此外筆者在程式碼二用了 Array of Function Pointers 的解法。
程式碼一:
#include <cstdio>
int main() {
int n;
while(scanf("%d", &n) != EOF)
{
int a;
long long int b, c;
for(int i = 0; i < n; i++)
{
scanf("%d %lld %lld", &a, &b, &c);
switch(a) {
case 1:
printf("%lld", b+c);
break;
case 2:
printf("%lld", b-c);
break;
case 3:
printf("%lld", b*c);
break;
case 4:
printf("%lld", b/c);
break;
default:
break;
}
printf("\n");
}
}
return 0;
}
程式碼二:
#include <iostream>
using namespace std;
long long int add(long long int b, long long int c)
{
return b + c;
}
long long int mins(long long int b, long long int c)
{
return b - c;
}
long long int mul(long long int b, long long int c)
{
return b * c;
}
long long int divi(long long int b, long long int c)
{
return b / c;
}
int main() {
int n;
long long int(*cal[5])(long long int, long long int) = {NULL, add, mins, mul, divi};
while(cin >> n)
{
int a;
long long int b, c;
for(int i = 0; i < n; i++)
{
cin >> a >> b >> c;
cout << cal[a](b, c) << endl;
}
}
return 0;
}
沒有留言:
張貼留言