[PTA] 7-1 find the smallest string for the input N strings, output the smallest string (c ++), pta7-1
7-1 find the smallest string this question requires programming, for the input N strings, output the smallest string.
Input Format:
The first line of the input is a positive integer N, followed by N rows. Each line is a non-null string of less than 80 characters, without line breaks, spaces, and tabs.
Output Format:
Output the smallest string in the following format in a row:
Min is: Minimum string input example:
5 Li Wang Zha Jin Xian output example:
Min is: Jin
Answer:
1 #include <iostream> 2 #include<string> 3 using namespace std; 4 5 int main() 6 { 7 int N; 8 string Min, k; 9 10 cin>>N;11 cin>>k;12 Min=k;13 14 for(int i=0;i<N-1;i++)15 {16 17 cin>>k;18 19 if(k<Min) Min=k;20 21 }22 cout<<"Min is: "<<Min<<endl;23 //system("pause"); 24 }
Attached debugging process:
1 # include <iostream> 2 # include <string> 3 using namespace std; 4 5 int main () 6 {7 int N; 8 string Max, Min, k; 9 10 cin> N; 11 12 cout <"enter" <N <"strings:" <endl; 13 14 cin> k; 15 Max = k; 16 Min = k; 17 18 cout <"k:" <k <endl; 19 cout <"Max: "<Max <endl; 20 cout <" Min: "<Min <endl; 21 22 for (int I = 1; I <N; I ++) 23 {24 cout <"Enter the" <I + 1 <"string:" <endl; 25 cin> k; 26 27 if (k> Max) Max = k; 28 cout <"Max:" <Max <endl; 29 if (k <Min) Min = k; 30 cout <"Min:" <Min <endl; 31 32} 33 cout <"Max is:" <Max <endl; 34 cout <"Min is:" <Min <endl; 35 36 system ("pause"); 37 38}
Maybe you have a better idea. Please contact us!