- 求N分之一序列前N项和
#include <stdio.h>
int main()
{
double sum=0;
int n, i;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
sum = sum + 1.0/i;
}
printf("sum = %.6lf",sum);
return 0;
}
- 求阶乘序列前N项和
#include <stdio.h>
int main()
{
int n, i=1, sum=0, jc, t;
scanf("%d",&n);
for(; i<=n; i++)
{
t = i;
for(jc=1; i>0; i--)
{
jc *= i;
}
sum += jc;
i=t;
}
printf("%d",sum);
return 0;
}
- 求简单交错序列前N项和
#include <stdio.h>
int main()
{
int n, i;
double numerator, denominator, t, sum=0.0;
scanf("%d",&n);
for(i=0; i<n; i++)
{
numerator = 1;
denominator = 1+3*i;
if(i%2 == 0)
{
t = numerator/denominator;
}
else
{
t = -(numerator/denominator);
}
sum += t;
}
printf("sum = %.3lf",sum);
return 0;
}
- 特殊a串数列求和
#include <stdio.h>
#include <math.h>
int main()
{
int i, n, a, t=0, sum=0;
scanf("%d %d",&a, &n);
for(i=1; i<=n; i++)
{
t = t*10 + a;
sum += t;
}
printf("s = %d",sum);
return 0;
}
- 生成3的乘方表
#include <stdio.h>
#include <math.h>
int main()
{
int i, n, y;
scanf("%d",&n);
for(i=0;i<=n;i++)
{
y = pow(3,i);
printf("pow(3,%d) = %d\n",i, y);
}
return 0;
}
- 找出最小值
#include <stdio.h>
int main(void)
{
int n, i, k, min;
scanf("%d", &n);
for(i = 0; i < n; i++)
{
scanf("%d", &k);
if( k < min )
{
min = k;
}
}
printf("min = %d", min);
return 0;
}
- 找出三位水仙花数
#include <stdio.h>
#include <math.h>
int main()
{
int m, n, i, sum;
scanf("%d %d",&m, &n);
if(m>n || n<100 || n>999 || m<100 || m>999)
{
printf("Invalid Value.");
}
else
{
for(;m<=n;m++)
{
i = m;
sum=0;
for(;i>0;i/=10)
{
sum += pow(i%10,3);
}
if(sum==m)
{
printf("%d\n",m);
}
}
}
return 0;
}
- 猴子吃桃问题
#include <stdio.h>
int main()
{
int n, num=1, i;
scanf(" %d",&n);
for(i=2; i<=n; i++)
{
num = (num+1)*2;
}
printf("%d",num);
return 0;
}
- 求整数段和
#include<stdio.h>
int main()
{
int a,b,sum=0,count=0;
scanf("%d %d",&a,&b);
for(int i=a; i<=b; i++)
{
printf("%5d",i);
sum += i;
count++;
if(count%5==0 && count!=0) printf("\n");
}
if(count%5!=0) printf("\n");
printf("Sum = %d",sum);
}
- 求整数的位数及各位数字之和
#include <stdio.h>
int main()
{
long long int n;
int i=0, num=0;
scanf("%lld",&n);
for(;n>0;)
{
i += n%10;
num++;
n /= 10;
}
printf("%d %d",num, i);
return 0;
}
- 统计数字字符和空格
#include<stdio.h>
int main()
{
int blank,digit,other;
blank = other = digit = 0;
char n;
while(n!='\n')
{
n=getchar();
switch(n)
{
case ' ': case '\n':
blank++;
break;
case '0': case '1': case '2': case '3': case'4': case'5':
case '6': case '7': case '8': case '9':
digit++;
break;
default:
other++;
break;
}
}
blank-=1;
printf("blank = %d, digit = %d, other = %d",blank, digit, other);
return 0;
}
- 韩信点兵
#include<stdio.h>
int main()
{
int i=1,j;
while(1)
{
if((i%5==1) && (i%6)==5 && (i%7)==4 && (i%11)==10)
{
printf("%d",i);
break;
}
else
i++;
}
return 0;
}
- 爬动的蠕虫
#include <stdio.h>
int main()
{
int n, d, u;
int time, i=0, s=0;
scanf("%d %d %d",&n, &u, &d);
while(s+u < n)
{
s = s+u-d;
i = i+2;
}
i = i+1;
printf("%d",i);
return 0;
}
- 近似求PI
#include <stdio.h>
int main()
{
double eps;
scanf("%le", &eps);
double sum = 1;
double temp = 1;
for(int i=1; temp>eps; i++)
{
temp = temp*i/(2*i+1);
sum += temp;
}
printf("PI = %.5f\n", 2*sum);
return 0;
}
- 输出华氏-摄氏温度转换表
#include <stdio.h>
#include <math.h>
int main ()
{
int lower, upper;
int F;
double C;
scanf("%d %d",&lower, &upper);
if(lower > upper)
{
printf("Invalid.");
}
else
{
printf("fahr celsius\n");
for(lower;lower<=upper;lower=lower+2)
{
F = lower;
C = 5*(F*1.0-32)/9;
printf("%d%6.1f\n",F,C);
}
}
return 0;
}
- 统计学生成绩
#include <stdio.h>
int main()
{
int student[1000];
int n, i, j;
int a=0, b=0, c=0, d=0, e=0;
scanf("%d",&n);
for(i=0; i<n; i++)
scanf("%d", &student[i]);
for(j=0; j<n; j++)
{
switch(student[j] / 10)
{
case 10:
case 9: a++; break;
case 8: b++; break;
case 7: c++; break;
case 6: d++; break;
default: e++; break;
}
}
printf("%d %d %d %d %d",a,b,c,d,e);
return 0;
}
- 最佳情侣身高差
#include <stdio.h>
int main()
{
int i,j,n;
char c[10];
double a[10];
scanf("%d",&n);
for(i=0;i<n;i++)
{
getchar();
scanf("%c %lf",&c[i], &a[i]);
}
for(j=0; j<n; j++)
{
if(c[j] == 'M')
{
printf("%.2lf\n",a[j]/1.09);
}
else if(c[j] == 'F')
{
printf("%.2lf\n",a[j]*1.09);
}
}
return 0;
}
- 小于m的最大的10个素数
#include <stdio.h>
int main()
{
int i, m, j, isprime=1;
scanf("%d", &m);
m--;
for(i=0; i<10; m--)
{
isprime = 1;
for(j=2; j<m; j++)
{
if(m%j == 0)
isprime = 0;
}
if(isprime == 1)
{
printf("%6d",m);
i++;
}
}
return 0;
}
- 统计素数并求和
#include <stdio.h>
int main()
{
int count = 0, sum = 0;
int m, n;
int i, j;
scanf("%d %d", &m, &n);
for (i = m; i <= n; i++)
{
for (j = 2; j <= i; j++)
{
if (i % j == 0) break;
}
if (j == i)
{
sum += i;
count += 1;
}
}
printf("%d %d", count, sum);
return 0;
}
- 人民币兑换
#include<stdio.h>
int main ()
{
int n, a, m=0, b, c;
scanf ("%d", &n);
for (a=1; a<=30; a++)
{
for (b=1; b<=75; b++)
{
c=100-a-b;
if (a*5+b*2+c==150)
{
printf ("%d %d %d\n", a, b, c);
m++;
}
if (m==n)
break;
}
}
return 0;
}