用C++實現輸入三個整數,中間用逗號隔開
阿新 • • 發佈:2020-12-21
輸入字元
之前做到一些藍橋杯的題目時,不會在輸入時輸入字元,今天特地學習了一下,我總結了兩種方法。
以輸入三個整數,輸出它們之間的最大值舉例。
第一種:用C語言實現
1 #include<stdio.h>
2 int m(int a,int b,int c) //此函式用於求三個數的最大值
3 {
4 int max=0;
5 if(a>max) max=a;
6 if(b>max) max=b;
7 if(c>max) max=c;
8 return max;
9 }
10 int main()
11 {
12 int a,b,c,max;
13 scanf("%d,%d,%d",&a,&b,&c); //C語言不用定義一個字元變數,可直接輸入字元
14 max=m(a,b,c);
15 printf("%d\n",max);
16 return 0;
17 }
第二種:用C++實現
1 #include<iostream>
2 using namespace std;
3 int m(int a,int b,int c) //此函式用於求三個數的最大值
4 {
5 int max=0;
6 if(a>max) max=a;
7 if(b>max) max=b;
8 if(c>max) max=c;
9 return max;
10 }
11 int main()
12 {
13 int a,b,c,max;
14 char ch; //定義一個字元變數
15 cin>>a>>ch>>b>>ch>>c; //可以在在整數之間輸入任意一個字元
16 cout<<"max="<<m(a,b,c)<<endl;
17 return 0;
18 }
執行結果: