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; else temp = b; if(temp > c) max = temp; else max = 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; else temp = b; if(temp > c) max = temp; else max = c; cout<<"max="<<max<<endl; return 0; }