Coursera_程序设计与算法_计算导论与C语言基础_数组应用练习

您也可以在我的个人博客中阅读此文章:跳转

描述

在一个字符串中找出元音字母a,e,i,o,u出现的次数。

输入

输入一行字符串(字符串中可能有空格,请用gets(s)方法把一行字符串输入到字符数组s中),字符串长度小于80个字符。

输出

输出一行,依次输出a,e,i,o,u在输入字符串中出现的次数,整数之间用空格分隔。

##样例输入

If so, you already have a Google Account. You can sign in on the right.

##样例输出

5 4 3 7 3

##代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;
int main () {
string s;
getline(cin,s);
int len=s.size();
int a=0,b=0,c=0,d=0,e=0;
for (int i=0;i<len;i++){
if (s[i]=='a') a++;
if (s[i]=='e') b++;
if (s[i]=='i') c++;
if (s[i]=='o') d++;
if (s[i]=='u') e++;
}
cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;
return 0;
}

编程题#2:忽略大小写比较字符串大小

描述

一般我们用strcmp可比较两个字符串的大小,比较方法为对两个字符串从前往后逐个字符相比较(按ASCII码值大小比较),直到出现不同的字符或遇到’\0’为止。如果全部字符都相同,则认为相同;如果出现不相同的字符,则以第一个不相同的字符的比较结果为准。但在有些时候,我们比较字符串的大小时,希望忽略字母的大小,例如”Hello”和”hello”在忽略字母大小写时是相等的。请写一个程序,实现对两个字符串进行忽略字母大小写的大小比较。

输入

输入为两行,每行一个字符串,共两个字符串。(请用gets录入每行字符串)(每个字符串长度都小于80)

输出

如果第一个字符串比第二个字符串小,输出一个字符”<”

如果第一个字符串比第二个字符串大,输出一个字符”>”

如果两个字符串相等,输出一个字符”=”

样例输入

1
2
Hello
hello

样例输出

1
=

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
using namespace std;
int main () {
string s1,s2;
cin>>s1>>s2;
for (int i=0;i<s1.size();i++)
s1[i]=toupper(s1[i]);
for (int i=0;i<s2.size();i++)
s2[i]=toupper(s2[i]);
if (s1<s2) cout<<"<";
else if (s1>s2) cout<<">";
else cout<<"=";
return 0;
}

编程题#3:最长单词2

描述

一个以’.’结尾的简单英文句子,单词之间用空格分隔,没有缩写形式和其它特殊形式

输入

一个以’.’结尾的简单英文句子(长度不超过500),单词之间用空格分隔,没有缩写形式和其它特殊形式

输出

该句子中最长的单词。如果多于一个,则输出第一个

样例输入

I am a student of Peking University.

样例输出

University

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstring>
#include <string>
#include <sstream>
#include <cstdio>
using namespace std;
int main () {
string s;
char t[600];
gets(t);
s=t;
s[s.size()-1]=' ';
//puts(t);
stringstream ss(s);
string ans;
int len=0;
while (ss>>s) if (s.size()>len) {len=s.size();ans=s;}
cout<<ans<<endl;
return 0;
}

编程题#4:矩阵交换行

描述

编写一个函数,输入参数是5*5的二维数组,和n,m两个行下标。功能:判断n,m是否在数组范围内,如果不在,则返回0;如果在范围内,则将n行和m行交换,并返回1。

在main函数中, 生成一个5*5的矩阵,输入矩阵数据,并输入n,m的值。调用前面的函数。如果返回值为0,输出error。如果返回值为1,输出交换n,m后的新矩阵。

输入

5*5矩阵的数据,以及n和m的值。

输出

如果不可交换,则输出error;

如果可交换,则输出新矩阵

样例输入

1
2
3
4
5
6
1 2 2 1 2
5 6 7 8 3
9 3 0 5 3
7 2 1 4 6
3 0 8 2 4
0 4

样例输出

1
2
3
4
5
3 0 8 2 4
5 6 7 8 3
9 3 0 5 3
7 2 1 4 6
1 2 2 1 2

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Author: Bingo
* Created Time: 2015/8/6 19:33:47
* File Name: 9-4.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
#include <iomanip>
using namespace std;
const int maxint = -1u>>1;
int main () {
int a[5][5];
for (int i=0;i<5;i++)
for (int j=0;j<5;j++)
cin>>a[i][j];
int n,m;
cin>>n>>m;
if (n<0||n>4||m<0||m>4) cout<<"error"<<endl;
else {
for (int i=0;i<5;i++){
if (i==n){
for(int j=0;j<4;j++) cout<< setw(4)<<a[m][j];
cout<< setw(4)<<a[m][4]<<endl;
}else if (i==m){
for(int j=0;j<4;j++) cout<< setw(4)<<a[n][j];
cout<< setw(4)<<a[n][4]<<endl;
}else {
for(int j=0;j<4;j++) cout<< setw(4)<<a[i][j];
cout<< setw(4)<<a[i][4]<<endl;
}
}
}
return 0;
}

编程题#5:异常细胞检测

描述

我们拍摄的一张CT照片用一个二维数组来存储,假设数组中的每个点代表一个细胞。每个细胞的颜色用0到255之间(包括0和255)的一个整数表示。我们定义一个细胞是异常细胞,如果这个细胞的颜色值比它上下左右4个细胞的颜色值都小50以上(包括50)。数组边缘上的细胞我们不检测。现在我们的任务是,给定一个存储CT照片的二维数组,写程序统计照片中异常细胞的数目。

输入

第一行包含一个整数N(100>=N>2).

下面有 N 行,每行有 N 个0~255之间的整数,整数之间用空格隔开。

输出

输出只有一行,包含一个整数,为异常细胞的数目。

样例输入

1
2
3
4
5
4
70 70 70 70
70 10 70 70
70 70 20 70
70 70 70 70

样例输出

2

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# include <iostream>
# include <cmath>
using namespace std;
int main () {
int n;
int a[150][150];
cin>>n;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
cin>>a[i][j];
int cnt=0;
for (int i=1;i<n-1;i++)
for (int j=1;j<n-1;j++)
if ((a[i-1][j]-a[i][j]>=50)&&(a[i+1][j]-a[i][j]>=50)&&(a[i][j-1]-a[i][j]>=50)&&(a[i][j+1]-a[i][j]>=50)) {cnt++;}
cout<<cnt<<endl;
return 0;
}

编程题#6:循环移动

描述

给定一组整数,要求利用数组把这组数保存起来,再利用指针实现对数组中的数循环移动。假定共有n个整数,则要使前面各数顺序向后移m个位置,并使最后m各数变为最前面的m各数。

注意,不要用先输出后m个数,再输出前n-m个数的方法实现,也不要用两个数组的方式实现。

要求只用一个数组的方式实现,一定要保证在输出结果时,输出的顺序和数组中数的顺序是一致的。

输入

输入有两行:第一行包含一个正整数n和一个正整数m,第二行包含n个正整数。每两个正整数中间用一个空格分开。

输出

输出有一行:经过循环移动后数组中整数的顺序依次输出,每两个整数之间用空格分隔。

样例输入

1
2
11 4
15 3 76 67 84 87 13 67 45 34 45

样例输出

1
67 45 34 45 15 3 76 67 84 87 13

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* Author: Bingo
* Created Time: 2015/8/7 10:41:45
* File Name: 9-6.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
int main () {
int n,m;
cin>>n>>m;
int a[500];
for (int i=0;i<n;i++) cin>>a[i];
for (int k=0;k<m;k++){
int t=a[n-1];
for (int i=n-1;i>0;i--) a[i]=a[i-1];
a[0]=t;
}
for (int i=0;i<n-1;i++) cout<<a[i]<<" ";
cout<<a[n-1]<<endl;
return 0;
}

编程题#7:中位数

描述

中位数定义:一组数据按从小到大的顺序依次排列,处在中间位置的一个数或最中间两个数据的平均值(如果这组数的个数为奇数,则中位数为位于中间位置的那个数;如果这组数的个数为偶数,则中位数是位于中间位置的两个数的平均值).

给出一组无序整数,求出中位数,如果求最中间两个数的平均数,向下取整即可(不需要使用浮点数)

输入

该程序包含多组测试数据,每一组测试数据的第一行为N,代表该组测试数据包含的数据个数,1 <= N <= 15000.

接着N行为N个数据的输入,N=0时结束输入

输出

输出中位数,每一组测试数据输出一行

样例输入

1
10
30
20
40
3
40
30
50
4
1
2
3
4
0

样例输出

1
2
3
25
40
2

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* Author: Bingo
* Created Time: 2015/8/7 11:16:29
* File Name: 9-7.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
int main () {
int n;
while(cin>>n&&n){
int a[n];
for (int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
if (n%2) cout<<a[n/2]<<endl;
else cout<<(a[n/2-1]+a[n/2])/2<<endl;
}
return 0;
}

编程题#8:校门外的树

描述

某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米。我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置;数轴上的每个整数点,即0,1,2,……,L,都种有一棵树。

马路上有一些区域要用来建地铁,这些区域用它们在数轴上的起始点和终止点表示。已知任一区域的起始点和终止点的坐标都是整数,区域之间可能有重合的部分。现在要把这些区域中的树(包括区域端点处的两棵树)移走。你的任务是计算将这些树都移走后,马路上还有多少棵树。

输入

输入的第一行有两个整数L(1 <= L <= 10000)和 M(1 <= M <= 100),L代表马路的长度,M代表区域的数目,L和M之间用一个空格隔开。接下来的M行每行包含两个不同的整数,用一个空格隔开,表示一个区域的起始点和终止点的坐标。

输出

输出包括一行,这一行只包含一个整数,表示马路上剩余的树的数目。

样例输入

1
2
3
4
500 3
150 300
100 200
470 471

样例输出

1
298

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
* Author: Bingo
* Created Time: 2015/8/7 11:25:51
* File Name: 9-8.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
int main () {
int a[10002];
int n,m,l;
int x,y;
memset(a,0,sizeof(a));
cin>>l>>m;
for (int i=0;i<m;i++){
cin>>x>>y;
for(int j=x;j<=y;j++)
a[j]=1;
}
int ans=0;
for (int i=0;i<=l;i++)
if (a[i]==0) ans++;
//if (ans) ans+=1;
cout<<ans<<endl;
return 0;
}