第一次作业参考程序

以下程序仅供参考,大家可以在此程序基础上进行再次完善:

第一题:

/**********************************************************/ //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; //sum cout<<"Average is "<<(a + b + c)/3.0<<endl; //average cout<<"Product is "<<a * b * c<<endl; //product d=a<b?a:b; cout<<"Smallest is "<<(d < c ? d : c)<<endl; //min d = 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; else cout<<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; else return false; } bool multiple(int *x,int *y) //Integer pointer parameters function { if(*y % *x == 0) return true; else return 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; else cout<<"False"<<endl; if(multiple(&a,&b)) cout<<"True"<<endl; else cout<<"False"<<endl; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值