C++上机考试试题解析
C++上机考试试题解析
0x1 求日期是该年的第几天
输入日期(年、月、日),输出它是该年的第几天。
Input year, month, day:1981 3 1
判断闰年方法:a%4==0&&a%100!=0||a%400==0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<iostream> using namespace std; int main() { cout<<"Input year, month, day:"; int a,b,c,temp=0; int days,i; cin>>a; cin>>b; cin>>c; int month[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; if(a%4==0&&a%100!=0||a%400==0) month[1]=29; for(i=0; i<b-1; i++) temp+=month[i]; days=temp+c; cout<<"Days of year:"<<days<<endl; return 0; } |
0x2 判断输入字符个数
输入10 个字符,统计其中英文字母、空格、数字字符和其他字符的个数。
通过ASCII码判别输入的字符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <iostream> #include <cstdio> using namespace std; int main() { int uppercase=0; int lowercase=0; int digits=0; int spacebars=0; int other=0; int i=0; char *p; char s[100]; cout<<"Input a string:"; while((s[i]=getchar())!='\n') { i++; } p=&s[0]; while(*p!='\n') { if(('A'<=*p)&&(*p<='Z')) { ++uppercase; } else if(('a'<=*p)&&(*p<='z')) { ++lowercase; } else if(*p==' ') { ++spacebars; } else if((*p<='9')&&(*p>='0')) { ++digits; } else { ++other; } p++; } cout<<"Number of uppercase letters is :"<<uppercase<<endl; cout<<"Number of lowercase letters is :"<<lowercase<<endl; cout<<"Number of spacebars is :"<<spacebars<<endl; cout<<"Number of digits is :"<<digits<<endl; cout<<"Number of other characters is :"<<other<<endl; return 0; } |
0x3 顺序循环移动
有n个整数,使其中所有整数顺序向右(向后)循环移动m个位置(m<n)。编写一个函数实现以上功能,在主函数中输入n个整数并输出调整后的n个整数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include<stdio.h> void mov(int *,int,int); int main(void) { int m,n,i,a[80],*p; printf("Input n,m:"); scanf("%d%d",&n,&m); for(p =a,i =0; i <n; i ++) scanf("%d",p++); mov(a,n,m); printf("After move:"); for(i =0; i <n; i ++) printf("%5d",a[i]); return 0; } void mov(int*x,int n,int m) { int i,j,k; for(i =0; i <m; i ++) { k=x[n-1]; for(j =n -1; j >0; j --) x[j] =x[j -1]; x[0] =k; } } |
0x4 换算一笔钱
将一笔钱(大于8分,小于1元,精确到分)换算成1分、2分和5分的硬币组合。输入金额,问有几种换算方法?要求每种硬币至少有一枚。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> using namespace std; int main() { int n; int x,y,z;//x是1分,y是2分,z是5分(硬币个数) int num=0;//用于保存有多少种方法 cout<<"Input money: "; cin>>n; for(x=1;x<=n;x++) for(y=1;y<=n/2;y++) for(z=1;z<=n/5;z++) { if(x+2*y+5*z==n) { cout<<"1分"<<x<<"个,2分"<<y<<"个,5分"<<z<<"个n"; num++; } } cout<<"一共有"<<num<<"种方法"<<endl; } |
0x5 加密一个四位正整数
输入一个四位正整数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10 取余,作为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; int main() { int m; int a,b,c,d; cout<<"Enter a number:"; cin>>m; a=(m/1000+9)%10; b=(m%1000/100+9)%10; c=(m%100/10+9)%10; d=(m%10+9)%10; cout<<"The encrypted number is "<<c<<d<<a<<b<<endl; return 0; } |
0x6 同构数
同构数是其平方数的尾数等于该数自身的自然数,例如:25×25=625。从键盘输入一个不大于10000的自然数,判断其是否是同构数并按样例格式输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> using namespace std; int main() { int a; cin>>a; int b=a*a-a; int c=a,d=b; while((c=c/10)!=0) { if(d%10!=0) { cout<<a<<" no,"<<a<<"*"<<a<<"="<<a*a; return 0; } d=d/10; } if(d%10!=0) cout<<a<<" no,"<<a<<"*"<<a<<"="<<a*a; else cout<<a<<" yes,"<<a<<"*"<<a<<"="<<a*a; return 0; } |
0x7 奇数为取出
将一个整数中的每一位上为奇数的数依次取出,构成一个新数并输出。高位仍在高位,低位仍在低位。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> using namespace std; void function(int a) { if(a==0) return; function(a/10); if(a%2!=0&&a>0) cout<<a%10; else if(a%2!=0&&a<0) cout<<-a%10; } int main() { int a; cin>>a; if(a>0) function(a); else { cout<<"-"; function(a); } } |
0x8 交换最大、最小值
输入一个正整数n(1 <n≤10),再输入n 个整数,将最小值与第一个数交换,最大值与最后一个数交换,然后输出交换后的n 个数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <iostream> using namespace std; int main() { int i,n; int a[10]; cout<<"Input n: "; cin>>n; cout<<"Input "<<n<<" integers: ";; for(i=0; i<n; i++) { cin>>a[i]; } int s,p,j; s=a[0]; p=0; for(j=1; j<n; j++) { if(s<a[j]) { s=a[j]; p=j; } } a[p]=a[n-1]; a[n-1]=s; s=a[0]; p=0; for(j=1; j<n; j++) { if(s>a[j]) { s=a[j]; p=j; } } a[p]=a[0]; a[0]=s; cout<<"After swapped: "; for(i=0; i<n; i++) { cout<<a[i]<<" "; } return 0; } |
0x9 阶乘之和
输入2 个正整数m 和n,计算m!+n!。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> using namespace std; int fact(int n) { int i,t=1; for(i=1; i<=n; i++) { t*=i; } return t; } int main() { int m,n; cout<<"Enter m:"; cin>>m; cout<<"Enter n:"; cin>>n; cout<<m<<"!+"<<n<<"!="<<fact(m)+fact(n); return 0; } |
0x10 阶乘之和
输入2个正整数m和n(m<=n),求(i=m—>n)(1/i)累加和,输出时保留3位小数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> #include <iomanip> using namespace std; double fact(int n,int m) { double i,t=0.0; for(i=m; i<=n; i++) { t+=(1/i); } return t; } int main() { int m,n; double s; cout<<"Enter m:"; cin>>m; cout<<"Enter n:"; cin>>n; s=fact(n,m); cout<<"sum="<<fixed<<setprecision(3)<<s; return 0; } |
0x11 合并字符串(gets有问题)
从键盘输入两个有序字符串(其中字符按ASCII码从小到大排序,并且不含重复字符),将两字符串合并,要求合并后的字符串仍是有序的,并且重复字符只出现一次,最后输出合并后的结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <stdio.h> #include <cstring> using namespace std; int main() { char a[100],b[100],c[100]; int i=0,j=0,k; gets(a); gets(b); for(i=0,j=0,k=0; a[i]!=0&&b[j]!=0;) { if(a[i]>b[j]) { c[k]=b[j]; j++; } else { c[k]=a[i]; i++; } k++; } if(a[i]=='\0') { for(; b[j]!=0; j++,k++) c[k]=b[j]; } else { for(; a[i]!=0; i++,k++) c[k]=a[i]; } c[k]='\0'; printf("%s",c); printf("\n"); return 0; } |
0x12 存款到期利息
输入存款金额money、存期year和年利率rate,根据下列公式计算存款到期时的利息interest(税前),输出时保留2位小数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int money,year; double rate,interest; cout<<"Enter money,year and rate: "; cin>>money>>year>>rate; interest=money*pow((1+rate),year)-money; cout << "interest=" <<fixed<<setprecision(2)<<interest<<endl; return 0; } |
0x13 两组整数的交集
从标准输入中输入两组整数(每组不超过20个整数,每组整数中的元素不重复,并且整数大于等于0),编程求两组整数的交集,即在两组整数中都出现的整数,并按从小到大顺序排序输出。若交集为空,则什么都不输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include <iostream> using namespace std; int main() { int a[1000]; int b[1000];int o=0,d=0,m=0; int c[1000]; for(int i=0;;i++) { cin>>a[i];o++; if(a[i]==-1) break; } for(int i=0;;i++) { cin>>b[i];d++; if(b[i]==-1) break; } for(int i=0;i<o-1;i++) { for(int n=0;n<d-12;n++) if(a[i]==b[n]) { c[m]=a[i]; m++; } } for(int i=0;i<m;i++) { for(int h=i;h<m;h++) { if(c[i]>c[h]) { int k=c[i]; c[i]=c[h]; c[h]=k; } } } for(int i=0;i<m;i++) cout<<c[i]<<" "; return 0; } |
求两组整数的异或集和交集。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#include <stdio.h> #include <stdlib.h> int main() { int num1[20], num2[20], num3[20], num4[20]; int i,j,j1=0,j2=0,k1=0,k2=0,tmp,flag=0; char a=0; while(a!= '\n') { scanf("%d",&num1[j1++]); a= getchar(); } a=0; while(a != '\n') { scanf("%d", &num2[j2++]); a= getchar(); } for (i = 0; i <j1; i++) { for (j = 0; j<j2; j++) { if (num1[i] == num2[j]) { flag=1; } } if(flag) { num3[k1++]=num1[i]; } else { num4[k2++]=num1[i]; } flag=0; } for (i = 0; i <j2; i++) { for (j = 0; j <j1; j++) { if (num2[i] == num1[j]) { flag=1; } } if(!flag) { num4[k2++]=num2[i]; } flag=0; } for (i = 0; i < (k2-1); i++) { for (j = i+1; j < k2; j++) { if (num4[i] > num4[j]) { tmp = num4[i]; num4[i] = num4[j]; num4[j] = tmp; } } } for (i = 0; i < k2; i++) { printf("%d ", num4[i]); } printf ("\n"); for (i = 0; i < (k1-1); i++) { for (j = i+1; j < k1; j++) { if (num3[i] > num3[j]) { tmp = num3[i]; num3[i] = num3[j]; num3[j] = tmp; } } } for (i = 0; i < k1; i++) { printf("%d ", num3[i]); } return 0; } |
0x14 阶乘和数(正序)
一个正整数如果等于组成它的各位数字的阶乘之和,则该正整数称为阶乘和数。例如正整数145,1!+4!+5!等于145,因此145就是一个阶乘和数。输入一个正整数,计算它的各位数字的阶乘之和,并判断它是否是一个阶乘和数。注意:输入的正整数,以及组成它的各位数字的阶乘之和都不会超过int类型的表示范围,并且输入的正整数的最高位不为0。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char str[10]; int i, j, sum = 0, num[10], tmp = 0; gets(str); printf("%s,", str); for (i = 0; i<strlen(str); i++) { num[i] = str[i] - '0'; } for (i = strlen(str) - 1; i >= 0; i--) { j = 1; if (num[i]>1) { while (num[i]--) { j = j * (num[i] + 1); } sum = sum + j; } else { sum = sum + 1; } if (i == 0) { printf("%d=%d\n", sum - tmp, sum); tmp = atoi(str); if (tmp == sum) { puts("Yes"); } else { puts("No"); } } else { printf("%d+", sum - tmp); } tmp = sum; } return 0; } |
0x15 求符合要求的自然数对
编程输出符合如下要求的自然数对:它们的和为667,它们的最小公倍数除以最大公约数的商为120。输出格式为每对一行,小数在前,大数在后,两数间用逗号分隔,多对则按每对中小数的大小升序排列先后。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> #include <cmath> using namespace std; //求最大公约数。 int func(int a,int b) { int i,j,c; if(a>b) i=a,j=b; else i=b,j=a; c=i%j; while(c!=0) { i=j; j=c; c=i%j; } return 0; } int main() { int x,y; int a,b; for(x=1; x<667; x++) { y=667-x; a=func(x,y); b = x*y/a; if(fabs(((double)b)/a-120) < 1e-9) { printf("%d %d\n",x,y); } } return 0; } |
0x16 数组的均值
输入1个正整数n (1<n≤10),再输入n个整数,输出平均值(保留2位小数)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <iomanip> using namespace std; int main() { int a[10]; int n,sum=0; cin>>n; for(int i=0; i<n; i++) { cin>>a[i]; sum+=a[i]; } double avg; avg=(float)sum/n; cout<<fixed<<setprecision(2)<<avg<<endl; return 0; } |
0x17 打印数字菱形图案
编程打印用数字构成的菱形图案,菱形上半部分的行数n( 1<n<10 )从键盘输入,总行数为2n-1。图案的样式按下面的样例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> using namespace std; int main() { int i,j; int n; cin>>n; for(i=0; i<n; i++) { for(j=0; j<2*n-2*i; j++) cout<<" "; for(j=1; j<=i+1; j++) cout<<j<<" "; for(j=j-2; j>0; --j) cout<<j<<" "; cout<<endl; } for(i=n-1; i>0; i--) { for(j=0; j<=2*n-2*i+1; j++) cout<<" "; for(j=1; j<i+1; j++) cout<<j<<" "; for(j=j-2; j>0; --j) cout<<j<<" "; cout<<endl; } return 0; } |
0x18 报数3的人退出圈子
有n个人围成一圈,按顺序从1到n编号。从第一个人开始报数,报数3的人退出圈子,下一个人从1开始重新报数,报数3的人退出圈子。如此循环,直到留下最后一个人。问留下来的人的编号。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include<stdio.h> #include<stdlib.h> int main() { int i=0,j=0; int a[10000]= {0}; int n; printf("Input n(n must be a natural number less than 10000):"); scanf("%d",&n); if(n>10000||n<=0) { printf("%d is out of range of valid values.\n",n); exit(1); } for(j=0; j<n; j++) a[j]=1; int k=0; int sum=0; do { k=k+a[i%n]; if(k==3) { a[i%n]=0; k=0; sum=0; } i++; for(j=0; j<n; j++) sum=sum+a[j]; } while(sum!=0); printf("Last No. is:%d\n",(i-1)%n+1); return 0; } |
0x19 计算旅途时间
输入2个整数time1和time2,表示火车的出发时间和到达时间,计算并输出旅途时间。有效的时间范围是0000 ~2359(前两位表示小时,后两位表示分钟),不需要考虑出发时间晚于到达时间的情况。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; int main() { int a,b,c,d,e,f; cout<<"Enter time1:"; cin>>a; cout<<"Enter time2:"; cin>>b; c=a/100; d=b/100; e=a%100; f=b%100; if(f>=e) cout<<"The train journey time is "<<d-c<<" hours "<<f-e<<" minutes"<<endl; else cout<<"The train journey time is "<<d-c-1<<" hours "<<f+60-e<<" minutes"<<endl; return 0; } |
0x20 打印W形图案
从键盘上输入n(1<n<21),按样例格式打印n行由字符 * 构成的W形图案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> int main() { int n=0; int i=0; scanf( "%d", &n ); for( i=0; i<n; i++ ) { int width=0 ; int j=0 ; width=2*(n-i-1)-1 ; for ( j=0; j<i; j++ ) printf(" " ); printf("*"); //左边 for ( j=0; j<width; j++ ) printf(" " ); //两*间距 if ( i < n-1 ) printf("*"); //中间* for ( j=0; j<2*i-1; j++ ) //中间两*间距 printf(" " ); if ( i> 0 && i< n-1 ) //非头尾,有四* printf("*"); for ( j=0; j<width; j++ ) printf(" " ); printf("*"); //右边 printf("\n"); } return 0; } |
0x21 求平均分
编写程序,依次输入某位学生的数学、英语和计算机课程的成绩,计算并输出该生3门课程的平均分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> int main() { int math, eng, comp; float average; printf("math="); scanf("%d", &math); printf("eng="); scanf("%d", &eng); printf("comp="); scanf("%d", &comp); average = (math + eng + comp) / 3.0; printf("average=%.2f\n", average); } |
0x22 前一秒和后一秒
从键盘输入24小时制的某个时间,计算并输出其前一秒和后一秒的时间。输入格式为:hh:mm:ss。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <stdio.h> int main() { char time[8]; int h=0,m=0,s=0; scanf("%s",time); h=(time[0]-'0') * 10 + time[1]-'0'; m=(time[3]-'0') * 10 + time[4]-'0'; s=(time[6]-'0') * 10 + time[7]-'0'; int h1,h2,m1,m2,s1,s2; int flag=0; s1 = (s+60-1)%60; if(s-1<0) { flag=1; } m1 = (m+60-flag)%60; flag=0; if(m-1<0) { flag=1; } h1 = (h+24-flag)%24; printf("%02d:%02d:%02d\n",h1,m1,s1); s2 = (s+1)%60; flag =0; if(s+1>59) { flag=1; } m2 = (m+flag)%60; flag=0; if(m+1>59) { flag=1; } h2 = (h+flag)%24; printf("%02d:%02d:%02d",h2,m2,s2); } |
0x23 闰年、季节和月天数
输入年份和月份,判断输出该年是否是闰年,并根据月份判断输出是什么季节和该月有多少天。
闰年(Leap year)的条件是符合下面两条件之一:
⑴ 年份能被 4 整除,但不能被 100 整除;
⑵ 年份能被 400 整除。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> #include <stdlib.h> int main() { int y,m,leap; scanf("%d %d",&y,&m); leap=(y%4==0 && y%100!=0)||(y%400==0); printf(leap?"Leap year,":"Common year,"); if(m/3==1) printf("Spring,"); else if(m/3==2) printf("Summer,"); else if(m/3==3) printf("Fall,"); else printf("Winter,"); if(m==2) printf("%d\n",28+leap); else if(m==4||m==6||m==9||m==11) printf("%d",30); else printf("%d",31); printf("\n"); } |
0x24 三角形周长和面积
输入三角形的三边a,b,c,如果能构成一个三角形,输出面积area
和周长perimeter(保留两位小数);否则,输出
These sides do not correspond to a valid triangle
在一个三角形钟,任意两边之和大于第三边。三角形面积 计算公式:
area=sqrt(s(s-a)(s-b)*(s-c))
其中:s=(a+b+c)/2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include<iomanip> #include<cmath> using namespace std; int main() { double a,b,c,s; cout<<"Enter 3 sides of the triangle: "; cin>>a; cin>>b; cin>>c; s=(a+b+c)/2; if(a+b>c&&a+c>b&&b+c>a) cout<<"area="<<fixed<<setprecision(2)<<sqrt(s*(s-a)*(s-b)*(s-c))<<"; perimeter="<<a+b+c; else cout<<"These sides do not correspond to a valid triangle"; return 0; } |
0x25 皮球落地
皮球从height米的高度自由落下,触地后反弹到原高度的一半,再落下,再反弹,如此反复。皮球在第n次落地时,在空中经过的路程是多少米?第n次反弹的高度是多少?(输出保留1位小数)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> int main() { int n,cnt=0; double distence,h; scanf("%lf %d", &h, &n); distence = h; cnt=1; do { h=0.5*h;//第一次落地弹起距离 distence += 2*h;//第二次落地空间划过的距离 cnt++; } while(cnt<n); //循环结束时,此时distence是第n次落地空间划过的距离,但是h是第n-1次落地弹起的距离 h=0.5*h;//第n次落地弹起的距离 printf("distence = %.1f, h = %.1f\n", distence, h); return 0; } |
0x26 n个a之和
输入2个正整数a和n,求a+aa+aaa+aa…a(n个a)之和。例如,输入2和3,输出246(2+22+222)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; int main() { int a,n,s=0; cout<<"Input a, n: "; cin>>a>>n; while(n>=0) { s=a*n+s; a=a*10; n--; } cout<<"s="<<s<< endl; return 0; } |
0x27 分类统计字符个数
输入一行文字,统计其中的大写字母、小写字母、空格、数字以及其他字符的个数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> using namespace std; int main() { char c; int up=0,lo=0,sp=0,nu=0,other=0; cout<<"Input a string:"; while(cin.get(c)) { if(c=='\n') break; if(c>='A' && c<='Z') up++; else if(c>='a' && c<='z') lo++; else if(c>='0'&&c<='9') nu++; else if(c==' ') sp++; else other++; } cout<<"Number of uppercase letters is :"<<up<<endl; cout<<"Number of lowercase letters is :"<<lo<<endl; cout<<"Number of spacebars is :"<<sp<<endl; cout<<"Number of digits is :"<<nu<<endl; cout<<"Number of other characters is :"<<other<<endl; return 0; } |
0x28 用对应字母替换字符串中的大写字母
输入一个以回车结束的字符串(少于80 个字符),将其中的大写字母用下面列出的对应大写字母替换,其余字符不变,输出替换后的字符串。
原字母 对应字母
A → Z
B → Y
C → X
D → W
…
X → C
Y → B
Z → A
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <cstdio> using namespace std; int main() { char s[81],c; int i=0; cout<<"Input a string:"; gets(s); cout<<"After replaced:"; while((c=s[i++])!='\0') { if (c>='A' && c<='Z') c='Z'-c+'A'; cout<<c; } cout<<endl; return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<iostream> #include <stdio.h> using namespace std; int main() { int i; char str[80]; cout<<"Input a string:"; i=0; while((str[i]=getchar())!='\n') i++; str[i]='\0'; cout<<"After replaced:"; for(i=0; str[i]!='\0'; i++) { if(str[i]>='A'&&str[i]<='Z') str[i]='A'+'Z'-str[i]; putchar(str[i]); } return 0; } |
0x29 温度转换
输入华氏温度,输出对应摄氏温度,输入输出都是整数。计算公式如下,其中:c 表示摄氏温度,f 表示华氏温度
c = 5×(f-32)/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> #include<stdlib.h> main() { float c,a=5,b=9,f; scanf("%f",&f); c=(a/b)*(f-32); printf("%f",c); } |
0x30 递增或递减输出
输入整数m和n,输出从m至n的所有数,根据m和n的大小数字递增或递减。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include<iostream> using namespace std; int main() { int m,n; cin>>m>>n; if(m>n) { for(int i=m; i>=n; i--) cout<<i<<endl; } else for(int i=m; i<=n; i++) cout<<i<<endl; return 0; } |
0x31 分数化简
编写程序,从控制台读入一个分数的分子和分母(分数无符号,并且分子小于分母,其大小不会超过int数据类型的表示范围),输出化简后分子和分母不含公约数的分数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include<iostream> using namespace std; int func(int a,int b) { int i,j,c; if(a>b) i=a,j=b; else i=b,j=a; c=i%j; while(c!=0) { i=j; j=c; c=i%j; } return 0; } int main() { int m,n; int a,b,c; cin>>m>>n; c=func(m,n); a=m/c;b=n/c; cout<<a<<" "<<b; } |
##0x32 分数化简
编写程序,从控制台读入一个分数的分子和分母(分数无符号,并且分子小于分母,其大小不会超过int数据类型的表示范围),输出化简后分子和分母不含公约数的分数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> using namespace std; //求最大公约数 int gcd(int n,int m) { int temp,r; //把大的数放在n里面 if(n<m) { temp=n; n=m; m=temp; } while(m!=0) { r=n%m; n=m; m=r; } return n; } int main() { int a,b;//a是分子 b是分母 cout<<"please input a and b:"; cin>>a>>b; cout<<a/gcd(a,b)<<"/"<<b/gcd(a,b); return 0; } |
##0x32 输出自然数和
计算并输出k以内最大的10个能被13或17整除的自然数之和。k的值从键盘输入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> using namespace std; int main() { int k; cin>>k; int a[10]= {0}; int j=0; int sum=0; for(int i=k; i>=0; i--) { if(i%13==0||i%17==0) { if(j>9) { break; } a[j]=i; j++; } } for(int m=0; m<=9; m++) { sum=sum+a[m]; } cout<<sum<<endl; return 0; } |
##0x33 输出实数和
已知一个数列的前三项分别为0,0,1, 以后的各项都是其相邻的前三项之和,计算并输出该数列前n项的平方根之和sum,默认6位小数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int a1=0; int a2=0; int a3=1; int a4; double sum=1; int n; cin>>n; for(int i=4; i<=n; i++) { a4=a1+a2+a3; sum=sum+sqrt(a4); a1=a2; a2=a3; a3=a4; } cout<<fixed<<setprecision(6)<<sum<<endl; return 0; } |
0x34 判断是否互质
判断两个整数m和n是否互质(即是否有公共的因子)(m≠1,n≠1)。方法是: 用2到t(t取m和n中较小的那个数)之间的数分别去除m和n,若m和n能同时被某个数除尽,则m和n不互质;否则它们互质。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> using namespace std; int main() { int m; int n; cin>>m>>n; int t=m>n?n:m; int i=2; for (i; i<=t; i++) { if(i<t&&m%i==0&&n%i==0) { cout<<"NO"<<endl; break; } if(i==t) { cout<<"YES"<<endl; } } return 0; } |
##0x35 求出素数
从键盘输入一个大于2的正整数n,求解并输出大小最接近n的素数(不包括n)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> #include <cmath> using namespace std; //判断素数 int isprime(int x) { int i; if(x<2) return 0; for(i=2; i<sqrt(x); i++) if(x%i==0) return 0; return 1; } int main() { int i,a,b,n; cin>>n; for(a=n+1; !isprime(a); a++); //不是素数就跳过,是素数就结束循环。 for(b=n-1; !isprime(b); b--); if(a-n<n-b) cout<<a<<endl; else if(a-n==n-b) cout<<a<<" "<<b<<endl; else cout<<b<<endl; return 0; } |
0x36 求公式近似值
已知ex的近似值可由下面公式计算得出:
ex=1 + x/1! + x2/2! + x3/3! + …… + xn/n!
给定x和n,利用上述公式求得ex的近似值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <iomanip> using namespace std; int main() { double ex=1; double sum=1; double x; int n; cin>>x>>n; for(int i=1; i<=n; i++) { sum=sum*x/i; ex=ex+sum; } cout<<fixed<<setprecision(6)<<ex<<endl; return 0; } |
##0x37 转换整数
编写一个程序,当用户输入一个小数(正值浮点数)后,将小数转化为最近的整数输出(四舍五入)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> using namespace std; int main() { double a; int b; while (cin>>a) { b=a+0.5; cout <<b<<endl; } return 0; } |
##0x38 正整数的打印
给出一个不多于5位的正整数,要求:
1.求出它是几位数。
2.分别打印出每一位数字。
3.按照逆序打印出每一位数字。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <cmath> using namespace std; int main() { int a; cin>>a; int b; int n=0; b=log10(a)+1; cout<<b<<endl; cout<<a<<endl; while (a) { n=n*10+a%10; a=a/10; } cout <<n<<endl; return 0; } |
##0x39 找最大最小整数
编写一个程序,用户输入若干整数,试找出其中的最大数和最小数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> using namespace std; int main() { int m; int n; cin>>m; int nmax=1000000; int nmin=0; for(int i=1; i<=m; i++) { cin>>n; if(n<nmax) { nmax=n; } if(n>nmin) { nmin=n; } } cout<<nmin<<" "<<nmax<<endl; return 0; } |
##0x40 求A,B
输入三位数字N,求两位数AB(其中个位数字为B,十位数字为A,且有0 < A < B <= 9)。使得下列等式成立:
AB x BA = N
其中BA是把AB中个、十位数字交换所得的两位数。
编写程序,接收控制台输入的三位整数N,求解A,B并输出。
如果没有解则输出 No Answer。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> using namespace std; int main() { int N; cin>>N; int A; int B; A=N/10%10;//十位 B=N%10;//个位 int a; a=A*10+B; int b; b=B*10+A; for(int i=1; i<=9; i++) //b { for(int j=1; j<i; j++) { a=j*10+i; b=i*10+j; if(a*b==N) { cout<<a<<endl; break; } else { continue; } }//a } return 0; } |
##0x41 回文数
控制台输入两个整数a和b(必有a<b),以空格分隔。
输出有若干行,每行有一个a和b之间的回文数。输出各行上的数字不重复,且从小至大依次按序输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
#include <iostream> using namespace std; int main() { int a; int b; cin>>a>>b; for(int i=a; i<=b; i++) { if(i<10) { cout<<i<<endl; } if(10<=i&&i<100) { int x=0; int y=i; while(y) { x=x*10+y%10; y=y/10; } if(x==i) { cout<<x<<endl; } } if(100<=i&&i<1000) { int x=0; int y=i; while(y) { x=x*10+y%10; y=y/10; } if(x==i) { cout<<x<<endl; } } if(1000<=i&&i<10000) { int x=0; int y=i; while(y) { x=x*10+y%10; y=y/10; } if(x==i) { cout<<x<<endl; } } if(10000<=i&&i<100000) { int x=0; int y=i; while(y) { x=x*10+y%10; y=y/10; } if(x==i) { cout<<x<<endl; } } } return 0; } |
##0x42 计算平均值
从键盘输入三个整数,分别存入x,y,z三个整型变量中,计算并输出三个数的和以及平均值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <iomanip> using namespace std; int main() { int x,y,z; cin>>x>>y>>z; double sum; double avg; sum=x+y+z; avg=sum/3; cout<<sum<<endl; cout<<fixed<<setprecision(2)<<avg<<endl; return 0; } |
##0x43 求和
编写一个程序,求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)。
输入一个正整数n,根据求s公式计算s并输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; int main() { int a; cin>>a; int sum=0; for(int i=1; i<=a; i++) { sum=i*(a+1-i)+sum; } cout<<sum; return 0; } |
##0x44 前驱、后继字符
从键盘输入一个字符,求出它的前驱和后继字符(按照ASCII码值排序),并按照从小到大的顺序输出这三个字符和对应的ASCII值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> using namespace std; int main() { char a; cin>>a; int b=a; int c=b-1; int d=b+1; char e=c; char f=d; cout<<e<<" "<<a<<" "<<f<<endl; cout<<c<<" "<<b<<" "<<d; return 0; } |
##0x45 能否被3,5,7整除
编程,从键盘输入一个整数,判断它能否被3,5,7整除,并输出以下信息之一:
⑴ Can be divisible by 3,5,7.
即能同时被3,5,7整除;
⑵ Can be divisible by ?,?. (其中?为3,5,7之一)
即能被其中两个数(要指出哪两个)整除;
⑶ Can be divisible by ?. (其中?为3,5,7之一)
即能被其中一个数(要指出哪一个)整除;
⑷ Can not be divisible by 3,5,7.
即不能被3,5,7任一个整除。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <iostream> using namespace std; int main() { int m; cin>>m; if(m%3==0&&m%5==0&&m%7==0) { cout<<"Can be divisible by 3,5,7."<<endl; } if(m%3!=0&&m%5==0&&m%7==0) { cout<<"Can be divisible by 5,7."<<endl; } if(m%3==0&&m%5!=0&&m%7==0) { cout<<"Can be divisible by 3,7."<<endl; } if(m%3==0&&m%5==0&&m%7!=0) { cout<<"Can be divisible by 3,5."<<endl; } if(m%3!=0&&m%5!=0&&m%7==0) { cout<<"Can be divisible by 7."<<endl; } if(m%3!=0&&m%5==0&&m%7!=0) { cout<<"Can be divisible by 5."<<endl; } if(m%3==0&&m%5!=0&&m%7!=0) { cout<<"Can be divisible by 3."<<endl; } if(m%3!=0&&m%5!=0&&m%7!=0) { cout<<"Can not be divisible by 3,5,7."<<endl; } return 0; } |
##0x46 圆的体积
编写一个程序实现如下功能:从键盘输入圆球半径r,屏幕输出圆球的体积(保留2位小数)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <iomanip> using namespace std; #define PI 3.1415926 int main() { double v; double r; cin>>r; v=4.0/3.0*PI*r*r*r; cout<<fixed<<setprecision(2); cout<<v<<endl; return 0; } |
##0x47 逆序乘积式
若两个正整数的乘积,等于两正整数各自逆序后的乘积,则称其为逆序乘积式。编写程序读入两个正整数,然后判断这两个正整数能否构成逆序乘积式。假设两个正整数的乘积不会超过int数据类型的表示范围。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> using namespace std; int main() { int a; int b; cin>>a>>b; int c=0; int d=0; int e=a; int f=b; while(e) { c=c*10+e%10; e=e/10; } while(f) { d=d*10+f%10; f=f/10; } if(a*b==c*d) { cout<<a<<"*"<<b<<"="<<c<<"*"<<d<<endl; } else { cout<<a<<"*"<<b<<"!="<<c<<"*"<<d<<endl; } return 0; } |
0x48 计算sinx的近似值
给定一个精度值e,用下列公式计算sin(x)的近似值,要求前后两次迭代之差的绝对值小于e,给出相应的最小迭代次数n和最后一次计算的sin(x)值。
sin x = x – x3/3! + x5/5! – x7/7! + … + (-1)n-1x2n-1/(2n-1)!
其中x为弧度,n为正整数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <cmath> #include <iomanip> using namespace std; double jc(double n) { double a=1; for(double i=1.0; i<=2*n-1; i++) { a*=i; } return a; } double lc(double x,double n) { double b=1; for(double i=1.0; i<=2*n-1; i++) { b=b*x; } return b; } int main() { double m=0; double x; double e; cin>>x>>e; for(int n=1; n<1000; n++) { double a=m; m=m+pow(-1,n-1)*lc(x,n)/jc(n); if(fabs(m-a)<e) { cout<<n<<" "<<fixed<<setprecision(9)<<m<<endl; break; } } return 0; } |
0x49 猴子选大王
要从n只猴子中选出一位大王。它们决定使用下面的方法:
n只猴子围成一圈,从1到n顺序编号。从第q只猴子开始,从1到m报数,凡报到m的猴子退出竞选,下一次又从退出的那只猴子的下一只开始从1到m报数,直至剩下的最后一只为大王。请问最后哪只猴子被选为大王。
控制台输入三个整数n,m,q。输出最后选为大王的猴子编号。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> using namespace std; int f(int n, int m, int q) { if (n == 1) return 1; if (q > 1) return (f(n,m,1)+q-2)%n+1; else return (f(n-1,m,1)+m-1)%n+1; } int main() { int n, m, q; cin>>n>>m>>q; cout<<f(n,m,q); return 0; } |
##0x50 求cos(x)的近似值
给定一个精度值e,用下列公式计算cos(x)的近似值,要求前后两次迭代之差的绝对值小于e,给出相应的最小迭代次数n和最后一次计算的cos(x)值。
cos(x)=x0/0!-x2/2!+x4/4!-x6/6!+……+(-1)n×x2n/((2n)!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> #include <cmath> #include <iomanip> using namespace std; double jc(double n) { double a=1; for(double i=1.0; i<=2*n; i++) { a*=i; } return a; } double lc(double x,double n) { double b=1; for(double i=1.0; i<=2*n; i++) { b=b*x; } return b; } int main() { double m=0; double x; double e; cin>>x>>e; for(int n=0; n<1000; n++) { double a=m; m=m+pow(-1,n)*lc(x,n)/jc(n); if(fabs(m-a)<e) { cout<<n<<" "<<fixed<<setprecision(7)<<m<<endl; break; } } return 0; } |
##0x51 奇数序列
每个立方数都是一个连续的奇数序列之和。编写一程序,输入一个整数(2 <= n <= 20),输出构成n的立方的最长的连续奇数序列(即:起始奇数最小的序列)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> using namespace std; int main() { int a; cin>>a; a=a*a*a; int i; int b; int c=0; for(i=1; i<=a; i=i+2) { c=0; for(int b=i; b>=1; b=b-2) { c=c+b; if(c==a) for(int d=b; d<=i; d=d+2) cout<<d<<" "; if(c==a) break; } if(c==a) break; } return 0; } |
0x52 求三角形面积
若已知三角形三个边的长度分别为a,b,c(并假设三个边长度的单位一致,在本编程题中忽略其单位),则可以利用公式S=sqrt(s(s-a)(s-b)(s-c))求得三角形的面积,其中:s=(a+b+c)/2。编程实现从控制台读入以整数表示的三个边的长度(假设输入的长度肯定可以形成三角形),然后利用上述公式计算面积并输出,结果小数点后保留3位有效数字。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; double s=(a+b+c)/2; double S; S=sqrt(s*(s-a)*(s-b)*(s-c)); cout<<fixed<<setprecision(3)<<S; return 0; } |
##0x53 计算三角形周长和面积
输入三角形的三边a,b,c,如果能构成一个三角形,输出面积area
和周长perimeter(保留两位小数);否则,输出
“These sides do not correspond to a valid triangle”
在一个三角形钟,任意两边之和大于第三边。三角形面积 计算公式:
area=sqrt(s(s-a)(s-b)*(s-c))
其中:s=(a+b+c)/2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { double a,b,c; double area; double perimeter; cout<<"Enter 3 sides of the triangle:"; cin>>a>>b>>c; if(a+b>c||b+c>a||a+c>b) { double s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); perimeter=a+b+c; cout<<fixed<<setprecision(2); cout<<"area="<<area<<"perimeter="<<perimeter; } else { cout<<"These sides do not correspond to a valid triangle"; } return 0; } |
0x54 两个整数的和、差、积、商与余数
输入两个整数num1和num2,计算并输出它们的和、差、积、商(用实数表示,保留2位小数点)与余数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <iomanip> using namespace std; int main() { double num1; double num2; cout<<"Enter num1:"; cin>>num1; cout<<"Enter num2:"; cin>>num2; cout<<num1<<"+"<<num2<<"="<<num1+num2<<endl; cout<<num1<<"-"<<num2<<"="<<num1-num2<<endl; cout<<num1<<"*"<<num2<<"="<<num1*num2<<endl; cout<<num1<<"/"<<num2<<"="<<fixed<<setprecision(2)<<num1/num2<<endl; cout<<(int)num1<<"%"<<(int)num2<<"="<<(int)num1%(int)num2<<endl; return 0; } |
0x55 按位拆解
输入一个三位正整数,求解并输出该数的个位数、十位数和百位数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> using namespace std; int main() { int a; int x,y,z; cin>>a; x=a%10; a=a/10; y=a%10; a=a/10; z=a; cout<<x<<" "<<y<<" "<<z<<endl; return 0; } |
0x56 判断可逆素数
若将某一素数的各位数字的顺序颠倒后得到的数仍是素数,则此素数称为可逆素数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <stdio.h> #include <math.h> int getrnum(int num){ int rnum=0; do{ rnum *= 10; rnum = rnum + num%10; }while((num/=10)!=0); return rnum; } int sushu(int num){ int i,k; k = (int)sqrt((double)num); for(i=2;i<=k;i++){ if(num%i==0) break; } if(i<k) return 0;//返回值为0表示不为素数 else return 1; } void keni(int num){ int flag,rnum; flag = sushu(num); if(flag == 0) printf("no\n"); else{ rnum = getrnum(num); flag = sushu(rnum); if(flag == 0) printf("no\n"); else printf("yes\n"); } } int main(){ int num,rnum; scanf("%d",&num); keni(num); getchar(); return 0; } |
0x57 r1重新构成新数
从键盘输入一个正整数x,将该数的奇数位上的数和偶数位上的数按反序排列构成两个新数分别存入变量a和b中并输出,各数之间以空格分隔。(奇、偶数位是从整数x的高位至低位依次确定)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> using namespace std; int main() { int a; int b; int c=0; int d=0; cin>>a; while(a!=0) { int i=0; b=a%10; if(b%2==0) c=c*10+b; else d=d*10+b; a=a/10; i++; } cout<<d<<" "<<c; return 0; } |
0x58 长度单位换算
编程,将从键盘输入的n英寸换算成用英里(mi)、码(yd)、英尺(ft)和英寸(in)的表示形式(1英里=1760码,1码=3英尺,1英尺=12英寸)输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> using namespace std; int main() { int a; cin>>a; int b=a/(1760*3*12); if(b!=0) cout<<b<<"mi"; int c=(a-b*(1760*3*12))/(3*12); if(c!=0) cout<<c<<"yd"; int d=(a-b*(1760*3*12)-c*(3*12))/12; if(d!=0) cout<<d<<"ft"; int e=a-b*(1760*3*12)-c*(3*12)-d*12; if(e!=0) cout<<e<<"in”; return 0; } |
0x59 判断三角形
从键盘输入三角形的三条边长,判断能否构成三角形,如能构成三角形,则判断是哪一种类型:等腰三角形、等腰直角算作等腰、等边三角形、直角三角形、任意三角形。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include<iostream> #include <cmath> using namespace std; int main() { double a; double b; double c; while(cin>>a>>b>>c) { if(a+b>c&&a+c>b&&b+c>a) { if(a==b||a==c||b==c) { if(a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b) { cout<<"isoceles right-angled triangle"<<endl; } if(a==b&&a==c) { cout<<"equilateral triangle"<<endl; } else { cout<<"isoceles triangle"<<endl; } } else { if(fabs(a*a+b*b-c*c)<0.00001||fabs(b*b+c*c-a*a)<0.00001||fabs(a*a+c*c-b*b)<0.00001) { cout<<"right-angled triangle"<<endl; } else { cout<<"arbitrary triangle"<<endl; } } } else { cout<<"It isn’t triangle"<<endl; } } return 0; } |
0x60 数值变换
编写一程序,从键盘输入输入一个三位正整数,然后反向输出对应的数,如果输入的数不是三位正整数,则输出-1。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int main() { int a,b,c,d; while(cin>>a) { if(100<=a&&a<=999) { b=a/100; c=(a-100*b)/10; d=a-100*b-10*c; cout <<d<<c<<b<<endl; } else cout <<"-1"<<endl; } return 0; } |