第8讲 函数 单元作业
1. 编一判断m是否为素数的函数,并在主函数中利用它输出十对最小的孪生素数。所谓孪生素数是指两个相差为2的素数,如3和5,11和13。程序运行结果见下图。
函数形式为:
bool isprime(int m)
yzy.ver:
1 #include "iostream" 2 using namespace std; 3 bool isprime(int m) 4 { 5 int i; 6 bool flag = 0; 7 for (i = 2; ; i++) 8 if (m % i == 0) 9 break; 10 if (i == m) 11 flag= 1; 12 return flag; 13 } 14 int main() 15 { 16 int m=2, s = 0; 17 while (s < 10) 18 { 19 if (isprime(m) && isprime(m + 2)) 20 { 21 cout << "<" << m << "," << m + 2 << ">" << endl; 22 s++; 23 } 24 m++; 25 } 26 system("pause"); 27 return 0; 28 }
cm's best ver:
1 #include "iostream" 2 3 using namespace std; 4 5 6 7 bool isprime(int m); 8 9 int main() 10 11 { 12 13 int two = 2, count = 0; 14 15 for(int j = 2; count<10; ++j){ 16 17 if(isprime(j) && isprime(j+two)){ 18 19 count++; 20 21 printf("(%d,%d)\n",j,j+two); 22 23 } 24 25 } 26 27 return 0; 28 29 } 30 31 bool isprime(int m){ 32 33 for(int i=2; i<m; ++i){ 34 35 if(m%i==0) 36 37 return false; 38 39 } 40 41 return true; 42 43 }
2.
编一函数,功能为判断一字符串是否为回文,如果是回文则返回1,否则返回0。回文是指顺读和倒读都一样的字符串,如“deed”和“level”是回文。在主函数中对输入的字符串加以调用。
函数形式为:int huiwen(char s[])
yzy.ver:
1 #include "iostream" 2 #define N 100 3 using namespace std; 4 int huiwen(char s[]) 5 { 6 int i,m; 7 m = strlen(s); 8 bool flag = 1; 9 for (i = 0; i <= (m - 1) / 2; i++) 10 { 11 if (s[i] != s[m - 1 - i]) 12 { 13 flag = 0; 14 } 15 } 16 return flag; 17 } 18 int main() 19 { 20 char s[N]; 21 gets_s(s); 22 int flag; 23 flag = huiwen(s); 24 cout << flag << endl; 25 system("pause"); 26 return 0; 27 }
cm's best ver:
1 #include <iostream> 2 3 #include <string.h> 4 5 using namespace std; 6 7 8 9 int huiwen(char s[]); 10 11 int main() 12 13 { 14 15 const int N = 100; 16 17 char s[N] = ""; 18 19 cin>>s; 20 21 if(huiwen(s)) 22 23 cout<<"是回文"<<endl; 24 25 else 26 27 cout<<"不是回文"<<endl; 28 29 return 0; 30 31 } 32 33 int huiwen(char s[]) 34 35 { 36 37 char *p = s, *q = s+strlen(s)-1; 38 39 while(p<q) 40 41 { 42 43 if(*p!=*q) 44 45 return 0; 46 47 p++, q--; 48 49 } 50 51 return 1; 52 53 }
3.
函数的功能是将学生成绩从高分到低分排序,并统计优秀与不及格的人数。用下面两种方法实现:
(1)函数形式为:int fun1(int s[],int n,int *x)
要求优秀人数通过return返回,不及格人数通过指针参数返回结果。
(2)函数形式为:void fun2(int s[],int n,int &x,int &y)
要求优秀与不及格的人数通过引用参数返回结果。
分别编二个函数,学生人数从键盘输入。
yzy.ver:
1 //函数一: 2 #include "iostream" 3 #define N 100 4 using namespace std; 5 int fun1(int s[], int n, int *x) 6 { 7 int i,j,k,t,r=0,p=0; 8 for (i = 0; i < n-1 ; i++) 9 { 10 k = i; 11 for (j = i + 1; j < n; j++) 12 if (s[k] < s[j]) 13 k = j; 14 if (i != k) 15 { 16 t = s[i]; 17 s[i] = s[k]; 18 s[k] = t; 19 } 20 } 21 for (i = 0; i < n; i++) 22 { 23 if (s[i] >= 90) 24 r++; 25 if (s[i] < 60) 26 p++; 27 } 28 *x = p; 29 return r; 30 } 31 int main() 32 { 33 int s[N]; 34 cout << "输入学生人数:"; 35 int n,i, num=0; 36 cin >> n; 37 cout << "输入学生成绩(满分100):"; 38 for (i = 0; i < n; i++) 39 { 40 cin >> s[i]; 41 } 42 fun1(s, n, &num); 43 cout << "优秀人数为" << fun1(s, n, &num) << endl; 44 cout << "不及格人数为" << num << endl; 45 cout << "从高到低排序成绩:" << endl; 46 for (i = 0; i < n; i++) 47 { 48 cout << s[i]<<' '; 49 } 50 system("pause"); 51 return 0; 52 } 53 //函数二: 54 #include "iostream" 55 #define N 100 56 using namespace std; 57 void fun2(int s[], int n, int& x, int& y) 58 { 59 int i, j, k, t, r = 0, p = 0; 60 for (i = 0; i < n - 1; i++) 61 { 62 k = i; 63 for (j = i + 1; j < n; j++) 64 if (s[k] < s[j]) 65 k = j; 66 if (i != k) 67 { 68 t = s[i]; 69 s[i] = s[k]; 70 s[k] = t; 71 } 72 } 73 for (i = 0; i < n; i++) 74 { 75 if (s[i] >= 90) 76 r++; 77 if (s[i] < 60) 78 p++; 79 } 80 x = r; y = p; 81 82 } 83 int main() 84 { 85 int s[N]; 86 cout << "输入学生人数:"; 87 int n,i, x, y; 88 cin >> n; 89 cout << "输入学生成绩(满分100):"; 90 for (i = 0; i < n; i++) 91 { 92 cin >> s[i]; 93 } 94 fun2(s, n, x,y); 95 cout << "优秀人数为" << x << endl; 96 cout << "不及格人数为" << y << endl; 97 cout << "从高到低排序成绩:" << endl; 98 for (i = 0; i < n; i++) 99 { 100 cout << s[i]<<' '; 101 } 102 system("pause"); 103 return 0; 104 }
cm's best ver:
1 #include "iostream" 2 3 #include <string.h> 4 5 using namespace std; 6 7 8 9 int fun1(int s[],int n,int *x); 10 11 void fun2(int s[],int n,int &x,int &y); 12 13 int main() 14 15 { 16 17 const int N = 100; 18 19 int s[N] = {0}; 20 21 int n; 22 23 cin>>n; 24 25 for(int i=0; i<n; ++i) 26 27 cin>>s[i]; 28 29 30 31 int x=0,y=0; 32 33 cout<<"优秀的人数:"<<fun1(s,n,&x)<<endl; 34 35 cout<<"不及格的人数:"<<x<<endl; 36 37 38 39 x=0,y=0; 40 41 fun2(s,n,x,y); 42 43 cout<<"优秀的人数:"<<y<<endl<<"不及格的人数:"<<x<<endl; 44 45 46 47 return 0; 48 49 } 50 51 int fun1(int s[],int n,int *x){ 52 53 int excellent=0; 54 55 * x = 0;// 56 57 for(int i=0; i<n-1; ++i){/*第0个元素有序,从第1个元素向右无序*/ 58 59 int max = s[i]; 60 61 int index = i; 62 63 for(int j = i+1; j<n; ++j){/*从i+1逐个比较*/ 64 65 if(max<s[j]){ /*是否比后面的小*/ 66 67 max = s[j]; 68 69 index = j; 70 71 } 72 73 } 74 75 if(index != i){/*找到了最大值才交换*/ 76 77 s[index] = s[i]; 78 79 s[i] = max; 80 81 } 82 83 } 84 85 for(int k=0; k<n; ++k){ 86 87 if(s[k]>=90) excellent++; 88 89 if(s[k]<60) (*x)++; 90 91 } 92 93 return excellent; 94 95 } 96 97 void fun2(int s[],int n,int &x,int &y){ 98 99 x=y=0; // 100 101 for(int i=0; i<n-1; ++i){/*第0个元素有序,从第1个元素向右无序*/ 102 103 int max = s[i]; 104 105 int index = i; 106 107 for(int j = i+1; j<n; ++j){/*从i+1逐个比较*/ 108 109 if(max<s[j]){ /*是否比后面的小*/ 110 111 max = s[j]; 112 113 index = j; 114 115 } 116 117 } 118 119 if(index != i){/*找到了最大值才交换*/ 120 121 s[index] = s[i]; 122 123 s[i] = max; 124 125 } 126 127 } 128 129 for(int k=0; k<n; ++k){ 130 131 if(s[k]>=90) y++; 132 133 if(s[k]<60) x++; 134 135 } 136 137 }
or:
1 #include "iostream" 2 using namespace std; 3 4 int fun1(int s[], int n, int* x) 5 { 6 int i, j, k, good = 0; 7 *x = 0; 8 for (i = 0; i <= n - 1; i++) 9 { 10 for (j = n - 1; j > i; j--) 11 if (s[j] > s[j - 1]) 12 { 13 k = s[j]; 14 s[j] = s[j - 1]; 15 s[j - 1] = k; 16 } 17 } 18 for (i = 0; i <= n - 1; i++) 19 { 20 if (s[i] >= 90) 21 good += 1; 22 else if (s[i] <= 60) 23 *x += 1; 24 } 25 return good; 26 } 27 28 void fun2(int s[], int n, int& x, int& y) 29 { 30 int i, j, k; 31 x = y = 0; 32 for (i = 0; i <= n - 1; i++) 33 { 34 for (j = n - 1; j > i; j--) 35 if (s[j] > s[j - 1]) 36 { 37 k = s[j]; 38 s[j] = s[j - 1]; 39 s[j - 1] = k; 40 } 41 } 42 for (i = 0; i <= n - 1; i++) 43 { 44 if (s[i] >= 90) 45 x += 1; 46 else if (s[i] <= 60) 47 y += 1; 48 } 49 } 50 51 int main() 52 { 53 //数据输入 54 int n, s[100], good, bad; 55 cout << "请输入学生人数:" << endl; 56 cin >> n; 57 cout << "请输入学生成绩:" << endl; 58 for (int i = 0; i <= n - 1; i++) 59 cin >> s[i]; 60 61 //调用fun1 62 good = fun1(s, n, &bad); 63 cout << "排序后的成绩为:" << endl; 64 for (int i = 0; i <= n - 1; i++) 65 cout << s[i] << " "; 66 cout << endl << "优秀人数为:" << good << endl; 67 cout << "不及格人数为:" << bad << endl; 68 69 //调用fun2 70 fun2(s, n, good, bad); 71 cout << "排序后的成绩为:" << endl; 72 for (int i = 0; i <= n - 1; i++) 73 cout << s[i] << " "; 74 cout << endl << "优秀人数为:" << good << endl; 75 cout << "不及格人数为:" << bad << endl; 76 77 system("pause"); 78 return 0; 79 }
4.
编一函数,功能为统计字符串中各个字母(不区分大、小写)出现的频率,同时找出频率出现最高的字母及次数,假设出现次数最多的字母只有一个。函数形式为:
void freq(char s[],int p[],char &chmax,int &max)
程序运行结果如下:
![]()
yzy.ver:
1 #include "iostream" 2 #define N 256 3 using namespace std; 4 5 void freq(char s[], int p[], char& chmax, int& max) 6 { 7 char *a = s; 8 while(*a) 9 { 10 if (*a > 'a' && *a < 'z' || *a>'A' && *a < 'Z') 11 { 12 if (*a < 'a') 13 p[*a + 32]++; 14 else 15 p[*a]++; 16 if (p[*a] > max) 17 { 18 max = p[*a]; 19 chmax = *a; 20 } 21 } 22 a++; 23 } 24 } 25 26 27 28 int main() 29 { 30 char s[N] = "" ; 31 char chmax ; 32 int p[N]={}, max=0; 33 gets_s(s); 34 freq(s, p, chmax, max); 35 for (int i = 0; i < N; i++) 36 { 37 if (p[i]) 38 cout << char(i) << "----" << p[i] << endl; 39 } 40 cout << "出现频率最高的字母:" << chmax << "----" << max << endl; 41 system("pause"); 42 return 0; 43 }
cm's best ver:
1 #include "iostream" 2 3 #include <string.h> 4 5 using namespace std; 6 7 8 9 void freq(char s[],int p[],char &chmax,int &max); 10 11 int main() 12 13 { 14 15 char chmax; 16 17 int max = 0; 18 19 const int N = 256; 20 21 int p[N] = {0}; 22 23 char s[N] = ""; 24 25 gets(s); 26 27 freq(s,p,chmax,max); 28 29 for(int i=0; i<N; ++i){ 30 31 if(p[i]) 32 33 printf("%c-----%d\n",i,p[i]); 34 35 } 36 37 printf("出现频率最高的字母:%c-----%d\n",chmax,max); 38 39 return 0; 40 41 } 42 43 void freq(char s[],int p[],char &chmax,int &max){ 44 45 char *q = s; 46 47 while(*q) 48 49 { 50 51 if(*q>='a'&&*q<='z'||*q>='A'&&*q<='Z') 52 53 { 54 55 if(*q<'a') 56 57 p[*q+32]++; 58 59 else 60 61 p[*q]++; 62 63 if(p[*q]>max){ 64 65 max = p[*q]; 66 67 chmax = *q; 68 69 } 70 71 } 72 73 q++; 74 75 } 76 77 }

浙公网安备 33010602011771号