云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > C++程序设计基本编程题型全解

C++程序设计基本编程题型全解

  • 62 次阅读
  • 3 次下载
  • 2025/6/14 16:35:41

} 法二

#include

bool IsSmith(long a);//是否是Smith数 int Sum(long a);//个位数之和 void main(void) { }

bool IsSmith(long a) { 因数之和

}

int Sum(long a) { long a; a=4937774; while(!IsSmith(a)) { a++; } cout<<\大于等于4937774的下一个Smith数是:\long sum_m=0,sum_n=0;//sum_m为各位数之和,sum_n为个质long temp; long t;// temp=a; t=2;//从最小质因数开始 sum_m=Sum(temp);//获得各位数之和 while(t!=a) { if(temp%t) { t++; } else { sum_n+=Sum(t); temp/=t; } } if(sum_m==sum_n)return true; else return false; long sum_m; long temp; long t;

temp=a; sum_m=0; while(temp!=0) { t=temp/10; sum_m+=temp-10*t; temp=t; } return sum_m; }

(7)先编写一个函数:int getRev(char a[]),计算并返回字符串a的逆序。

逆序的概念:在一个字符串中,如果存在ia[j],则称a[i]和a[j]构成一个逆序。例如\的逆序是5,其中D与A、A、B、C构成4个逆序,E与C构成1个逆序。

要求任意输入MAX个字符串(MAX是一个宏,值为>2的正整数),每个串不多于20个字符,先计算各串的逆序数,再按逆序数升序输出各串及其逆序数。

#include #include #define MAX 8 char s[MAX][20]; int a[MAX];

int getRev(const char a[]) { int res=0; for(int i=0;ia[j]) res++; } return res; }

void s_swap(char *a ,char * b) { char tmp[20]; strcpy(tmp,a); strcpy(a,b); strcpy(b,tmp); }

void a_swap(int &a,int &b) { int tmp; tmp=a; a=b;

b=tmp; }

void main() { int i; for(i=0;i>s[i]; a[i]=getRev(s[i]); } for(i=0;i<=MAX-2;i++) for(int j=0;j<=MAX-2-i;j++) { if(a[j]>a[j+1]) { s_swap(s[j],s[j+1]); a_swap(a[j],a[j+1]); } } cout<<\按逆序数升序输出:\ for(i=0;i<=MAX-1;i++) { cout<

(8) 一个集合set中的各个元素相互之间不相等。先编写一个函数:int getSet(int rs[], const int a[], int n),从数组a中取出相互不等的元素,放入数组rs中,并返回rs中元素的个数。此时数组rs中各元素都不相等,就构成一个集合set。例如:a={3, 1, 2, 3, 1, 5, 2, 1},那么结果rs={3, 1, 2, 5},返回4。先测试验证该函数的正确性。

然后再尝试设计一个函数,不仅能得到集合数组,而且得到集合中各元素出现的次数。例如上面例子中集合rs={3, 1, 2, 5},其中各元素出现次数分别为{2,3,2,1}。

再设计一个函数,将集合中的各元素按出现次数降序排序,最后输出各个元素及其出现次数。例如,上面例子输出结果如下:显示格式为“元素值:出现次数”。

1:3 3:2 2:2 5:1

#include #include #include #include const int n = 30;

int getSet(int rs[],const int a[],int n);

void getSet_improved(int rs[],const int a[],int n);

void create(int a[],int n); void show(int a[],int n); void main() { int rs[n],a[n]; create(a,n); show(a,n); cout<

int getSet(int rs[],const int a[],int n) { int counter = 0; bool flag; for(int i = 0;i < n;i++) { flag = true; for(int j = 0;j < counter;j++) { if(a[i] == rs[j]) flag = false; } if(flag == true) rs[counter++] = a[i]; } return counter; }

void getSet_improved(int rs[],const int a[],int n) { int counter = 0,total[30]; bool flag; for(int i = 0;i < n;i++) total[i] = 0; for(i = 0;i < n;i++)

搜索更多关于: C++程序设计基本编程题型全解 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

} 法二 #include bool IsSmith(long a);//是否是Smith数 int Sum(long a);//个位数之和 void main(void) { } bool IsSmith(long a) { 因数之和 } int Sum(long a) { long a; a=4937774; while(!IsSmith(a)) { a++; } cout<<\大于等于4937774的下一个Smith数是:\long sum_m=0,sum_n=0;//sum_m为各位数之和,sum_n为个质long temp; long t;// temp=a; t=2;//从最小质因数开始 sum_m=Sum(temp)

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com