**7-1 求e的近似值(15 分)**
#include <stdio.h>
int main(){
double e,item;
int n;
item=1;
e=1;
scanf("%d",&n);
for(int i=1;i<=n;i++){
item*=i;
e+=1.0/item;
}
printf("%.8f",e);
return 0;
}
7-2 换硬币(20 分)
#include <stdio.h>
int main(){
int money,count=0;
scanf("%d",&money);
for(int i=money/5;i>=1;i--){
for(int j=money/2;j>=1;j--){
for(int m=money;m>=1;m--){
if(5*i+2*j+m==money){
printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n",i,j,m,i+j+m);
count++;
continue;
}
}
}
}
printf("count = %d",count);
return 0;
}
7-3 输出三角形字符阵列(15 分)
#include <stdio.h>
int main(){
int n;
char c='A';
scanf("%d",&n);
for(int i=n;i>0;i--){
for(int j=i;j>0;j--){
printf("%c ",c++);
}
printf("\n");
}
return 0;
}
7-4 求幂级数展开的部分和(20 分)
#include <stdio.h>
#include <math.h>
int main() {
int i=1,j,k;
double x;
scanf("%lf",&x);
double sum=1,t,eps=1;
t=1;
while(fabs(eps)>1e-5) {
t*=i;
eps=pow(x,i)/t;
sum+=eps;
i++;
}
printf("%.4f",sum);
return 0;
}
7-5 输出整数各位数字(15 分)
#include<stdio.h>
#include<math.h>
int main()
{
int i, x, n, digit, count = 0;
scanf("%d", &x);
n = x;
if(x==0){
printf("0 ");
}else{
while (x != 0) {
x = x / 10;
count++;
}
for (i = count - 1; i>=0; i--) {
digit = n / pow(10, i);
n = n % (int)pow(10, i);
printf("%d ", digit);
}}
return 0;
}
7-5 输出整数各位数字(15 分)方法2
#include<stdio.h>
#include<math.h>
int main()
{
int x,num=0,count=0;
scanf("%d",&x);
if(x==0){
printf("0 ");
}else{
while(x!=0){
num=num*10+x%10;
if(x%10==0){
count++;
}
x/=10;
}
while(num!=0){
printf("%d ",num%10);
num/=10;
}
for(int i=0;i<count;i++){
printf("0 ");
}
}
return 0;
}
}
7-6 验证“哥德巴赫猜想”(20 分)
#include <stdio.h>
#include <math.h>
int prime(int p);
int main() {
long n;
int i,j;
scanf("%ld",&n);
for(i=2; i<n; i++) {
j=n-i;
if(prime(i) == 1 && prime(j) == 1 && n == (i + j)) {
printf("%d = %d + %d",n,i,j);
break;
}
}
return 0;
}
int prime(int p) {
int a = 0;
for (int i = 2; i *i<= p ; i++) {
if (p % i == 0) {
return 0;
}
}
return 1;
}
7-7 找完数(20 分)
#include <stdio.h>
int factorsum( int number );
void PrintPN( int m, int n );
int main()
{
int i, m, n;
scanf("%d %d", &m, &n);
PrintPN(m, n);
return 0;
}
int factorsum(int number) {
int sum = 1;
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0)
sum += i;
}
return sum;
}
void PrintPN(int m, int n) {
int count=0;
for (int i = m; i <= n; i++) {
if (factorsum(i) == i) {
count++;
printf("%d = ", i);
if (i == 1) {
printf("%d", 1);
}
for (int j = 1; j <= i / 2; j++) {
if (i % j == 0) {
printf("%d", j);
if (j != i / 2) {
printf(" + ");
}
}
}
printf("\n");
}
}
if(count==0){
printf("None");
}
}
网友评论