第一次作业参考程序
以下程序仅供参考,大家可以在此程序基础上进行再次完善:第一题:
/**********************************************************/ //Function : 主函数,从键盘输入三个值,//: 并打印其和、平均数、积、最小值和最大值 //parm : //return : void //Author : dxl //date : 2010.10.14 /**********************************************************/ #include <iostream.h>void main(){int a,b,c;int d;cout<<"Input three different integers:";cin>>a>>b>>c;cout<<"Sum is "<<a + b + c<<endl;//sumcout<<"Average is "<<(a + b + c)/3.0<<endl;//averagecout<<"Product is "<<a * b * c<<endl;//productd=a<b?a:b; cout<<"Smallest is "<<(d < c ? d : c)<<endl;//mind = a > b ? a : b;cout<<"Largest is "<<(d > c ? d : c)<<endl;//max}
第二题:
/**********************************************************/ //Function : main,计算0到10的平方和立方 //parm : //comment : //return : void //Author : dxl //date : 2010.10.14 /**********************************************************/ #include <iostream.h>void main(){cout<<"number\tsquare\tcube"<<endl;for(int i=0;i<=10;i++)cout<<i<<"\t"<<i*i<<"\t"<<i*i*i<<endl;}
第三题:
/**********************************************************/ //Function : main,读取五位整数并确定其是否为回文 //parm : //comment : //return : void //Author : //date : 2010.10.14 /**********************************************************/ #include <iostream.h>void main(){long num;int a,b,d,e;cin>>num;a=num / 10000;//万位b=num % 10000 / 1000;//千位d=num % 100 / 10;//十位e=num % 10; //个位if(a ==e && b==d )cout<<num<<" is palindrome!"<<endl;elsecout<<num<<" is not palindrome!"<<endl;}
第四题:
/**********************************************************/ //Function : main,求出从1626年到2008年24美元的存款本息 //parm : //comment : //return : void //Author : //date : 2010.10.14 /**********************************************************/ #include <iostream.h>#include <math.h>void main(){double amount,principal = 24,rate = 0.072;//amount:本息;principal:本金;rate:利率for(int i = 1;i <= 379;i++) //1626到2008总共379年amount = principal * pow(1.0 + rate, i);cout<<amount<<endl;}
第五题:
/**********************************************************/ //Function : main,信息加密 //parm : //comment : //return : void //Author : //date : 2010.10.14 /**********************************************************/ #include <iostream.h>void main(){int num,a,b,c,d;//信源数据,四位整数cin>>num;a = num / 1000;b = num % 1000 / 100;c = num % 100 / 10;d = num % 10;//以下为加密过程a = (a + 7) % 10;//加7除10求余b = (b + 7) % 10;c = (c + 7) % 10;d = (d + 7) % 10;int temp;temp = a;//1、3位交换a = c;c = temp; //2、4位交换temp = b;b = d;d = temp;cout<<a<<b<<c<<d<<endl;//以下为解密过程a = (a + 3) % 10;//加3除10求余b = (b + 3) % 10;c = (c + 3) % 10;d = (d + 3) % 10;temp = a;//1、3位交换a = c;c = temp;temp = b;//2、4位交换b = d;d = temp;cout<<a<<b<<c<<d<<endl;}
第六题:
/**********************************************************/ //Function : main,判断一对整数中第二个整数是否为第一个整数的倍数 //parm : //comment : //return : void //Author : //date : 2010.10.14 /**********************************************************/ #include <iostream.h>#include <stdlib.h>bool multiple(int x,int y)//Integer parameters function{if(y%x == 0)return true;elsereturn false;}bool multiple(int *x,int *y)//Integer pointer parameters function{if(*y % *x == 0)return true;elsereturn false;}void main(){int a,b;cin>>a>>b;if(a == 0 || b == 0){cout<<"Zero unexpected!"<<endl;exit(0);}if(multiple(a,b))cout<<"True"<<endl;elsecout<<"False"<<endl;if(multiple(&a,&b))cout<<"True"<<endl;elsecout<<"False"<<endl;}
页:
[1]