没有合适的资源?快使用搜索试试~ 我知道了~
C++编程练习题大全(带答案).pdf
2星 需积分: 40 393 下载量 165 浏览量
2020-03-06
18:41:57
上传
评论 59
收藏 2.61MB PDF 举报
温馨提示
试读
409页
409页C++练习题,涵盖了入门习题,提高习题,同时也包含了大量基础算法及一些链表、队列等算法习题,方便在入门C++的同时,对一些算法进行了解。
资源推荐
资源详情
资源评论
409--1
一、简单问题:
5. 编程计算: 1!+2!+3!+ …+20! ,并将结果输出 . 输出格式:
1!+2!+3!+ …+20!= 表达式的值
package moreEasy;
public class The5th {
public static void main(String[] args) {
long sum=0;
for(int i=1;i<=20;i++){
long r=1;
for(int j=1;j<=i;j++){
r*=j;
}
sum=sum+r;
//System.out.print(" r="+r);
if(i<20)
System.out.print(i+"!+");
else
System.out.print(i+"!="+sum);
}
}
}
1. 编程求下列式子的值,
y= 1-1/2+1/3-1/4+ …+1/99-1/100
并将结果输出,格式为 : 1-1/2+1/3-1/4+ …+1/99-1/100 = 表达式的值
public class Porg {
public static void main(String[] args) {
double sum=0;
for ( double i=1;i<=100;i++)
{
sum=sum+Math. pow (-1, i-1)*(1/i);
}
System. out .print(" 1-1/2+1/3- 1/4+ …+1/99 -1/100="+sum);
}
}
2. 请编程实现: 由键盘输入的任意一组字符, 统计其中大写字母的个数 m和小写字母的个数 n,并输出 m、
n 中的较大者。
import java.util.Scanner;
public class Prog2 {
public static void main(String[] args) {
int m=0,n=0;
Scanner cin= new Scanner(System. in );
409--2
String str=cin.nextLine();
for ( int i=0;i<str.length();i++)
{
if (str.charAt(i)>'A'&&str.charAt(i)<='Z')
m++;
else
n++;
}
if (m>n)
{
System. out .println(m);
}
else
System. out .println(n);
}
}
3. 编程,求全部水仙花数。所谓水仙花数是指一个三位数,其各位数字立方的和等于该数。如: 153 = 1
3
+ 5
3
+ 3
3
。
public class Prog3 {
public static void main(String[] args) {
int a,b,c;
for (a=1;a<=9;a++)
{
for (b=0;b<=9;b++)
{
for (c=0;c<=9;c++)
{
if (a*100+b*10+c==a*a*a+b*b*b+c*c*c)
System. out .println(a*100+b*10+c);
}
}
}
}
4. 请编制程序判断输入的正整数是否既是 5 又是 7 的整倍数。若是,则输出 yes ;否则输出 no。
import java.util.Scanner;
public class Prog4 {
public static void main(String[] args) {
Scanner cin= new Scanner(System. in );
int i=cin.nextInt();
if ( i%5==0&&i%7==0)
System. out .print("yes");
409--3
else
System. out .print("no");
}
}
5. 请编程实现:对于给定的一个百分制成绩,输出相应的五分制成绩。设: 90 分以上为‘ A’,80—89 分
为‘ B’,70— 79 分为‘ C’,60—69 分为‘ D’,60 分以下为‘ E’ 。
import java.util.Scanner;
public class Prog5 {
public static void main(String[] args) {
Scanner cin= new Scanner(System. in );
int m=cin.nextInt();
if (m>=60&&m<=69)
System. out .print("D");
else
if (m>=70&&m<=79)
System. out .print("C");
else
if (m>=80&&m<=89)
System. out .print("B");
else
if (m>=90)
System. out .print("A");
else
System. out .print("E");
}
}
6. 输入一行字符,将其中的字母变成其后续的第 3 个字母,输出。例: a→ d,x → a ;y → b ;编程实
现。
import java.util.Scanner;
public class Prog6 {
public static void main(String[] args) {
char [] zimu= new
char []{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','
r','s','t','u','v','w','x','y','z','a','b','c'};
Scanner cin= new Scanner(System. in );
String str=cin.nextLine();
for ( int j=0;j<str.length();j++)
{
for ( int i=0;i<29;i++)
{
if (str.charAt(j)==zimu[i])
{
System. out .println(zimu[i+3]);
409--4
break ;
}
}
}
7. 编写程序,输入任意一个 1~7 之间的整数,将他们转换成对应的英文单词 . 例如: 1 转换成 Monday,7
转换成 Sunday.
import java.util.Scanner;
public class Prog7 {
public static void main(String[] args) {
System. out .print(" 请输入任意一个 1~7 之间的整数 \n");
Scanner cin= new Scanner(System. in );
int m=cin.nextInt();
if (m==1)
System. out .print("Monday");
else
if (m==2)
System. out .print("Tuesday");
else
if (m==3)
System. out .print("Wensday");
else
if (m==4)
System. out .print("Thuesday");
else
if (m==5)
System. out .print("Friday");
else
if (m==6)
System. out .print("Saturday");
else
System. out .print("Sunday");
}
}
8. 输入三角形的三边 a, b, c ,判断能否构成三角形 . 若能,计算面积 .
import java.util.Scanner;
public class Prog8 {
public static void main(String[] args) {
System. out .print(" 请输入三个整数 \n");
Scanner cin= new Scanner(System. in );
int a=cin.nextInt();
int b=cin.nextInt();
int c=cin.nextInt();
if (a+b<c||a+c<b||c+b<a)
409--5
{
System. out .print(" 不能构成三角形 \n");
}
else
{
int l=a+b+c;
System. out .print(l);
}
}
}
9. 编程序,输入 a,b,c ,求方程 ax
2
+bx+c=0 的解。
import java.util.Scanner;
public class Prog9 {
public static void main(String[] args) {
System. out .print(" 请输入三个整数 \n");
Scanner cin= new Scanner(System. in );
int a=cin.nextInt();
int b=cin.nextInt();
int c=cin.nextInt();
if (a==0)
{
System. out .println(-c/b);
}
else if (b*b-4*a*c>=0)
{
double x1=(-b+Math. pow (1/2, b*b-4*a*c))/(2*a);
double x2=(-b-Math. pow (1/2, b*b-4*a*c))/(2*a);
System. out .println(x1);
System. out .print(x2);
}
else System. out .print(" 无解 ");
}
}
10. 计算出前 20 项 fibonacci 数列 , 要求一行打印 5 个数 .
一般而言,兔子在出生两个月后,就有繁殖能力,一对兔子每个月能生出一对小兔子来。如果所有
兔都不死,那么一年以后可以繁殖多少对兔子?
我们不妨拿新出生的一对小兔子分析一下:
第一个月小兔子没有繁殖能力,所以还是一对 ;
两个月后,生下一对小兔总数共有两对 ;
剩余408页未读,继续阅读
资源评论
- Scarlett20252021-04-13个人觉得书中排版不太好,阅读体验不佳
小mu加油
- 粉丝: 51
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- STM32单片机FPGA毕设电路原理论文报告嵌入式系统工程专业单片机课程教学改革与实践
- STM32单片机FPGA毕设电路原理论文报告嵌入式单片机与PC机组合应用
- STM32单片机FPGA毕设电路原理论文报告嵌入式单片机PPP协议的应用研究
- STM32单片机FPGA毕设电路原理论文报告嵌入式USB主机接口在温室环境监控中的应用研究
- STM32单片机FPGA毕设电路原理论文报告嵌入式usb数据采集系统
- 10个LED.pdsprj
- BaiduyunSpider-master.zip
- STM32单片机FPGA毕设电路原理论文报告嵌入式TCPIP协议单片机在网络通信中的应用
- localsend文件传输工具mac端
- STM32单片机FPGA毕设电路原理论文报告嵌入式linux下基于单片机的矩阵键盘与ARM的串行通信
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功