ꫛꪺꫀr᭙ꪁrᦔડ314 2021-06-10 12:23 采纳率: 100%
浏览 22
已结题

C++考查课试题,急用,谢谢

 

  • 写回答

3条回答 默认 最新

  • qfl_sdu 2021-06-10 14:09
    关注

    题目1:

    #include <iostream>
    using namespace std;
    int main()
    {
    	int i,n,sum = 0;;
    	cout << "请输入n:";
    	cin >> n;
    	for ( i = 1;i <=n; i++)
    	{
    		sum += i;
    	}
    	cout << " 1 + 2 + ...+ " << n << " = " << sum;
    	return 0;
    }

    题目2:

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	int zm = 0,nmb = 0;
    	string ss;
    	cout << "请输入字符串:";
    	cin >> ss;
    	for (int i = 0; i < ss.length(); i++)
    	{
    		if (ss.at(i) >= '0' && ss.at(i) <= '9')
    		{
    			nmb++;
    		}else if (ss.at(i) >= 'a' && ss.at(i) <= 'z' || ss.at(i) >= 'A' && ss.at(i) <= 'Z')
    		{
    			zm++;
    		}
    	}
    	cout << "字母字符:" << zm << ";数字字符:" <<nmb;
    	return 0;
    }

    题目3:

    #include <iostream>
    using namespace std;
    
    double fun(int n)
    {
    	int i;
    	int* a = new int[n];
    	int* b = new int[n];
    	a[0] = 1;
    	a[1] = 2;
    	b[0] = 2;
    	b[1] = 3;
    	double sum = b[0]/a[0] + b[1]/b[1];
    	for (i = 2; i < n; i++)
    	{
    		a[i] = a[i-1] + a[i-2];
    		b[i] = b[i-1] + b[i-2];
    		sum += 1.0 * b[i]/a[i];
    	}
    	return sum;
    }
    int main()
    {
    	int n;;
    	cout << "请输入n:";
    	cin >> n;
    	
    	cout << "前" << n << "项的和 = " << fun(n);
    	return 0;
    }

    题目4:

    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	float x,y;
    	
    	cout << "请输入x和y:";
    	cin >> x >> y;
    	float fu;
    	if (x >= 0)
    	{
    		fu = 2 * x + y * y;
    	}else
    		fu = 2 * x + y;
    
    	cout << "值:" << setiosflags(ios::fixed)<<setprecision(2)<< fu <<endl;
    	return 0;
    }

    题目5:

    #include <iostream>
    #include <string>
    using namespace std;
    
    bool issxh(int n)
    {
    	int dd = n;
    	int a = n%10;
    	n = n/10;
    	int b = n%10;
    	n=n/10;
    	int c = n%10;
    	int ss = a*a*a + b*b*b + c*c*c;
    	if (ss == dd)
    	{
    		return true;
    	}
    	return false;
    
    }
    
    int main()
    {
    	for (int i = 100; i < 999; i++)
    	{
    		if(issxh(i))
    			cout << i << " ";
    	}
    
    	cout << endl;
    	return 0;
    }

    题目6:

    #include <iostream>
    #include <string>
    using namespace std;
    int prime(int n)
    {
    	int i =2;
    	for(i = 2; i<n; i++)
    		if(n%i == 0)
    			return 0;
    	return 1;
    }
    
    int main()
    {
    	int a[20]={0};
    	int b[20] = {0};
    	int nmb = 0;
    	cout << "请输入20个正整数:";
    	for (int i = 0; i < 20; i++)
    	{
    		cin >> a[i];
    		if(prime(a[i]))
    			b[nmb++] = a[i];
    	}
    	
    	for (int i=0; i < nmb-1; i++)
    	{
    		for (int j = 0; j < nmb -1-i;j++)
    		{
    			if (b[j] < b[j+1])
    			{
    				int tt = b[j];
    				b[j] = b[j+1];
    				b[j+1] = tt;
    			}
    		}
    	}
    	cout << "所有素数降序输出:"<< endl;
    	for(int i = 0; i < nmb;i++)
    		cout << b[i] << " ";
    	cout << endl;
    	return 0;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化