当前位置:编程学习 > C/C++ >>

课后作业,帮忙编写完整的C++程序

课后作业,帮忙编写一个完整的C++程序,“判断376是否是水仙花数”。顺便给我些难度差不多的C++程序的题目,(要10个)带答案,不要雷同。先谢过...
追问:可我关键就是要10个题目 我自己想不出来 这老师很缺德

答案:水仙花束的:

#include <iostream>
using namespace std;
int main()
{
 int n=0;
 int f,s,t;
 cout<<"水仙花数为:\n";
 for(n=100;n<1000;n++)
 {
  f=n/100;
  s=n/10-f*10;
  t=n-f*100-s*10;
  if(f*f*f+s*s*s+t*t*t==n)
   cout<<n<<endl;
 }
 return 0;
}

 

定义一个数组,数据为:6,3,7,1,4,8,2,9,11,5,创建一个向量,把数组赋给该向量,然后求该向量的标准差。

#include<iostream>
#include<vector>
#include<cmath>

using namespace std;

int main()
{
 int a[10]={6,3,7,1,4,8,2,9,11,5};
 int i;;
 double s,r=0,average=0;
 vector<int> x(a,a+10);
 for (i=0;i<10;i++){
  average+=x[i]/10;
     r+=x[i]-pow(average,2);
 }
 s=sqrt(r/10);

 cout<<s<<endl;
}

 

打印99乘法表

#include "stdio.h"
#include "math.h"
int main(void)
{
    int flag, i, j, n;
    int a[6][6];
    int repeat, ri;

    scanf("%d", &repeat);
    for(ri = 1; ri <= repeat; ri++){
        scanf("%d", &n);
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                scanf("%d", &a[i][j]);
         for(i=0;i<n;i++)
    for(j=0;j<i;j++){
     if(a[i][j]!=0)
      flag=0;
    }
        if(flag != 0)  printf("YES\n");
        else  printf("NO\n");
    }
}

编程实现输入一个整数,输出相应的五分制成绩,90分及以上为’A’,80~89为’B’……59分及以下为’E‘

#include <iostream>
#include <ostream>
using namespace std;

int main()
{
 int grade;
 cin>>grade;
 if (grade>=90)
  grade=1;
 else if (grade>=80&&grade<90)
  grade=2;
 else if (grade>=70&&grade<80)
  grade=3;
 else if (grade>=60&&grade<70)
  grade=4;
 else
  grade=5;
 switch(grade)
 {
 case 1:cout<<"A"<<endl;break;
 case 2:cout<<"B"<<endl;break;
 case 3:cout<<"C"<<endl;break;
 case 4:cout<<"D"<<endl;break;
 case 5:cout<<"E"<<endl;break;
 }
 return 0;
}

 

打印整数-1234567的二进制位码

#include<iostream>

using namespace std;

int main()
{
int a=-123456,i;
for(i=31;i>=0;i--)
cout<<(a>>i&1);
cout<<endl;
return 0;
}

 

编程求值,注意不要数据溢出

#include<iostream>

using namespace std;

int main()
{
double factorial(int n);
double result=0;
result=factorial(18)/(factorial(13)*factorial(5));
cout<<result<<endl;
}

double factorial(int n)
{
int i;
double sum=1;

if(n==0)
sum=0;

for(i=1;i<=n;i++)
sum*=i;
return sum;
}

用递归算法实现如下问题:球从100米高空坠落,落地后反弹原高度的一半,求第10次落地后反弹的高度和已经过的距离

#include<iostream>
using namespace std;

int main()
{

 void freedown(int &n,double &rhight,double &distence);
    int n=10;
 double rhight=100;
 double distence=0;

 freedown(n,rhight,distence);
 cout<<"第十次落地后反弹的高度:"<<rhight<<endl<<"经过的距离:"<<distence;
}

void freedown(int &n,double &rhight,double &distence)
{
 
 while(n){

 rhight/=2;
 distence+=3*rhight;

 freedown(--n,rhight,distence);
 }
}

 

实现堆栈和回文判断

#include<iostream> #include<cstdlib>using namespace std;#define MaxSize 100 #define ElemType char typedef struct { ElemType data[MaxSize]; int top; }SeqStack; void StackInitial(SeqStack *pS) { pS->top=-1; } int IsEmpty(SeqStack *pS) { return pS->top==-1; } int IsFull(SeqStack *pS) { return pS->top>=MaxSize-1; } void Push(SeqStack *pS,ElemType e) { if(IsFull(pS)) { cout<<"Full"; exit(1); } pS->data[++pS->top]=e; } ElemType Pop(SeqStack *pS) { if(IsEmpty(pS)) { cout<<"Empty"; exit(1); } return pS->data[pS->top--]; } ElemType GetTop(SeqStack *pS) { if(IsEmpty(pS)) { cout<<"Empty"; exit(1); } return pS->data[pS->top]; } void MakeEmpty(SeqStack *pS) { pS->top=-1; } void main() {ElemType d1[100],ch; int i=0,j=0;//,a; /////////////////SeqStack stack; StackInitial(&stack); cout<<"Please input a character string\n"; cin>>d1; while((ch=d1[i++])!='\0') { Push(&stack,ch); } while(!IsEmpty(&stack)) { if(Pop(&stack)!=d1[j++])/////////////;cout<<"no"; } cout<<"yes"; }

 

编程实现复数类和实数类,实数类以复数类的子类来实现。复数类要求实现操作符重载,

#include<iostream>#include<cstdlib>#include<cmath>using namespace std;class complex{private: double re; double im;public:complex(){} complex(double a,double b){re=a;im=b;} void set(double a,double b){re=a;im=b;} double geti() {return im; } double getr() {return re; } complex operator +=(complex &a){ this->re+=a.re; this->im+=a.im; return *this; } complex operator -=(complex &a){ this->re-=a.re; this->im-=a.im; return *this; } complex operator *=(complex &a){ re =re*a.re-im*a.im; im =re*a.im+a.re*im; } complex operator /=(complex &a) { if(a.re||a.im) { re =(re*a.re+im*a.im)/(a.re*a.re+a.im*a.im); im =(re*a.im-a.re*im)/(a.re*a.re+a.im*a.im); } } friend ostream& operator << (ostream& os, complex& c); friend istream& operator >> (istream& is, complex& c);friend complex operator +(complex &a, complex &b); friend complex operator -(complex &a); friend complex operator -(complex &a, complex &b); friend complex operator *(complex &a, complex &b); friend complex operator /(complex &a, complex &b); friend int operator ==(complex &a, complex &b); friend int operator !=(complex &a, complex &b);};complex operator +(complex &a, complex &b){ complex temp; temp.re = a.re + b.re; temp.im = a.im + b.im; return temp;}complex operator -(complex &a){a.re=-a.re;a.im=-a.im;return a;}complex operator -( complex &a, complex &b ){ complex temp; temp.re = a.re - b.re; temp.im = a.im - b.im; return temp;}complex operator *(complex &a, complex &b){complex temp;temp.re = a.re * b.re - a.im * b.im;temp.im = a.re * b.im + a.im * b.re;return temp;}complex operator /(complex &a, complex &b){complex temp;temp.re = (a.re * b.re + a.im * b.im) / (b.re * b.re + b.im * b.im);temp.im = (a.im * b.re - a.re * b.im) / (b.re * b.re + b.im * b.im);return temp;} int operator ==(complex &a, complex &b){return (a.re == b.re && a.im == b.im);}int operator !=(complex &a, complex &b){return !(a==b);} ostream& operator << (ostream& os, complex& c) { os << c.re; if(c.im > 0) os << "+" << c.im &

上一个:用C++编一个身份证15位升18位的程序
下一个:帮忙写个C++程序。。关于时间类的。。

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,