本文地址:http://www.cnblogs.com/archimedes/p/programming-abstractions-in-c-1.html,转载请注明源地址。
1、温度转换:
#include<stdio.h>
int main()
{
double C;
while(~scanf("%lf", &C))
printf("%lf\n", * C * 1.0 / + );
return ;
}
2、长度转换:
#include<stdio.h>
int main()
{
double YCI, YC, M;
while(~scanf("%lf", &M)){
printf("YCI:%lf\n", 1.0 / 0.0254);
printf("YC:%lf\n", 1.0 / );
}
return ;
}
3、计算1+2+3+……+100
#include<stdio.h>
int main()
{
int sum = ;
for(int i = ; i <= ; i++)
sum += i;
printf("%d\n", sum);
return ;
}
4、计算序列值
#include<stdio.h>
int main()
{
int N, i, sum;
scanf("%d", &N);
sum = ;
i = ;
while(i <= N)
sum += * (i++) - ;
printf("%d\n", sum);
return ;
}
5、按照指定格式输入一个整数序列中的最大值
#include<stdio.h>
int main()
{
int max, n, i;
printf("This program finds the largest integer in a list.\n");
printf("Enter 0 to sigal the end of the list.\n");
printf("? ");
max = -;
while(scanf("%d", &n) && n) {
if(max < n) max = n;
printf("? ");
}
printf("The largest value is %d\n", max);
return ;
}
6、反转打印输入的整数
#include<stdio.h>
void print(int N)
{
printf("The reverse number is ");
while(N) {
printf("%d", N % );
N /= ;
}
printf("\n");
}
int main()
{
int N;
printf("This program reverses the digits in an integer.\n");
printf("Enter a positive integer: ");
scanf("%d", &N);
print(N);
return ;
}
7、寻找完全数
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
bool IsPerfect(int n)
{
int m, sum, t;
sum = ;
m = (int)sqrt(n);
for(int i = ; i <= m; i++){
if(n % i == ) {
if(i * i == n) {
sum += m;
} else {
sum += (i + n / i);
}
}
}
if(sum == n)
return true;
else
return false;
}
int main()
{
for(int i = ; i <= ; i++) {
if(IsPerfect(i))
printf("%d\n", i);
}
return ;
}
8、按照指定格式分解质因数
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#define N 100000
int isprime[N];
bool prime(int n)
{
int i;
for(i = ; i * i <= n; i++) {
if(n % i == )
return false;
}
return true;
}
void init()
{
int i, k;
k = ;
for(i = ; i < ; i++) {
if(prime(i))
isprime[k++] = i;
}
}
void solve()
{
int i, n, flag, k, t;
init();
printf("Enter number to be factored: ");
while(~scanf("%d", &n)) {
k = flag = ;
if(prime(n)) {
printf("%d\n", n);
} else {
t = n;
while(isprime[k] < n) {
while(t % isprime[k] == ) {
if(!flag) {
flag = ;
printf("%d", isprime[k]);
} else {
printf(" * %d", isprime[k]);
}
t /= isprime[k];
}
k++;
}
printf("\n");
}
printf("Enter number to be factored: ");
}
}
int main()
{
solve();
return ;
}
9、浮点数按照规则转化为整数
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
void Round(float n)
{
int m;
m = floor(n);
if(n > ) {
if(n - m < 0.5) {
printf("%d\n", m);
} else {
printf("%d\n", m + );
}
} else {
if(n - 0.5 > m) {
printf("%d\n", m + );
} else {
printf("%d\n", m);
}
} }
int main()
{
float n;
while(~scanf("%f", &n))
Round(n);
return ;
}
10、利用莱布利兹公式计算PI
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
double count(int n)
{
int t, i;
double ans = 0.0;
t = ;
for(i = ; i <= n; i++) {
ans += t * 1.0 / ( * i - );
t *= -;
}
return * ans;
}
int main()
{
printf("%f\n", count());
return ;
}
11、通过扇形的面积近似计算PI
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#define N 100
void solve()
{
int i;
double w, r, ans, x;
r = 2.0;
w = r / N;
ans = 0.0;
for(i = ; i <= N; i++) {
x = w * (i - 0.5);
ans += sqrt(r * r - x * x) * w;
}
printf("%f\n", ans);
}
int main()
{
solve();
return ;
}