最大公约数与最小公倍数
//辗转相除法求公约公倍
#include <stdio.h>int gcd(int a, int b) {return (a % b == 0) ? b : gcd(b, a % b);
}int main() {int m, n;scanf("%d %d", &m, &n); int ans = gcd(m, n);printf("%d %d\n", ans, m * n / ans);return 0;
}
字符串分类统计
#include <stdio.h>
#include <string.h>
int
main(void)
{char str[200];//声明一个数组用于存储输入的字符int letter = 0, number = 0, space = 0, other, n;gets(str); //获取输入并存储到数组里,gets会把输入的换行符\n丢弃n = strlen (str); //获取输入的字符长度for(int i = 0; i < n; i++)//for循环可以声明变量同时初始化,多个声明用 , 逗号隔开{if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))letter += 1;if (str[i] == ' ')space += 1;if (str[i] >= '0' && str[i] <= '9')number += 1; }other = n - space - number - letter; //把整个字符长度减去字母数、空格、数字得出其他字符的数量printf("%d %d %d %d", letter, number, space, other);return 0;
}
Sn的公式求和
#include<stdio.h>
int main(){int n, i, sum = 0;scanf("%d", &n);for(i = 0; i < n; i++){sum *= 10;sum += 2 * (i+1);}printf("%d", sum);return 0;
}
阶乘求和
#include <stdio.h>
int main(void)
{int n;long long Sn = 0, j;scanf("%d", &n); for (int i = 1; i <= n; i++){j = 1;for(int m = 0; m < i; m++)j *= (m + 1);Sn = Sn + j;}printf("%lld", Sn); //long long的格式指定符为%lldreturn 0;
}
求和训练
#include<stdio.h>
int main(void)
{int An = 0, Bn = 0;float Cn = 0;int a, b, c;float i; //这里i的声明类型也可以为int,但如果这样,下面有些地方就要做出改变scanf("%d %d %d", &a, &b, &c);for (i = 1; a >= i; i++){An = i + An;}for (i = 1; b >= i; i++){Bn = i * i + Bn;}for ( i = 1; c >= i; i++){Cn = 1 / i + Cn; /*如果上面i的定义不为浮点型而是整型,这里的1就应该变为1.0,否则1/i的结果就不是浮点型,小数点后面位数的数值会直接被忽略,这里也是导致程序结果虽然保留两位小数,但小数点后的位数却是零的缘故。*/}printf("%.2f", An + Bn + Cn);return 0;
}
水仙花数判断
#include<stdio.h>
int main(){int i;for(i=100;i<=999;i++){if(i==(i/100)*(i/100)*(i/100)+((i%100)/10)*((i%100)/10)*((i%100)/10)+(i%10)*(i%10)*(i%10))printf("%d\n",i);}return 0;
}
完数的判断
#include<stdio.h>
#define max 1001
int main() {int n,i,j,a[max],l;scanf("%d",&n);for(i=1; i<=n; i++) {int tem=0,k=0; //每次开始新一轮的内循环之前重置 for(j=1; j<i; j++) {if(i%j==0) {//满足条件 tem+=j;//储存因子之和 a[k++]=j;//储存因子 }}if(tem==i) {//满足条件 格式输出 printf("%d its factors are",i);for(l=0;l<k;l++){printf(" %d",a[l]);}printf("\n");}}return 0;
}
有规律的数列求和
#include<stdio.h>
int main(){double Sn=0,an=0,n,i,t,a=2.0,b=1.0;//a为分子,b为分母,an为单项和值,Sn为总和scanf("%lf",&n);Sn+=a/b;//第一项不在规律中,在循环前先加入for(i=1;i<n;i++){t=a;a=a+b;//将前一项的分子加分母给后一项的分子b=t;//将前一项的分子给后一项的分母an=a/b;Sn+=an;}printf("%0.2lf",Sn);return 0;
}
自由下落的距离计算
#include<stdio.h>
int main()
{double m, h, ans;int n;scanf("%lf%d",&m,&n);h = m / (1 << n);ans = m + (m - h * 2) * 2;printf("%.2lf %.2lf", h, ans);return 0;
}
猴子吃桃的问题
# include <stdio.h>int main()
{int sum=1,N;scanf("%d",&N);while(--N)sum=(sum+1)*2;printf("%d",sum);return 0;
}
迭代法求平方根
#include <stdio.h>
#include <math.h>
int main()
{ float left,right,mid; float fleft, fright,fmid; left=-10.; right=10.; mid=(left+right)/2; fmid=2*mid*mid*mid-4*mid*mid+3*mid-6; while(fabs(fmid)>1e-6) { fleft=2*left*left*left-4*left*left+3*left-6; fright=2*right*right*right-4*right*right+3*right-6; if(fleft*fmid>0) left=mid; else right=mid; mid=(left+right)/2; fmid=2*mid*mid*mid-4*mid*mid+3*mid-6; } printf("%.2f\n",mid); return 0;
}