第一题
第二题
略
第三题
#include<stdio.h>
#include<math.h>
int main(){
int m,n;
scanf("%d,%d",&m,&n);
int temp;
if(m>n) {
temp = m;
m = n;
n = temp;
}
int n1,n2;
for(int i = m;i>0;i--){
if(m%i==0 && n%i==0){
n1 = i;
break;
}
}
printf("最大公约数为:%d\n",n1);
for(int i = n;i<=n*m;i++){
if(i%m==0 && i%n==0){
n2 = i;
break;
}
}
printf("最小公倍数为:%d\n",n2);
return 0;
}
第四题
#include<stdio.h>
#include<math.h>
int main(){
char c;
int n1=0,n2=0,n3=0,n4=0;
while((c = getchar()) != '\n'){
if(c>='a'&&c<='z' || c>='A'&&c<='Z') n1++;
else if(c>='0'&&c<='9') n2++;
else if(c == ' ') n3++;
else n4++;
}
printf("英文字母总共%d个\n",n1);
printf("数字总共%d个\n",n2);
printf("空格总共%d个\n",n3);
printf("其他字符总共%d个\n",n4);
return 0;
}
第五题
#include<stdio.h>
#include<math.h>
int main(){
int a,n;
int sum = 0;
int temp = 0;
printf("请按照该格式输入:数字,位数\n");
scanf("%d,%d",&a,&n);
for(int i = 1;i<=n;i++){
temp = temp*10+a;
sum += temp;
printf("%d\n",temp);
}
printf("求和结果为:%d\n",sum);
return 0;
}
第六题
#include<stdio.h>
#include<math.h>
int main(){
double s = 1,sum = 0;
for (int i = 1;i<=20;i++){
s = 1;
for (int j = 1;j<=i;j++){
s *= j;
}
sum += s;
}
printf("所求结果为: %lf\n",sum);
return 0;
}
第七题
#include<stdio.h>
#include<math.h>
int main(){
double sum = 0;
double s1=0,s2=0,s3=0;
for(int i = 1;i<=100;i++){
s1 += i;
}
for (int i = 1;i<=50;i++){
s2 += i*i;
}
for (int i = 1;i<=10;i++){
s3 += 1.0/i;
}
sum = s1+s2+s3;
printf("所求结果为:%lf\n",sum);
return 0;
}
第八题
#include<stdio.h>
#include<math.h>
int main(){
for(int i = 111;i<=999;i++){
int k = i;
int sum = 0;
while(k>0){
int m = k%10;
sum += m*m*m;
k /= 10;
}
if(sum == i) printf("%d为水仙花数\n",i);
}
return 0;
}
第九题
#include<stdio.h>
#include<math.h>
int main(){
for (int i = 2;i<=1000;i++){
int s = 0;
for(int j = 1;j<=i/2;j++){
if(i%j == 0) s += j;
}
if(s == i) {
printf("%d its factors are 1,",i);
for(int n = 2;n<=i/2;n++){
if(i%n == 0) printf("%d,",n);
}
printf("\n");
}
}
return 0;
}
第十题
#include <stdio.h>
int main()
{
double a = 2, b = 1, sum = 0;
double temp;
for (int i = 0; i < 20; i++)
{
sum += a / b;
temp = a;
a = a + b;
b = temp;
}
printf("前20项之和为:sum=%9.7f\n",sum);
return 0;
}
第十一题
#include <stdio.h>
int main()
{
double H = 100;
int count = 0;
double h,sum = 0;
while(count < 10){
H /= 2.0;
h = H;
sum += h*2;
count ++;
}
printf("共经过%lf米,第十次反弹%lf米\n",sum+100,h);
return 0;
}
第十二题
#include <stdio.h>
int main()
{
int sum = 1;
for(int i =0;i<9;i++){
sum = (sum+1)*2;
}
printf("一共有%d个桃子\n",sum);
return 0;
}
第十三题
#include <stdio.h>
#include <math.h>
int main()
{
float a, x0, x1;
printf("请输入一个正数: ");
scanf("%f", &a);
x0 = a / 2;
x1 = (x0 + a / x0) / 2;
do
{
x0 = x1;
x1 = (x0 + a / x0) / 2;
} while (fabs(x0 - x1) >= 1e-5);
printf("[%f] 的平方根为 [%f]\n", a, x1);
return 0;
}
第十四题
#include <stdio.h>
#include <math.h>
int main()
{
double x1, x0, f, f1;
x1 = 1.5;
do
{
x0 = x1;
f = ((2 * x0 - 4) * x0 + 3) * x0 - 6;
f1 = (6 * x0 - 8) * x0 + 3;
x1 = x0 - f / f1;
} while (fabs(x1 - x0) >= 1e-5);
printf("方程在1.5附近的根为:%lf\n", x1);
return 0;
}
第十五题
#include <stdio.h>
#include <math.h>
int main()
{
double left = -10,right = 10,mid;
double temp=10;
while(fabs(temp)>1e-5){
mid = (left+right)/2;
temp = 2*mid*mid*mid-4*mid*mid+3*mid-6;
if(temp>0){
right = mid;
}else if(temp<0){
left = mid;
}
}
printf("在(10,-10)中的根为%lf\n",mid);
return 0;
}
第十六题
#include <stdio.h>
#include <math.h>
int main()
{
for(int i = 1;i<=4;i++){
for(int k = 1;k<=4-i;k++){
printf(" ");
}
for (int j = 1;j<=2*i-1;j++){
printf("*");
}
printf("\n");
}
for(int i = 4;i>=1;i--){
for(int k = 1;k<=4-i;k++){
printf(" ");
}
for (int j = 2*i-1;j>=1;j--){
printf("*");
}
printf("\n");
}
return 0;
}
第十七题
#include <stdio.h>
int main()
{
int A, B, C;
for (A = 'X'; A <= 'Z'; A++)
{
for (B = 'X'; B <= 'Z'; B++)
{
for (C = 'X'; C <= 'Z'; C++)
{
//去除限制条件
if (A == 'X' || C == 'X' || C == 'Z' || B == A || B == C || A == C)
{
continue;
}
printf("A对%c\tB对%c\tC对%c", A, B, C);
}
}
}
return 0;
}