題目內容:
輸入一個英文句子,句子中的單詞用空格隔開,隔開單詞的空格可能不止一個,現要求去掉單詞之間多余的空格,使得兩個單詞之間只有一個空格,且句子開頭無空格,再統計句子中單詞的個數並輸出。
輸入格式:
輸入占一行,是一個包含空格的英文句子,以回車結束
輸出格式:
輸出包括兩行,第一行是去掉多余空格后的英文句子;第二行是一個數值,表示句子中單詞的個數。
輸入樣例:
I am happy.
輸出樣例:
I am happy.
3
1 #include <iostream> 2 using namespace std; 3 #define N 250 4 5 int main() 6 { 7 char str[N] = ""; 8 char *p = str,*p1=str; 9 gets(str); 10 int count = 0; 11 12 while(*p) 13 { 14 if(*p!=' ') 15 { 16 *p1++ = *p; 17 if(*(p+1)==' '||!(*(p+1))) 18 { 19 count++; 20 *p1++ = ' '; 21 } 22 } 23 p++; 24 } 25 *(p1-1)='\0'; 26 27 cout<<str<<endl; 28 cout<<count<<endl; 29 30 return 0; 31 }
題目內容:
設計一個時間類(class Time),其中有表示“時、分、秒”的數據成員,設計初始化3個數據成員的構造函數,實參缺省時均初始化為0;設計拷貝構造函數,用一個已經存在的Time對象初始化正在創建的新對象;設計成員函數SetTime設置時間的值;設計成員函數Print以24小時格式輸出時間(如“09:20:45”、“14:30:00”)。注意,若表示時、分、秒的數據不在合理范圍內,則將不合理的數據取0值。以下是主函數:
int main()
{
int h,m,s;
cin>>h>>m>>s; //從鍵盤依次輸入時、分、秒的值
Time t1(h);
t1.Print();
t1.SetTime(h,m,s);
t1.Print();
Time t2(t1);
t2.Print();
return 0;
}
輸入格式:
輸入3個整型數,分別表示時、分、秒,數據之間以空格隔開。
輸出格式:
輸出包括三行,每行都是一個以24小時格式輸出的時間。
輸入樣例:
4 50 66
輸出樣例:
04:00:00
04:50:00
04:50:00
請注意:提交代碼時,需要完整的程序,即除了類的設計,還需要包括以上主函數。
1 #include <iostream> 2 #include <iomanip> 3 using namespace std; 4 5 class Time{ 6 int hour,minute,second; 7 public: 8 Time(int h=0,int m=0,int s=0){ 9 if(h>=24) h=0; 10 if(m>=60) m = 0; 11 if(s>=60) s = 0; 12 hour = h; 13 minute = m; 14 second = s; 15 } 16 Time(const Time &t) 17 { 18 hour = t.hour; 19 minute = t.minute; 20 second = t.second; 21 } 22 void SetTime(int h,int m,int s){ 23 if(h>=24) h=0; 24 if(m>=60) m = 0; 25 if(s>=60) s = 0; 26 hour = h; 27 minute = m; 28 second = s; 29 } 30 void Print(){ 31 cout<<setw(2)<<setfill('0')<<hour<<":"; 32 cout<<setw(2)<<setfill('0')<<minute<<":"; 33 cout<<setw(2)<<setfill('0')<<second<<endl; 34 } 35 }; 36 int main() 37 { 38 int h,m,s; 39 cin>>h>>m>>s; //從鍵盤依次輸入時、分、秒的值 40 Time t1(h); 41 t1.Print(); 42 t1.SetTime(h,m,s); 43 t1.Print(); 44 Time t2(t1); 45 t2.Print(); 46 47 return 0; 48 }
3、逆序字符串
用指針處理從鍵盤輸入的字符串,使其逆序並輸出。重復這個過程直到輸入空串為止。
1 #include "iostream" 2 #include <string.h> 3 using namespace std; 4 #define N 100 5 int main() 6 { 7 char s[N] = "";//gets(s); 8 while(cin.getline(s,N)&& strcmp(s,"\0")) 9 { 10 char *p = s+strlen(s)-1; 11 cout<<"逆序后:"; 12 while(*p) 13 { 14 cout<<*p--; 15 } 16 cout<<endl; 17 } 18 cout<<"輸入了空串,程序終止"<<endl; 19 return 0; 20 }
4、動態創建整形數組
編寫程序,根據用戶輸入的值n,建立長度為n的整型數組,再向數組輸入n個元素的值,並求其所有元素之和。
1 #include "iostream" 2 using namespace std; 3 int main() 4 { 5 int n,sum = 0; 6 cout<<"n=?"<<endl; 7 cin>>n; 8 int *p = new int[n]; 9 10 for(int i=0; i<n; ++i) 11 { 12 cin>>p[i]; 13 sum += p[i]; 14 } 15 cout<<"數組元素的和為:"<<sum<<endl; 16 delete(p); 17 return 0; 18 }
5、合並字符串
輸入兩個字符串a和b,並動態構造一個新的字符串c。要求是:將a和b對應字符中的較大者存入c對應的位置上,若a和b不一樣長,則將較長字符串多出部分的字符全部依序存入c中。
1 #include "iostream" 2 #include <string.h> 3 using namespace std; 4 #define N 100 5 int main() 6 { 7 char a[N] = "", b[N] = "", c[2*N] = ""; 8 cout<<"請輸入兩個字符串:"<<endl; 9 cin>>a>>b; 10 char *pa=a,*pb=b,*pc=c; 11 12 while(*pa&&*pb) 13 { 14 if(*pa>*pb) 15 *pc=*pa; 16 else 17 *pc=*pb; 18 pa++,pb++,pc++; 19 } 20 21 if(pa) 22 strcat(c,pa); 23 if(pb) 24 strcat(c,pb); 25 cout<<"新的字符串是:"<<c<<endl; 26 return 0; 27 }
6、編寫程序,求能被7整除 且個位數字為9的所有三位數,將滿足條件的數每10個一行顯示,並輸出滿足條件的數的個數。
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 cout<<"滿足條件的數有:"<<endl; 7 int count = 0; 8 for(int i=109; i<=999; ++i){ 9 if(i%7==0 && i%10==9){ 10 count++; 11 cout<<i<<" "; 12 if(count%10==0){ 13 cout<<endl; 14 } 15 } 16 } 17 cout<<endl<<"滿足條件的數共有:"<<count<<"個"<<endl; 18 return 0; 19 }
7、編寫程序,輸入兩個正整數m和n,求出這兩個數的最大公約數並顯示出來。
1 #include <iostream> 2 using namespace std; 3 int GCD(int x, int y); 4 int main() 5 { 6 cout<<"請輸入兩個正整數:"; 7 int a,b; 8 cin>>a>>b; 9 cout<<a<<"和"<<b<<"的最大公約數"<<GCD(a,b)<<endl; 10 return 0; 11 } 12 int GCD(int x, int y){ 13 return y == 0 ? x : GCD(y, x%y); 14 }
8、猴子吃桃問題,猴子第一天摘下若干個桃子,當即吃了一半,還不過癮,又多吃了一個,第二天早上又將剩下的桃子吃了一半,又多吃了一個。以后每天早上都吃了前一天剩下的一半零一個。到第五天早上想再吃時,見只剩下一個桃子了。請編程求第一天共摘了多少個桃子?
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int day = 4, remain = 1; 7 //因為第5天早上想吃的時候只剩一個,從計算第4天吃之前的桃子數開始 8 for(day =4; day>0; --day) 9 { 10 remain = 2 * (remain + 1);//第一輪計算第4天早上的沒吃的時候的桃子,以此類推 11 } 12 13 cout<<"第一天一共摘了"<<remain<<"個桃子"<<endl; 14 return 0; 15 }
9、輸入整型數組的元素個數n,依次輸入n個數組元素,求其中負數的和。
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 cout<<"請輸入數組元素個數:"<<endl; 7 const int N = 100; 8 int arr[N] = {0}, n, index=0, sum = 0; 9 10 cin>>n; 11 while(n--) 12 { 13 cin>>arr[index]; 14 if(arr[index]<0) 15 sum += arr[index]; 16 index++; 17 } 18 cout<<"數組中負數元素的和為:"<<sum<<endl; 19 return 0; 20 }
10、求一個n階矩陣的轉置。輸入n值及矩陣的所有元素,存儲在二維數組a中,要求在a中進行轉置。
1 #include<iostream> 2 using namespace std; 3 const int N = 100; 4 5 void Print(int arr[][N], int n); 6 int main() 7 { 8 cout<<"請輸入矩陣階數:"<<endl; 9 int arr[N][N] = {0}, n; 10 cin>>n; 11 12 /* 輸入矩陣 */ 13 for(int i=0; i<n; ++i){ 14 for(int j=0; j<n; ++j){ 15 cin>>arr[i][j]; 16 } 17 } 18 /* 輸出矩陣 */ 19 cout<<"原矩陣為:"<<endl; 20 Print(arr,n); 21 22 /* 轉置矩陣 */ 23 for(int i=0; i<n; ++i){ 24 for(int j=0; j<i; ++j){ 25 int t = arr[i][j]; 26 arr[i][j] = arr[j][i]; 27 arr[j][i] = t; 28 } 29 } 30 /* 輸出轉置矩陣 */ 31 cout<<"轉置后矩陣為:"<<endl; 32 Print(arr,n); 33 return 0; 34 } 35 36 void Print(int arr[][N], int n) 37 { 38 for(int i=0; i<n; ++i){ 39 for(int j=0; j<n; ++j){ 40 cout<<arr[i][j]<<"\t"; 41 } 42 cout<<endl; 43 } 44 }
11、假設一個字符串中不含空格,輸入該字符串並統計其中字母和非字母的個數。
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 cout<<"請輸入一個不含空格的字符串:"<<endl; 7 const int N = 100; 8 char arr[N] = ""; 9 cin>>arr; 10 11 int count1 = 0, count2 = 0; 12 for(int i=0; arr[i]; ++i){ 13 if(arr[i]>='a'&&arr[i]<='z' || arr[i]>='A'&&arr[i]<='Z') 14 count1++; 15 else 16 count2++; 17 } 18 cout<<"字符串中字母的個數為:"<<count1<<endl; 19 cout<<"字符串中非字母的個數為:"<<count2<<endl; 20 return 0; 21 }
12、輸入不含空格的字符串s及待刪除的字符ch,將s中所有與ch相同的字符都刪除掉,輸出刪除后得到的新串。要求直接在數組s中進行刪除,得到的新串仍然在數組s中。
1 #include<iostream> 2 #include <string.h> 3 using namespace std; 4 #define N 100 5 6 void deletechar(char s[],char c); 7 int main() 8 { 9 cout<<"請輸入一個不含空格的字符串:"<<endl; 10 char s[N] = "", c; 11 cin>>s; 12 13 cout<<"請輸入要刪除的字符:"<<endl; 14 cin>>c; 15 16 deletechar(s,c); 17 cout<<"刪除后的字符串為:"<<s<<endl; 18 return 0; 19 } 20 void deletechar(char s[],char c){ 21 char t[N] = ""; 22 char *p = s, *q = t; 23 while(*p) 24 { 25 if(*p!=c) 26 *q++ = *p; 27 p++; 28 } 29 strcpy(s,t); 30 }
13、用指針處理從鍵盤輸入的字符串,使其逆序並輸出。重復這個過程直到輸入空串為止。
1 #include "iostream" 2 #include <string.h> 3 using namespace std; 4 #define N 100 5 int main() 6 { 7 char s[N] = "";//gets(s); 8 while(cin.getline(s,N)&& strcmp(s,"\0")) 9 { 10 char *p = s+strlen(s)-1; 11 cout<<"逆序后:"; 12 while(*p) 13 { 14 cout<<*p--; 15 } 16 cout<<endl; 17 } 18 cout<<"輸入了空串,程序終止"<<endl; 19 return 0; 20 }
14、編寫程序,根據用戶輸入的值n,建立長度為n的整型數組,再向數組輸入n個元素的值,並求其所有元素之和。
1 #include "iostream" 2 using namespace std; 3 int main() 4 { 5 int n,sum = 0; 6 cout<<"n=?"<<endl; 7 cin>>n; 8 int *p = new int[n]; 9 10 for(int i=0; i<n; ++i) 11 { 12 cin>>p[i]; 13 sum += p[i]; 14 } 15 cout<<"數組元素的和為:"<<sum<<endl; 16 delete(p); 17 return 0; 18 }
15、輸入兩個字符串a和b,並動態構造一個新的字符串c。要求是:將a和b對應字符中的較大者存入c對應的位置上,若a和b不一樣長,則將較長字符串多出部分的字符全部依序存入c中。
1 #include "iostream" 2 #include <string.h> 3 using namespace std; 4 #define N 100 5 int main() 6 { 7 char a[N] = "", b[N] = "", c[2*N] = ""; 8 cout<<"請輸入兩個字符串:"<<endl; 9 cin>>a>>b; 10 char *pa=a,*pb=b,*pc=c; 11 12 while(*pa&&*pb) 13 { 14 if(*pa>*pb) 15 *pc=*pa; 16 else 17 *pc=*pb; 18 pa++,pb++,pc++; 19 } 20 21 if(pa) 22 strcat(c,pa); 23 if(pb) 24 strcat(c,pb); 25 cout<<"新的字符串是:"<<c<<endl; 26 return 0; 27 }
16、計算4名學生3門課程成績的平均分。在主函數中給定各門課程的成績和輸出計算得到的各平均分,avg函數用於計算各課程的平均分。
1 #include<iostream> 2 using namespace std; 3 4 double avg(double a, double b, double c){ 5 return (a+b+c)/3; 6 } 7 8 int main() 9 { 10 int n = 4; 11 double a,b,c; 12 while(n--) 13 { 14 cin>>a>>b>>c; 15 cout<<avg(a,b,c)<<endl; 16 } 17 return 0; 18 }
17、編寫程序,求出輸入的一個正整數n的階乘。要求設計遞歸函數求出n的階乘。
1 #include<iostream> 2 using namespace std; 3 4 long fac(int n){ 5 if(n==1) 6 return 1; 7 return n*fac(n-1); 8 } 9 10 int main() 11 { 12 int n; 13 cin>>n; 14 cout<<fac(n)<<endl; 15 return 0; 16 }
18、編寫程序。輸入一個正整數判斷其是否是質數。要求設計一個函數對正整數n是否是質數進行判斷,是質數函數返回1,不是質數返回0。
1 #include<iostream> 2 using namespace std; 3 4 bool isPrime(int n){ 5 for(int i=2; i<n; ++i) 6 if(n%i==0) 7 return false; 8 return true; 9 } 10 11 int main() 12 { 13 int n; 14 cin>>n; 15 if(isPrime(n)) 16 cout<<"是質數\n"<<endl; 17 else 18 cout<<"不是質數\n"<<endl; 19 return 0; 20 }