SDUT 1154 求三个整数的最大值

来源:互联网 发布:c语言200种游戏源代码 编辑:程序博客网 时间:2024/06/05 19:22

Problem Description

请编写程序,输入三个整数,求出其中的最大值输出。

Input

在一行上输入三个整数,整数间用逗号分隔。

Output

输出三个数中的最大值。

Example Input

5,7,9

Example Output

max=9
答案:
#include<iostream>#include<stdio.h>using namespace std;int main() {int a, b, c, temp, max;scanf("%d, %d, %d",&a, &b, &c);//使用C中的scanf输入函数可以达到输入之间逗号隔开if(a > b)temp = a;elsetemp = b;if(temp > c)max = temp;elsemax = c;cout<<"max="<<max<<endl;return 0;}
或者是:(纯C++版本,但比较繁琐)
#include<iostream>#include<stdio.h>using namespace std;int main() {int a, b, c, temp, max;cin>> a;if (cin.get()==',' ) //如果不按 数字+逗号+数字格式输入,则不允许输入第二个数,以达到限制要求      否则,用户输入数字+空格+数字也能达到输入两个数字    {        cin>>b ;    }if (cin.get()==',' ) //如果不按 数字+逗号+数字格式输入,则不允许输入第二个数,以达到限制要求      否则,用户输入数字+空格+数字也能达到输入两个数字    {        cin>>c ;    }if(a > b)temp = a;elsetemp = b;if(temp > c)max = temp;elsemax = c;cout<<"max="<<max<<endl;return 0;}