1.两个整数四则运算
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int multi(int a, int b)
{
return a * b;
}
double div(int a, int b)
{
double ans = a * 1.0 / b;
return ans;
}
int mod(int a, int b)
{
return a % b;
}
int main()
{
int m, n;
scanf("%d %d", &m, &n);
printf("%d + %d = %d\n", m, n, add(m, n));
printf("%d - %d = %d\n", m, n, sub(m, n));
printf("%d * %d = %d\n", m, n, multi(m, n));
if(n != 0)
printf("%d / %d = %.2f\n", m, n, div(m, n));
printf("%d %% %d = %d", m, n, mod(m, n));
}
执行效果:
2.两个浮点数运算
#include <stdio.h>
double add(double a, double b)
{
return a + b;
}
double sub(double a, double b)
{
return a - b;
}
double multi(double a, double b)
{
return a * b;
}
double div(double a, double b)
{
float ans = a * 1.0 / b;
return ans;
}
int main()
{
double m, n;
scanf("%lf %lf", &m, &n);
printf("%lf + %lf = %.2lf\n", m, n, add(m, n));
printf("%lf - %lf = %.2lf\n", m, n, sub(m, n));
printf("%lf * %lf = %.2lf\n", m, n, multi(m, n));
printf("%lf / %lf = %.2lf\n", m, n, div(m, n));
}
执行效果:
3.字符转ASCII码
#include <stdio.h>
#include <string.h>
int main()
{
char c1[7] = {'a', ' b', 'c', 'd', 'e', 'f', 'g'};
char c2[7] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
for(int i=0; i < strlen(c1); i++)
{
printf("%c-->%d %c-->%d\n", c1[i], c1[i], c2[i], c2[i]);
}
return 0;
}
执行效果:
4.数值比较
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
int max = a > b ? a : b;
printf("%d", max);
return 0;
}
执行效果:
5.计算int/float/double/char的字节大小
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("size of int: %d bytes\n", sizeof(a));
printf("size of float: %d bytes\n", sizeof(b));
printf("size of double: %d bytes\n", sizeof(c));
printf("size of char: %d bytes\n", sizeof(d));
}
执行效果:
6.交换两个数的值
#include <stdio.h>
int main()
{
int a, b, temp;
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("%d %d\n", a, b);
}
执行效果:
7.判断奇偶
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
if(n % 2 == 0)
printf("%d是偶数\n", n);
else
printf("%d是奇数\n", n);
}
执行效果:
8.判断元音/辅音
#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
if((c=='a' || c=='e' || c=='i' || c=='o' || c=='u') || (c=='A' || c=='E' || c=='I' || c=='O' || c=='U'))
{
printf("%c是元音\n", c);
}
else
{
printf("%c是辅音\n", c);
}
return 0;
}
执行效果示例:
9.判断三个数中的最大最小值
#include <stdio.h>
int main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int max = a, min = a;
if(max < b)
{
max = b;
}
if(max < c)
{
max = c;
}
if(min > b)
{
min = b;
}
if(min > c)
{
min = c;
}
printf("max = %d, min = %d", max, min);
}
执行效果:
10.判断闰年
#include <stdio.h>
int main()
{
int year;
scanf("%d",&year);
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("%d 是闰年\n", year);
}else{
printf("%d 不是闰年\n", year);
}
return 0;
}
执行效果:
11.判断字母
#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
printf("%c是字母\n", c);
}
else
{
printf("%c 不是字母", c);
}
return 0;
}
执行效果:
12.求1-n的和
#include <stdio.h>
int main()
{
int n, i, sum = 0;
scanf("%d", &n);
for(i=1; i<=n; i++)
{
sum += i;
}
printf("%d", sum);
return 0;
}
执行效果:
13.输出九九乘法表
#include <stdio.h>
int main()
{
int i, j;
for(i = 1; i <= 9; i++)
{
for(j = 1; j <= i; j++)
{
printf("%dx%d=%d\t", j, i, i * j);
}
printf("\n");
}
return 0;
}
执行效果:
14.斐波那契数列
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, t3;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("%d ", t1);
t3 = t1 + t2;
t1 = t2;
t2 = t3;
}
return 0;
}
执行效果:
15.求两数的最大公约数
#include <stdio.h>
int main()
{
int a, b, min, gcd;
scanf("%d %d", &a, &b);
min = a < b ? a : b;
for(int i=1; i <= min; i++)
{
if (a % i == 0 && b % i == 0)
{
gcd = i;
}
}
printf("%d", gcd);
return 0;
}
执行效果:
16.求两数的最小公倍数
#include <stdio.h>
int main()
{
int a, b, min;
scanf("%d %d", &a, &b);
min = (a < b) ? a : b;
for(int i = min; i <= a * b; i++)
{
if((i % a == 0) && (i % b == 0))
{
printf("%d", i);
break;
}
}
return 0;
}
执行效果示例:
17.求阶乘
#include <stdio.h>
int main()
{
int n, fact=1;
scanf("%d", &n);
if(n < 0)
{
printf("error");
return -1;
}
for(int i=1; i <= n; i++)
{
fact *= i;
}
printf("%d", fact);
return 0;
}
执行效果示例:
18.判断数字为几位数
#include <stdio.h>
int main()
{
long long n;
int count = 0;
scanf("%lld", &n);
while(n)
{
n /= 10;
count++;
}
printf("输入的是一个%d位数", count);
return 0;
}
执行效果:
19.判断回文数
#include <stdio.h>
int main()
{
int n, reversed = 0, m, original;
scanf("%d",&n);
original = n;
while(n != 0)
{
m = n % 10;
reversed = reversed * 10 + m;
n /= 10;
}
if(reversed == original)
printf("%d是回文数\n", original);
else
printf("%d不是回文数\n", original);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
int n, m;
char s[20]={'\0'},s1[20]={'\0'};
scanf("%d", &n);
sprintf(s, "%d", n);
int count = strlen(s);
int i, j;
for(i = count-1, j=0; i >= 0, j<count; i--, j++)
{
s1[j] = s[i];
}
if(strcmp(s, s1) == 0)
printf("%d 是回文数\n", n);
else
printf("%d 不是回文数\n", n);
}
执行效果:
20.判断素数
#include <stdio.h>
#include <math.h>
int main()
{
int n, i, k;
scanf("%d", &n);
k = sqrt(n);
for(i = 2; i <= k; i++)
{
if(n % i == 0)
break;
}
if(i <= k)
printf("%d不是素数", n);
else
printf("%d是素数", n);
return 0;
}
执行效果:
21.输出水仙花数
#include <stdio.h>
int main()
{
int n, i, j, k;
for(n=100; n<1000; n++)
{
i = n / 100;
j = n / 10 % 10;
k = n % 10;
if(i * i * i + j * j * j + k * k * k == n)
printf("%d ", n);
}
return 0;
}
执行效果:
22.判断Armstrong数
#include <stdio.h>
#include <math.h>
int main()
{
int n, result = 0, m, count = 0, original1, original2;
scanf("%d",&n);
original1 = original2 = n;
while(original1)
{
original1 /= 10;
count++;
}
while(original2 != 0)
{
m = original2 % 10;
result += pow(m, count);
original2 /= 10;
}
if(result == n)
printf("%d是Armstrong数\n", n);
else
printf("%d不是Armstrong数\n", n);
return 0;
}
执行效果:
23.判断完数
#include <stdio.h>
void print(int n)
{
int i;
for(i=1; i<n; i++)
{
if(n % i == 0)
printf("%d ", i);
}
}
int main()
{
int n, sum=0, i;
scanf("%d", &n);
for(i=1; i<n; i++)
{
if(n % i == 0)
{
sum += i;
}
}
if(sum == n)
{
printf("%d 是一个完数,它的因子是:", n);
print(n);
}
else
printf("%d 不是一个完数", n);
}
执行效果:
24.求一个整数的所有因数
#include <stdio.h>
int main()
{
int num, i;
scanf("%d", &num);
for(i=1; i<= num; i++)
{
if(num % i == 0)
{
printf("%d ", i);
}
}
return 0;
}
执行效果:
25. 判断是否能构成三角形
#include <stdio.h>
bool is_triangle(int a, int b, int c)
{
if((a+b>c) && (a+c>b) && (b+c>a))
return true;
else
return false;
}
bool is_isosceles(int a, int b, int c)
{
if(is_triangle(a, b, c) && (a==b || a==c || b==c))
return true;
else return false;
}
bool is_equilateral(int a, int b, int c)
{
if(is_triangle(a, b, c) && (a==b && a==c))
return true;
else return false;
}
bool is_right(int a, int b, int c)
{
if(is_triangle(a, b, c))
{
if((a*a + b*b == c*c) || (a*a + c*c == b*b) || (b*b + c*c == a*a))
return true;
else
return false;
}
return false;
}
int main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(is_triangle(a, b, c))
printf("%d,%d,%d 能组成三角形\n", a, b, c);
else
printf("%d,%d,%d 不能组成三角形\n", a, b, c);
if(is_isosceles(a, b, c))
printf("%d,%d,%d 能组成等腰三角形\n", a, b, c);
else
printf("%d,%d,%d 不能组成等腰三角形\n", a, b, c);
if(is_equilateral(a, b, c))
printf("%d,%d,%d 能组成等边三角形\n", a, b, c);
else
printf("%d,%d,%d 不能组成等边三角形\n", a, b, c);
if(is_right(a, b, c))
printf("%d,%d,%d 能组成直角三角形\n", a, b, c);
else
printf("%d,%d,%d 不能组成直角三角形\n", a, b, c);
}
执行效果:
26.判断一个数是否可以拆分成两个素数之和
#include <stdio.h>
#include <math.h>
bool is_prime(int n)
{
int i, k=sqrt(n);
for(i=2; i<=k; i++)
{
if(n % i == 0)
break;
}
if(i <= k)
return false;
else
return true;
}
int main()
{
int n, i, flag = 0;
scanf("%d", &n);
for(i=2; i<=n/2; i++)
{
if(is_prime(i) && is_prime(n-i))
{
printf("%d = %d + %d\n", n, i, n-i);
flag = 1;
}
}
if(flag == 0)
printf("%d 不能拆分成两个素数", n);
return 0;
}
执行效果示例:
27.二进制与十进制相互转换
#include <stdio.h>
#include <math.h>
int BinaryToDecimal(long long n)
{
int ans = 0, i = 0, temp;
while(n != 0)
{
temp = n % 10;
n /= 10;
ans += temp * pow(2, i);
i++;
}
return ans;
}
long long DecimalToBinary(int n)
{
long long ans = 0;
int temp, i = 1;
while(n != 0)
{
temp = n % 2;
n /= 2;
ans += temp * i;
i *= 10;
}
return ans;
}
int main()
{
int m;
long long n;
printf("输入一个二进制数:");
scanf("%lld", &n);
printf("二进制数 %lld 转为十进制:%d\n", n, BinaryToDecimal(n));
printf("输入一个十进制数:");
scanf("%d", &m);
printf("十进制数 %d 转为二进制:%lld\n", m, DecimalToBinary(m));
return 0;
}
执行效果示例:
28.二进制与八进制相互转换
#include <stdio.h>
#include <math.h>
int BinaryToOctal(long long n)
{
int ans = 0, i = 0, temp, dec = 0, j = 1;
while(n != 0)
{
temp = n % 10;
dec += temp * pow(2, i);
n /= 10;
i++;
}
while(dec != 0)
{
temp = dec % 8;
ans += temp * j;
dec /= 8;
j *= 10;
}
return ans;
}
long long OctalToBinary(int n)
{
long long ans = 0;
int temp, i = 0, dec = 0, j = 1;
while(n != 0)
{
temp = n % 10;
dec += temp * pow(8, i);
i++;
n /= 10;
}
while(dec != 0)
{
temp = dec % 2;
dec /= 2;
ans += temp * j;
j *= 10;
}
return ans;
}
int main()
{
int m;
long long n;
printf("输入一个二进制数:");
scanf("%lld", &n);
printf("%lld", n);
printf("二进制数 %lld 转为八进制:%d\n", n, BinaryToOctal(n));
printf("输入一个八进制数:");
scanf("%d", &m);
printf("八进制数 %d 转为二进制:%lld\n", m, OctalToBinary(m));
return 0;
}
执行效果示例:
29.八进制与十进制相互转换
#include <stdio.h>
#include <math.h>
int OctalToDecimal(long long n)
{
int ans = 0, i = 0, temp;
while(n != 0)
{
temp = n % 10;
ans += temp * pow(8, i);
i++;
n /= 10;
}
return ans;
}
int DecimalToOctal(int n)
{
int ans = 0;
int temp, i = 1;
while(n != 0)
{
temp = n % 8;
ans += temp * i;
n /= 8;
i *= 10;
}
return ans;
}
int main()
{
int m;
int n;
printf("输入一个八进制数:");
scanf("%d", &n);
printf("八进制数 %d 转为十进制:%d\n", n, OctalToDecimal(n));
printf("输入一个十进制数:");
scanf("%d", &m);
printf("十进制数 %d 转为八进制:%d\n", m, DecimalToOctal(m));
return 0;
}
执行效果:
30.打印杨辉三角
#include <stdio.h>
#define N 10
int main()
{
int i, j, a[N][N];
for(i=0; i<N; i++)
{
a[i][0] = 1;
a[i][i] = 1;
}
for(i=2; i<N; i++)
{
for(j=1; j<i; j++)
{
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
for(i=0; i<N; i++)
{
for(j=0; j<=i; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
return 0;
}
执行效果:
31.查找数组中的最大/最小元素
#include <stdio.h>
int main()
{
int n;
int i, max, min;
scanf("%d", &n);
int a[n];
for(i=0; i<n; i++)
scanf("%d", &a[i]);
min = a[0], max = a[0];
for(i=1; i< n; i++)
{
max = (a[i] > max) ? a[i] : max;
min = (a[i] < min) ? a[i] : min;
}
printf("最大值为:%d,最小值为:%d", max, min);
return 0;
}
执行效果:
32.矩阵转换
#include <stdio.h>
int main()
{
int n, m, temp, i, j;
printf("输入矩阵的行数和列数:");
scanf("%d %d", &n, &m);
int a[n][m];
int b[m][n];
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
scanf("%d", &a[i][j]);
}
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
b[j][i] = a[i][j];
}
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("%d ", b[i][j]);
if(j == n - 1)
printf("\n");
}
}
return 0;
}
执行效果:
33.统计字符在字符串中出现的次数
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
char ch;
int i, count = 0;
printf("输入字符串:");
scanf("%s", &str);
int len = strlen(str);
printf("请输入要查找的字符:");
getchar();
scanf("%c", &ch);
for(i=0; i<len; i++)
{
if(str[i] == ch)
count++;
}
printf("字符 %c 在输入的字符串中出现的次数为:%d", ch, count);
return 0;
}
执行效果示例:
34.字符串中字母数字等的出现次数
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int letter = 0, digit = 0, space = 0, others = 0;
printf("输入字符串:\n");
gets(str);
int len = strlen(str);
for(int i=0; i<len; i++)
{
if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
letter++;
else if(str[i] >= '0' && str[i] <= '9')
digit++;
else if(str[i] == ' ')
space++;
else
others++;
}
printf("字母:%d,数字:%d,空格:%d,其他字符:%d", letter, digit, space, others);
return 0;
}
执行效果示例:
35.计算两个时间段的差值
#include <stdio.h>
#include <math.h>
int main()
{
int hours_1, minutes_1, seconds_1, hours_2, minutes_2, seconds_2;
int diff_hours, diff_minutes, diff_seconds, temp, time1, time2;
printf("请输入第一个时间(小时 分钟 秒):\n");
scanf("%d %d %d", &hours_1, &minutes_1, &seconds_1);
printf("请输入第二个时间(小时 分钟 秒):\n");
scanf("%d %d %d", &hours_2, &minutes_2, &seconds_2);
time1 = hours_1*60*60 + minutes_1*60 + seconds_1;
time2 = hours_2*60*60 + minutes_2*60 + seconds_2;
temp = abs(time2 - time1);
diff_hours = temp / 3600;
diff_minutes = temp / 60 % 60 ;
diff_seconds = temp % 60;
printf("时间差值为:%d小时%d分%d秒", diff_hours, diff_minutes, diff_seconds);
return 0;
}
执行效果:
36.计算日期是第几天
#include <stdio.h>
int main()
{
int count_days(int month, int day);
bool is_leap(int year);
int year, month, day, days;
printf("请输入日期(年,月,日):");
scanf("%d,%d,%d", &year, &month, &day);
days = count_days(month, day);
if(month > 2 && is_leap(year))
days += 1;
printf("%d-%d-%d是%d年的第%d天", year, month, day, year, days);
return 0;
}
int count_days(int month, int day)
{
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i;
if(month > 1)
{
for(i=0; i<month-1; i++)
day += months[i];
}
return day;
}
bool is_leap(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
执行效果:
37.将字符串写入文件
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];
FILE *fp;
fp = fopen("test_str.txt", "w");
if(fp == NULL)
{
printf("Error!");
exit(0);
}
printf("输入字符串:\n");
fgets(str, (sizeof str / sizeof str[0]), stdin);
fprintf(fp, "%s", str);
fclose(fp);
return 0;
}
执行效果:
38.读取文件
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];
FILE *fp;
fp = fopen("test_str.txt", "r");
if(fp == NULL)
{
printf("Error!");
exit(0);
}
fscanf(fp, "%[^\n]", str);
printf("%s", str);
fclose(fp);
return 0;
}
执行效果:
39.读写文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *in;
FILE *out;
char str[5][10];
int i, j=0, n = 5;
printf("请输入字符串:\n");
for(i=0; i<n; i++)
{
gets(str[i]);
}
if ((in = fopen("test_str2.txt", "w"))== NULL)
{
printf("无法打开文件!");
exit(0);
}
for(i=0; i<n; i++)
{
fputs(str[i], in);
fputs("\n", in);
}
fclose(in);
if ((out = fopen("test_str2.txt", "r"))== NULL)
{
printf("无法打开文件!");
exit(1);
}
printf("从文件中读取的字符串如下:\n");
while(fgets(str[j], n, out)!= NULL)
{
printf("%s", str[j]);
j++;
}
fclose(out);
return 0;
}
执行效果:
#include <stdio.h>
#define N 5
struct Student
{
int num;
char name[20];
float chinese, math, english, physics, chemistry, biology;
float average;
float total;
}stu[N];
int main()
{
void save(void);
int i;
FILE *fp1;
printf("请输入各学生信息及各门课程分数:学号、姓名、语文、数学、英语、物理、化学、生物:\n");
for(i=0; i<N; i++)
{
scanf("%d %s %f %f %f %f %f %f", &stu[i].num, &stu[i].name, &stu[i].chinese, &stu[i].math,
&stu[i].english, &stu[i].physics, &stu[i].chemistry, &stu[i].biology);
stu[i].total = stu[i].chinese + stu[i].math + stu[i].english + stu[i].physics + stu[i].chemistry + stu[i].biology;
stu[i].average = stu[i].total / 6.0;
}
save();
fp1 = fopen("stu_score.txt", "r");
printf("\n学号 姓名 语文 数学 英语 物理 化学 生物 总分 平均分\n");
printf("------------------------------------------------------------------------------\n");
for(i=0; i<N; i++)
{
fread(&stu[i], sizeof(struct Student), 1, fp1);
printf("%d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n", stu[i].num, stu[i].name,
stu[i].chinese, stu[i].math, stu[i].english, stu[i].physics, stu[i].chemistry,
stu[i].biology, stu[i].total, stu[i].average);
}
fclose(fp1);
return 0;
}
void save(void)
{
FILE *fp;
int i;
if((fp = fopen("stu_score.txt", "w")) == NULL)
{
printf("无法打开文件!");
return;
}
for(i=0; i<N; i++)
{
if(fwrite(&stu[i], sizeof(struct Student), 1, fp) != 1)
{
printf("文件写入失败!");
return;
}
}
fclose(fp);
}
执行效果:
40.将一个数插入排好序的数组
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int a[21], i, j, num, t, t1, t2;
srand((int)time(NULL));
for(i=0; i<20; i++)
{
a[i] = rand() % 90 + 10;
}
for(i=0; i<20; i++)
{
for(j=0; j<19-i; j++)
{
if(a[j] > a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
for(i=0; i<20; i++)
{
printf("%d ", a[i]);
}
printf("\n");
printf("输入一个整数:\n");
scanf("%d", &num);
if(num > a[19])
a[20] = num;
else
{
for(i=0; i<20; i++)
{
if(a[i] > num)
{
t1 = a[i];
a[i] = num;
for(j=i+1; j<21; j++)
{
t2 = a[j];
a[j] = t1;
t1 = t2;
}
break;
}
}
}
for(i=0; i<21; i++)
{
printf("%d ", a[i]);
}
return 0;
}
执行效果:
参考:
|