#include
int main()
{
int a = 0;
int b = 0;
int sum = 0;
scanf("%d %d", &a, &b);
sum = a + b;
printf("sum=%d", sum);
return 0;
}
写法一:
//求三个数中的较大值(用函数)
#include
int Max(int a, int b, int c)
{
int max = 0;
max = a > b ? a : b;
max = max > c ? max : c;
return max;
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
int max = 0;
scanf("%d %d %d", &a, &b, &c);
max = Max(a, b, c);
printf("max=%d", max);
return 0;
}
写法二:
#include
int main()
{
int a = 0;
int b = 0;
int c = 0;
int max = 0;
scanf("%d %d %d", &a, &b, &c);
if(a > b)
{
max = a;
}
else
{
max = b;
}
if(c > max)
{
max = c;
}
printf("max=%d", max);
return 0;
}
#include
int main()
{
int i = 1;
int n = 0;
int sum = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
sum = sum * i;
}
/*
while (n)
{
sum = sum * i;
i++;
n--;
}
*/
printf("%d\n", sum);
return 0;
}
n! = (n-1)! * n
#include
int func(int n)
{
if( n == 0)
{
return 1;
}
else
{
return n * func(n - 1);
}
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = func(n);
printf("%d\n", ret);
return 0;
}
#include
#define M 4
int main()
{
int arr[M][2] = { 0 };
int i = 0;
int number = 0;
int score = 0;
int count = 0;
for (i = 0; i < M; i++)
{
scanf("%d %d", &arr[i][0], &arr[i][1]);
}
for (i = 0; i < M; i++)
{
if (arr[i][1] > 80)
{
printf("学号:%d 分数:%d\n", arr[i][0], arr[i][1]);
count++;
}
}
printf("80分以上的共%d人\n", count);
return 0;
}
#include
int main()
{
int i = 0;
for (i = 2000; i <= 2500; i++)
{
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))
{
printf("%d年是闰年\n", i);
}
else
{
printf("%d年不是闰年\n", i);
}
}
return 0;
}
如何求非闰年?
#include
int main()
{
int i = 0;
for (i = 2000; i <= 2500; i++)
{
//方法一:对闰年的要求逐个取反
//if(i % 4 !=0 || i % 100 ==0 && i % 400 != 0)
//方法二:直接对闰年整个取反 !(闰年要求)
if (!((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)))
{
printf("%d年不是闰年\n", i);
}
}
return 0;
}
#include
int main()
{
int i = 0;
//特别注意 一定要有变量是浮点型!!
float flag = 1;
float sum = 0;
for (i = 1; i <= 100; i++)
{
sum += flag / i;
flag = -flag;
}
printf("%f\n", sum);
return 0;
}
#include
int main()
{
int i = 0;
//特别注意 一定要有变量是浮点型!!
float flag = 1;
float sum = 0;
int n = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
int m = i;//记录当前数是几
for (int j = 1; j < i; j++)
{
m = i + m * 10;//算分母
}
sum += flag / m;
flag = -flag;
}
printf("%f\n", sum);
return 0;
}
#include
#include
int main()
{
int i = 0;
int j = 0;
int sum = 0;
for (i = 100; i < 1000; i++)
{
for (j = 2; j < i; j++)
{
if (i % j == 0)
{
break;
}
}
if (j == i)
{
printf("%d ", i);
sum++;
}
}
printf("\n%d\n", sum);
return 0;
}
#include
#include
int main()
{
int i = 0;
int j = 0;
int sum = 0;
for (i = 100; i <1000; i++)
{
int flag = 1;//每次假设i是素数
for (j = 2; j < i; j++)
{
if (i % j == 0)
{
flag = 0;//i不是素数
break;
}
}
if (flag == 1)
{
printf("%d ", i);
sum++;
}
}
printf("\n%d\n", sum);
return 0;
}
#include
int main()
{
int i = 0;
int j = 0;
int sum = 0;
for (i = 100; i < 1000; i++) //优化3:for (i = 101; i < 1000; i += 2)
{ //因为偶数一定不是素数
for (j = 2; j <= sqrt(i); j++)
{
if (i % j == 0)
{
break;
}
}
if (j > sqrt(i))
{
printf("%d ", i);
sum++;
}
}
printf("\n%d\n", sum);
return 0;
}
#include
int main()
{
int arr[10] = { 0 };
int i = 0;
for (i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
int max = arr[0];
//只需从1下标开始
for (i = 1; i < 10; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
printf("max=%d", max);
return 0;
}
#include
void swap(int* n, int* m)
{
int tmp = *n;
*n = *m;
*m = tmp;
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
scanf("%d %d %d", &a, &b, &c);
if (a < b)
swap(&a, &b);
if (a < c)
swap(&a, &c);
if (b < c)
swap(&b, &c);
//a最大,c最小
printf("%d %d %d\n", a, b, c);
return 0;
}
#include
int main()
{
int a = 0;
scanf("%d", &a);
if (0 == a % 3 && 0 == a % 5)
{
printf("%d能被3和5整除\n", a);
}
else
printf("%d不能被3和5整除\n", a);
return 0;
}
最小公倍数=两数的积÷最大公约数
#include
int main()
{
int n = 0;
int m = 0;
int total = 0;
scanf("%d %d", &n, &m);
total = n * m;//两数之积
int min = n < m ? n : m;
while (1)
{
if (n % min == 0 && m % min == 0)
{
break;
}
min--;
}
//此时min为最大公约数
//最小公倍数就等于:两个数的乘积/最大公约数
printf("%d\n", min);
printf("%d\n", total / min);
}
#include
int main()
{
int n = 0;
int m = 0;
int total = 0;
scanf("%d %d", &n, &m);
total = n * m;//两数之积
while (n % m)
{
int tmp = n % m;
n = m;
m = tmp;
}
//此时m为最大公约数
//最小公倍数就等于:两个数的乘积/最大公约数
printf("%d\n", m);
printf("%d\n", total / m);
#include
int Fun(int n, int m)
{
if (n > m)
{
return Fun(m, n - m);
}
else if (n < m)
{
return Fun(n, m - n);
}
//两数相等
else
return n;
}
int main()
{
int n = 0;
int m = 0;
int total = 0;
scanf("%d %d", &n, &m);
total = n * m;//两数之积
int ret = Fun(n, m);
printf("%d\n", ret);
printf("%d\n", total /ret);
return 0;
}
#include
int Fun(int n, int m)
{ // while (n % m)
if (m == 0) //{
return n; // int tmp = n % m;
else // n = m;
return Fun(m, n % m); // m = tmp;
//}
//printf("%d\n", m);
}
int main()
{
int n = 0;
int m = 0;
int total = 0;
scanf("%d %d", &n, &m);
total = n * m;//两数之积
int ret = Fun(n, m);
printf("%d\n", ret);
printf("%d\n", total / ret);
return 0;
}
#include
#include
int main()
{
double a, b, c;
double flag = 0;
double x1, x2;
scanf("%lf %lf %lf", &a, &b, &c);
flag = b * b - 4 * a * c;
if (flag > 0)
{
x1 = (-b + sqrt(flag)) / (2 * a);
x2 = (-b - sqrt(flag)) / (2 * a);
printf("方程有两个不相等的实根:x1=%f,x2=%f\n", x1, x2);
}
else if (flag < 0)
{
printf("方程无解\n");
}
else
{
x1 = x2 = -b / (2 * a);
printf("方程有两个不相等的实根:x1=x2=%f\n", x1);
}
return 0;
}
#include
int main()
{
float temperature = 0;
float Ht = 0;
scanf("%f", &temperature);
Ht = ( 5.0 / 9) * (temperature - 32);
printf("%f\n", Ht);
return 0;
}
#include
int main()
{
char ch1 = '0';
char ch2 = '0';
scanf("%c", &ch1);
//大写转小写
if (ch1 >= 97)
{
ch2 = ch1 - 32;
}
//小写转大写
else
{
ch2 = ch1 + 32;
}
printf("%c\n", ch2);
return 0;
}
#include
#include
int main()
{
double a = 0;
double b = 0;
double c = 0;
double area = 0;
double s = 0;
scanf("%lf %lf %lf", & a, & b, & c);
s = (a + b + c) / 2;
//能构成三角形 (任意两边之和大于第三边)
if (a + b > c || a + c > b || c + b > a)
{
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("area= %lf\n", area);
}
else
{
printf("不是三角形!\n");
}
return 0;
}
int main()
{
char ch = 0;
while ((ch = getchar()) != '\n')
{
//如果是字母
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
{
//是最后四个字母,就减22
if (ch >= 'w' && ch <= 'z' || ch >= 'W' && ch <= 'Z')
{
ch = ch - 22;
}
//加4
else
{
ch = ch + 4;
}
printf("%c", ch);
}
//是数字
else
printf("%c", ch);
}
return 0;
}
#include
int main()
{
double r, heigh, circle, area, superarea, v1, v2;
double pi = 3.14;
scanf("%lf %lf", &r, &heigh);
printf("圆的周长:%lf\n", 2 * pi * r);
printf("圆的面积:%lf\n", pi * r * r);
printf("圆球的表面积:%lf\n", 4 * pi * r * r);
printf("圆球的体积:%lf\n", 3.0 / 4 * pi * r * r * r);
printf("圆柱的体积:%lf\n", pi * r * r * heigh);
return 0;
}
#include
#include
int main()
{
int num = 0;
int sq = 0;
do
{
printf("请输入一个小于1000的正整数:");
scanf("%d", &num);
} while (num >1000 || num < 0);
sq = sqrt(num);
printf("%开平方根的结果是:%d\n", sq);
return 0;
}
#include
int main()
{
int x = 0;
int y = 0;
scanf("%d", &x);
if (x < 1)
{
y = x;
printf("x=%d y=x=%d\n",x, y);
}
if (x >= 1 && x < 10)
{
y = 2 * x - 1;
printf("x=%d y=2x-1=%d\n", x, y);
}
if (x >= 10)
{
y = 3 * x - 11;
printf("x=%d y=3x-11=%d\n", x, y);
}
return 0;
}
90分以上为 :A ,80-89为:B,70-79为:C,60-69为:D,60分以下为:E
#include
int main()
{
int score = 0;
scanf("%d", &score);
switch (score / 10)
{
case 9:
printf("A\n");
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
case 6:
printf("D\n");
break;
default:
printf("E\n");
break;
}
return 0;
}
①求出它是几位数;
②分别输出每一位数字;
③按逆序输出各位数字,例如原数为321,应输出123
#include
//输出每一位
void fun(int n)
{
if (n > 9)
{
fun(n / 10);
}
printf("%d ", n % 10);
}
int main()
{
int num = 0;
int count = 1; //输入的至少是一位数吧
printf("输入一个不多于5位的正整数:");
scanf("%d", &num);
//输出位数
int tmp = num;
while (tmp / 10)
{
count++;
tmp = tmp / 10;
}
printf("共有%d位数\n", count);
//输出每一位,递归
printf("每一位数如下:");
fun(num);
printf("\n");
//逆序输出
printf("逆序输出:");
tmp = num;
while (tmp)
{
printf("%d ", tmp % 10);//输出个位
tmp = tmp / 10; //去除个位
}
return 0;
}
与第9题相似
int main()
{
for (int i = 9; i > 0; i--)
{
for (int j = 1; j <= i; j++)
{
printf("%d*%d=%2d ", j, i, j * i);
}
printf("\n");
}
return 0;
}
int main()
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
printf("%d*%d=%2d ", j, i, j * i);
}
printf("\n");
}
}
int main()
{
for (int i = 9; i>0; i-- )
{
for (int k = 0; k < 9-i; k++)
{
printf("\t");
}
for (int j = 1; j <= i; j++)
{
printf("%2d*%2d=%2d", j, i, i * j);
}
printf("\n");
}
return 0;
}
int main()
{
for (int i = 1; i <= 9; i++)
{
for (int k = 0; k < 9 - i; k++)
{
printf("\t");
}
for (int j = 1; j <= i; j++)
{
printf("%2d*%2d=%2d",i, j, i*j);
}
printf("\n");
}
return 0;
}
#include
int main()
{
int sum = 0;
int i = 1;
/*for (i = 1; i <= 100; i++)
{
sum += i;
}*/
while (i <= 100)
{
sum += i;
i++;
}
printf("%d\n", sum);
return 0;
}
#include
#define M 100000
int main()
{
double money = 0;
double sum = 0;
double ave = 0;
int i = 0;
for (i = 1; i <= 1000; i++)
{
scanf("%lf", &money);
sum += money;
if (sum >= M)
{
break;
}
}
ave = sum / i; //注意此处是i
printf("人数:%d,平均每人捐:%lf\n", i, ave);
return 0;
}
#include
int main()
{
int i = 0;
//int count = 0;
for (i = 100; i <= 200; i++)
{
if (i % 3 == 0)
{
continue;
}
else
{
printf("%d ", i);
//count++;
}
//5个数一行
/*if (count % 5 == 0)
{
printf("\n");
}*/
}
return 0;
}
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
#include
int main()
{
int i = 0;
int j = 0;
for (i = 1; i <= 4; i++) //几行
{
for (j = 1; j <= 5; j++) //几列
{
printf("%2d ", i * j);
}
printf("\n");
}
return 0;
}
#include
#include
int main()
{
double pi = 0;
double den = 1;
double term = 1.0;
int flag = 1;
while (fabs(term) >= 1e-6)
{
pi = pi + term; //累加
den = den + 2; //分母
flag = -flag;
term = flag / den; //某一项
}
pi = 4 * pi;
printf("pi=%lf\n", pi);
return 0;
}
#include
int main()
{
int f1 = 1;
int f2 = 1;
printf("%12d%12d", f1, f2);
//int i = 3;
int i = 0;
int f3 = 0;
for (i = 1; i <= 38; i++)
{
f3 = f1 + f2;
printf("%12d", f3);
f1 = f2;
f2 = f3;
}
/*while (i <= 40)
{
f3 = f1 + f2;
printf("%12d", f3);
f1 = f2;
f2 = f3;
i++;
}*/
return 0;
}
数组
int main()
{
int arr[40] = { 1,1 };
int i = 0;
for (i = 2; i < 40; i++)
{
arr[i] = arr[i - 1] + arr[i - 2];
}
for (i = 0; i < 40; i++)
{
printf("%12d", arr[i]);
//为了避免0的时候换行
if ((i+1) % 5 == 0)
{
printf("\n");
}
}
return 0;
}
int fib(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return fib(n - 1) + fib(n - 2);
}
}
#include
int main()
{
int f1 = 1;
int f2 = 1;
printf("%12d%12d", f1, f2);
int i = 0;
for (i = 3; i <= 40; i++)
{
int ret = fib(i);
printf("%12d", ret);
}
return 0;
}
一次求两个数
#include
int main()
{
int f1 = 1;
int f2 = 1;
int i = 0;
for (i = 1; i <= 20; i++)
{
printf("%12d%12d", f1, f2);
f1 = f1 + f2;
f2 = f2 + f1;
}
return 0;
}
#include
int main()
{
char ch = 0;
int character = 0;
int number = 0;
int spacing = 0;
int other = 0;
while ((ch = getchar()) != '\n')
{
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
{
character++;
}
else if (ch >= '0' && ch <= '9')
{
number++;
}
else if (ch == ' ') //也可写成ch == 32(空格的ascll码为32)
{
spacing++;
}
else
other++;
}
printf("character = %d, number = %d, spacing = %d, other = %d\n", character, number, spacing, other);
return 0;
}
int main()
{
int a = 0;
int n = 0;
scanf("%d %d", &a, &n);
int count = 0;
int sum = 0;
int tmp = a;
while (count < n)
{
printf("%d ", a);
sum += a;
a = a * 10 + tmp; //2*10+2 22*10+2 222*10+2
count++;
}
printf("\nsum = %d\n", sum);
return 0;
}
int main()
{
int n = 0;
scanf("%d", &n);
int sum = 0;
for (int i = 1; i <= n; i++)
{
int ret = 1;
for (int j = 1; j <= i; j++)
{
//求一个数的阶乘
ret = ret * j;
}
//把每个数的阶乘相加
sum += ret;
}
printf("%d\n", sum);
return 0;
}
#include
int main()
{
int n = 0;
scanf("%d", &n);
int sum = 0;
int ret = 1;
for (int i = 1; i <= n; i++)
{
ret = ret * i;
sum += ret;
}
//int a = 1;
//while (n)
//{
// ret = ret * a; //n! = (n-1)! * n
// a++;
// sum += ret;
// n--;
//}
printf("%d\n", sum);
return 0;
}
#include
int main()
{
int i = 0;
int a, b, c ,sum;
for (i = 100; i < 1000; i++)
{
a = i / 100;
b = i / 10 % 10;
c = i % 10;
sum = a * a * a + b * b * b + c * c * c;
if (sum == i)
{
printf("%d ", i);
}
}
return 0;
}
#include
int main()
{
int i = 0;
for (i = 1; i <= 1000; i++)
{
int sum = 0;
int j = 0;
for (j = 1; j < i; j++)
{
if (i % j == 0)
{
sum += j;
}
}
if (sum == i)
{
printf("%d is factors are ", i);
for (j = 1; j < i; j++)
{
if (i % j == 0)
{
printf("%d ", j);
}
}
printf("\n");
}
}
return 0;
}
2/1+3/2+5/3+8/5+13/8…
#include
int main()
{
float a = 2;
float b = 1;
float sum = 0;
float tmp = 0;
int i = 0;
for (i = 1; i <= 20; i++)
{
sum += a / b;
tmp = a + b;
b = a;
a = tmp;
}
printf("%f\n", sum);
}
#include
int main()
{
float sum = 100;
float h = sum / 2;
for (int i = 2; i <= 10; i++)
{
sum += h * 2;//第n次落地经过的m数
h = h / 2; //接下来反弹多高
}
printf("10次落地共经历%fm\n", sum);
printf("10次反弹%fm\n", h);
return 0;
}
#include
int main()
{
int x1 = 0;
int x2 = 1;
int day = 9;
while (day)
{
x1 = (x2 + 1) * 2; //第一天的桃子是第二天桃子加一后的两倍 例:100 吃51 ,剩49
x2 = x1;
day--;
}
printf("共有:%d个\n", x1);
return 0;
}
#include
int main()
{
int n = 0;
//输入打印几行
scanf("%d", &n);
int i = 0;
//打印上半部分
for (i = 0; i < n; i++)
{
//打印空格
int j = 0;
for (j = 0; j < n -1- i; j++)
{
printf(" ");
}
//打印**
for (j = 0; j < 2 * i + 1; j++)
{
printf("*");
}
printf("\n");
}
//打印下半部分
for (i = 0; i < n - 1; i++)
{
//打印空格
int j = 0;
for (j = 0; j <= i; j++)
{
printf(" ");
}
//打印*
for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
int main()
{
int arr[10] = { 1,4,8,3,5,0,2,7,9,10 };
int i = 0;
//一共比较多少趟
for (i = 0; i < 10; i++)
{
int j = 0;
//每趟比较几次
for (j = 0; j < 10 - 1 - i; j++)
{
//从小到大排列
if (arr[j] > arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
//双向冒泡排序
void d_bubble_sort(int arr[], int num)
{
int left = 0;
int right = num - 1;
//优化,如果数组已经有序,就跳出循环
int flag = 0;
while (left < right)
{
flag = 1;
//从左向右找最大
for (int i = left; i < right; i++)
{
//前>后,交换
if (arr[i] > arr[i + 1])
{
flag = 0;
int tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
//优化,如果遍历了一遍数组,没有发生交换,那就说明数组已经有序了
if (flag == 1)
{
break;
}
right--; //最大值已放在右侧
//从右向左找最小
for (int j = right; j > left; j--)
{
//前>后,交换
if (arr[j] < arr[j - 1])
{
int tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
}
}
left++;//最小值放在左侧
}
}
int main()
{
int arr[10] = { 5,3,7,9,1,2,4,8,6,10 };
int arr2[10] = { 1,2,3,4,5,6,7,8,9,0 };
d_bubble_sort(arr2,10);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
int main()
{
int arr[10] = { 1,4,8,3,5,0,2,7,9,10 };
int i = 0;
for (i = 0; i < 10; i++)
{
int j = 0;
int min = i;
//将i后的数进行比较
for (j = i + 1; j < 10; j++)
{
if (arr[min] > arr[j])
{
min = j; //找对最小数的下标
}
}
//将数中的最小值与第一个数交换
int tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr1[2][3] = { 1,2,3,4,5,6 };
int arr2[3][2] = { 0 };
int i = 0;
int j = 0;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", arr1[i][j]);
arr2[j][i] = arr1[i][j];
}
printf("\n");
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", arr2[i][j]);
}
printf("\n");
}
return 0;
}
int main()
{
int arr[3][4] = { 1,2,3,4,9,8,7,6,-1,-5,-8,-4 };
int i = 0;
int j = 0;
int row = 0;
int col = 0;
int max = arr[0][0];
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
if (max < arr[i][j])
{
max = arr[i][j];
row = i;
col = j;
}
}
}
printf("max=%d 下标位:%d,%d\n", max, row, col);
return 0;
}
int main()
{
char str[100] = { 0 };
gets(str);
int num = 0;
int word = 0;
int i = 0;
for (i = 0; str[i] != '\0'; i++)
{
char c = str[i];
if (c == ' ') //是空格,则说明当前位置,单词还没开始,或者刚结束
{
word = 0;
}
else if (word == 0) //当前位置不是空格是字符,并且没有操作过,则是一个单词的开始
{
word = 1;
num += word;
}
//如果该位置不是空格,并且前面不是空格,则说明它属于一个单词,不需要计算
}
printf("%d\n", num);
return 0;
}
#include
#include
int main()
{
char arr[3][20];
char str[20];
int i = 0;
for (i = 0; i < 3; i++)
{
gets(arr[i]);
}
if (strcmp(arr[0], arr[1]) > 0)
{
strcpy(str, arr[0]);
}
else
{
strcpy(str, arr[1]);
}
if (strcmp(arr[2], str) > 0)
{
strcpy(str, arr[2]);
}
printf("%s\n", str);
return 0;
}
#include
//筛选法求1-100的素数
int main()
{
int arr[101] = { 0,0 };//1不是素数,可以直接设置为0
int i = 0;
for (i = 2; i <= 100; i++)
{
arr[i] = i;
}
//用2 - 99的数去除
for (i = 2; i < 100; i++)
{
int j = 0;
//被除数是3-100
for (j = i + 1; j <= 100; j++)
{
if (arr[j] % i == 0)
{
arr[j] = 0;
}
}
}
int count = 0;
for (i = 0; i < 101; i++)
{
if (arr[i] != 0)
{
printf("%d\t", arr[i]);
count++;
}
if (count == 10)
{
printf("\n");
count = 0;
}
}
return 0;
}
int main()
{
int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };
int sum = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
//横、纵坐标相等则是正对角线
if (i == j)
{
sum += arr[i][j];
}
}
}
printf("%d\n", sum);
return 0;
}
int main()
{
int arr[3][3] = { 1,2,3,4,5,6,7,8,9 };
int sum = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
//横、纵坐标的和等于:行数/列数减一
if (i+j == 3-1)
{
sum += arr[i][j];
}
}
}
printf("%d\n", sum);
return 0;
}
#include
int main()
{
int arr[11] = { 1,2,3,4,5,6,7,8,9,10 };
int insert = 0;
scanf("%d", &insert);
int i = 0;
//从后往前遍历
for (i = 9; i >= 0; i--)
{
//arr[i] > insert,arr[i]往后移
if (arr[i] > insert)
{
arr[i + 1] = arr[i];
}
else
{
arr[i + 1] = insert;
//插入后,停止遍历
break;
}
}
//如果insert是最小的,那么i就减到了-1
if (i < 0)
{
arr[0] = insert;
}
for (i = 0; i <= 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10};
int left = 0;
int right = sizeof(arr) / sizeof(arr[0]) - 1;
while (left < right)
{
int tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
left++;
right--;
}
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
int main()
{
int arr[10][10] = { 0 };
int i = 0;
int j = 0;
for (i = 0; i < 10; i++)
{
for (j = 0; j <= i; j++)
{
if (j == 0 || i == j)
{
arr[i][j] = 1;
}
else
{
arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
}
}
}
for (i = 0; i < 10; i++)
{
//打印空格
for (j = 0; j < 10 - i; j++)
printf(" ");
for (j = 0; j <= i; j++)
{
printf("%4d", arr[i][j]);
}
printf("\n");
}
return 0;
}
int func(int m, int n)
{
if (m == n || n == 0)
{
return 1;
}
else
return func(m - 1, n) + func(m - 1, n - 1);
}
int main()
{
int line = 0;
scanf("%d", &line);
int i = 0;
int j = 0;
for (i = 0; i < line; i++)
{
for (j = 0; j < line - i; j++)
{
printf(" ");
}
for (j = 0; j <=i; j++)
{
printf("%4d", func(i, j));
}
printf("\n");
}
return 0;
}
鞍点:即该位置上的元素在该行上最大,在该列上最小。
一个数组也可能没有鞍点
int main()
{
int arr[3][3] = { {13,8,7}, {11,12,5}, {14,6,3} };
int i = 0;
int j = 0;
int flag = 1; //假设该数组有鞍点
for (i = 0; i < 3; i++)
{
//先假设每行第一个数最大
int max_min = arr[i][0];
int col = 0;//记下该数是哪一列
for (j = 0; j < 3; j++)
{
//如果max_min不是该行最大的,就换
if (arr[i][j] > max_min)
{
max_min = arr[i][j];
col = j;
}
}
//再遍历每一行的col列,比较是否是该列最小
int k = 0;
for (k = 0; k < 3; k++)
{
if (arr[k][col] < max_min)
{
flag = 0;
break; //有比该数小的数,则该数不是鞍点
}
}
if (flag == 1)
{
printf("该数组的鞍点是:arr[%d][%d]=%d\n", i, col, max_min);
break;
}
}
if (flag == 0)
{
printf("该数组没有鞍点\n");
}
return 0;
}
int main()
{
int arr[15] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
int input = 0;
scanf("%d", &input);
int left = 0;
int right = sizeof(arr) / sizeof(arr[0]) - 1;
int flag = 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (input == arr[mid])
{
printf("找到了,下标是:%d\n", mid);
flag = 0;
break;
}
else if (input < arr[mid])
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
//if (left > right)
if (flag)
{
printf("找不到\n");
}
return 0;
}
int main()
{
char arr[3][80];
int upc = 0;
int lowc = 0;
int dig = 0;
int spa = 0;
int other = 0;
int i = 0;
for (i = 0; i < 3; i++)
{
printf("输入第%d行:", i+1);
gets(arr[i]);
int j = 0;
for (j = 0; j < 80 && arr[i][j] != '\0'; j++)
{
if (arr[i][j] >= 'A' && arr[i][j] <= 'Z')
{
upc++;
}
else if (arr[i][j] >= 'a' && arr[i][j] <= 'z')
{
lowc++;
}
else if (arr[i][j] >= '0' && arr[i][j] <= '9')
{
dig++;
}
else if (arr[i][j] == ' ')
{
spa++;
}
else
{
other++;
}
}
}
printf("大写:%d,小写:%d,数字:%d,空格:%d, 其它:%d\n", upc, lowc, dig, spa, other);
return 0;
}
int main()
{
char str1[20] = { "hello-" };
char str2[20] = { "world" };
int i = 0;
int j = 0;
while (str1[i] != '\0')
{
i++;
}
while (str2[j] != '\0')
{
str1[i] = str2[j];
i++;
j++;
}
str2[j] = '\0';
printf("%s\n", str1);
return 0;
}
char* my_strcat(char* dest, char* src)
{
//
char* ret = dest;
while (*dest != '\0')
{
dest++;
}
//第一种写法
while (*src != '\0')
{
*dest = *src;
dest++;
src++;
}
*dest = '\0';
//第二种写法
/*while (*dest++ = *src++)
{
;
}*/
return ret;
}
int main()
{
char str1[20] = "hello-";
char* str2 = "world";
char* ret = my_strcat(str1, str2);
printf("%s\n", ret);
return 0;
}
int main()
{
char str1[20] = { 0 };
char str2[20] = { 0 };
gets(str1);
gets(str2);
int i = 0;
int j = 0;
int ret = 0;
while(str1[i] == str2[i] && str1[i] != '\0')
{
i++;
}
if (str1[i] == str2[i] && str1[i] == '\0')
{
ret = 0;
}
else
{
ret = str1[i] - str2[i];
}
printf("%d\n", ret);
return 0;
}
int my_strcmp(char* str1, char* str2)
{
while (*str1 == *str2 )
{
if (*str1 == '\0')
{
return 0;
}
str1++;
str2++;
}
return *str1 - *str2;
}
int main()
{
char str1[20] = { 0 };
char str2[20] = { 0 };
gets(str1);
gets(str2);
int ret = my_strcmp(str1, str2);
printf("%d\n", ret);
return 0;
}
1.数组
#include
#include
int main()
{
char str1[20] = { 0 };
char str2[20] = { 0 };
gets(str2);
int len = strlen(str2);
for (int i = 0; i <= len; i++)
{
str1[i] = str2[i];
}
printf("%s\n", str1);
return 0;
}
void my_strcpy(char* dest, char* src)
{
while (*dest = *src)
{
dest++;
src++;
}
}
int main()
{
char str1[20] = { 0 };
char str2[20] = { 0 };
gets(str2);
my_strcpy(str1, str2);
printf("%s\n", str1);
return 0;
}
#include
int Max2(int x, int y)
{
return x > y ? x : y;
}
int Max_1(int a, int b, int c, int d)
{
int m = 0;
m = Max2(a, b);
m = Max2(m, c);
m = Max2(m, d);
return m;
}
int main()
{
int a, b, c, d, max;
scanf("%d %d %d %d", &a, &b, &c, &d);
max = Max_1(a,b,c,d);
printf("%d\n",max);
return 0;
}
#include
void move(char x, char y)
{
printf("%c--->%c\n", x, y);
}
void hanoi(int n, char A, char B, char C)
{
//A盘只剩一个,直接移动到C盘
if (n == 1)
{
move(A, C);
}
else
{
//n-1个,从A盘借助C移到B盘
hanoi(n - 1, A, C, B);
//第n个从A盘移动到C盘
move(A, C);
//n-1个,从B盘借助A移到C盘
hanoi(n - 1, B, A, C);
}
}
int main()
{
int n = 0;
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
return 0;
}
//全局变量
int Max = 0;
int Min = 0;
float average(int arr[], int n)
{
int i = 0;
float sum = 0;
Max = Min = arr[0];
for (i = 0; i < n; i++)
{
if (arr[i] > Max)
{
Max = arr[i];
}
else if (arr[i] < Min)
{
Min = arr[i];
}
sum += arr[i];
}
return sum / n;
}
int main()
{
int arr[10] = { 0 };
int i = 0;
for (i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
float ave = average(arr, 10);
printf("max=%d min=%d ave=%f\n", Max, Min, ave);
return 0;
}
void Delete(char c[], char ch)
{
int i = 0;
int j = 0;
for (i = 0; c[i] != '\0'; i++)
{
if (c[i] != ch)
{
c[j] = c[i]; // 自己放自己里面
j++;
}
}
c[j] = '\0';
}
int main()
{
char arr[] = "I am student";
char ch = 0;
printf("%s\n", arr);
scanf("%c", &ch);
Delete(arr, ch);
printf("%s\n", arr);
}
void Delete(char* c, char ch)
{
char* point = c;
while (*point != '\0')
{
if (*point != ch)
{
//当前字符不是要删除的字符,就放进c中
*c = *point;
//放完后c++
c++;
}
//1.放进c中后,point也++
//2.是要删除的字符,直接跳过
point++;
}
*c = '\0';//最后*c的位置放上\0
}
int main()
{
char arr[] = "I am student";
char ch = 0;
printf("%s\n", arr);
scanf("%c", &ch);
Delete(arr, ch);
printf("%s\n", arr);
}
#include
#include
void reverse(char c[], int left, int right)
{
while (left <= right)
{
char tmp = c[left];
c[left] = c[right];
c[right] = tmp;
left++;
right--;
}
}
void Print(char arr[])
{
int i = 0;
for (i = 0; arr[i] != '\0'; i++)
{
printf("%c", arr[i]);
}
printf("\n");
}
int main()
{
char arr[] = "abcdef";
int start = 0;
int end = strlen(arr)-1;
Print(arr);
reverse(arr, start, end);
Print(arr);
return 0;
}
#include
#include
void func(char str[])
{
//0123 4
//3689 \0
//01234567 8
//3 6 8 9 \0
int i = 0;
//第一个数字不动
//别忘记\0
for (i = strlen(str); i > 0; i--)
{
str[2 * i] = str[i];
str[2 * i - 1] = ' ';
}
printf("%s", str);
}
int main()
{
char str[10];
scanf("%s", &str);
func(str);
return 0;
}
int judge_alpha(char ch)
{
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
{
return 1;
}
else
{
return 0;
}
}
int TheLongestString(char string[], int len)
{
int i = 0;
int length = 0;
int end_len = 0;
int flag = 1;//假设该位置是一个单词的开始
int start_point = 0;
int place = 0;
for (i = 0; i <= len; i++)
{
//是字母,计数
if (judge_alpha(string[i]))
{
//是一个单词的开始,记录开始位置
if (flag)
{
start_point = i;
flag = 0;
}
//记录完位置,记录长度
//不是一个单词的开始,那就说明属于这个单词,计算该单词的长度
length++;
}
//不是字符:单词结束
else
{
flag = 1;
if (length > end_len)
{
end_len = length;
length = 0; //置为0,继续记录下一个单词的长度
}
}
}
return start_point;
}
int main()
{
char str[20] = { 0 };
gets(str);
int len = strlen(str);
int ret = TheLongestString(str, len);
int i = 0;
//此处也可以定义全局变量接最长字符串的长度,就不需要调用judeg_alpha函数了
for (i = ret; judge_alpha(str[i]); i++)
{
printf("%c", str[i]);
}
return 0;
}
#include
#define M 2
#define N 5
//计算每个人的平均分
void student_ave(float arr[M][N], float ave[N])
{
int i = 0;
for (i = 0; i < M; i++)
{
float sum = 0;
int j = 0;
for (j = 0; j < N; j++)
{
sum += arr[i][j];
}
ave[i] = sum / N;
printf("Num %d: average score = %.2f\n", i+1, ave[i]);
}
printf("\n");
}
//计算每门课程平均分
void lesson_ave(float arr[M][N])
{
int i = 0;
int j = 0;
for (i = 0; i < N; i++)
{
float sum = 0;
for (j = 0; j < M; j++)
{
sum += arr[j][i];
}
printf("lesson%d average:%.2f\n", i + 1, sum / M);
}
printf("\n");
}
//找出所有课程中最大的分数及其学生、课程名
void findmax(float arr[M][N])
{
float max = arr[0][0];
int i = 0;
int j = 0;
int student = 0;
int course = 0;
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
if (arr[i][j] > max)
{
max = arr[i][j];
student = i; //记录名字
course = j; //记录课程
}
}
}
printf("max = %.2f student = %d course = %d\n", max, student+1, course+1);
}
//求平均分的方差
void s_s(float ave[N])
{
float sum_s = 0.0;
float sum = 0.0;
for (int i = 0; i < N; i++)
{
sum_s += ave[i] * ave[i];
sum += ave[i];
}
printf("方差是:%.2f\n", (sum_s / N) - ((sum / N) * (sum / N)));
}
int main()
{
float arr[M][N] = { 0 };
float ave_score[N] = { 0 };
int i = 0;
for (i = 0; i < M; i++)
{
int j = 0;
for (j = 0; j < N; j++)
{
scanf("%f", &arr[i][j]);
}
}
//计算每个学生平均分
student_ave(arr, ave_score);
lesson_ave(arr);
findmax(arr);
s_s(ave_score);
return 0;
}
int convert(char* p)
{
int sum = 0;
while (*p != '\0')
{
if (*p >= 'A' && *p <= 'f')
{
sum = sum * 16 + *p - 'A' + 10;
}
else if (*p >= 'a' && *p <= 'f')
{
sum = sum * 16 + *p - 'a' + 10;
}
else if(*p >= '0' && *p <="9")
{
sum = sum * 16 + (*p - '0') * 16;
}
p++;
}
return sum;
}
int main()
{
char str[10] = {0};
gets(str);
int sum = convert(str);
printf("%d\n", sum);
return 0;
}
void convert(int n)
{
if (n)
{
convert(n / 8);
printf("%d", n % 8);
}
}
int main()
{
int num = 0;
scanf("%d", &num);
convert(num);
return 0;
}
int convert(int n)
{
int sum = 0;
int ret = 0;
if (n)
{
ret = convert(n / 2) * 10 ;
sum = ret + (n % 2);
}
return sum;
}
int main()
{
int num = 0;
scanf("%d", &num);
int ret = convert(num);
printf("%d\n", ret);
return 0;
}
void convert(int n)
{
if (n / 10 != 0)
{
convert(n / 10);
}
putchar(n % 10 + '0');
}
int main()
{
int n = 0;
scanf("%d", &n);
if (n < 0)
{
putchar('-');
n = -n;
}
convert(n);
return 0;
}
int sum_day(int year, int month, int day)
{
int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int sum = day;
for (int i = 1; i < month; i++)
{
sum += arr[i];
}
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
sum++;
}
return sum;
}
int main()
{
int year, month, day;
scanf("%d %d %d", &year, &month, &day);
int ret = sum_day(year, month, day);
printf("%d\n", ret);
return 0;
}
void swap(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
if (a < b)
{
swap(&a, &b);
}
printf("max = %d, min = %d\n", a, b);
return 0;
}
void reverse(int* arr, int num)
{
int n = num / 2;
int* left = arr;
int* right = arr + num - 1;
for (int i = 0; i < n; i++)
{
int tmp = 0;
tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
reverse(arr, 10);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
#include
void swap(char* s1, char* s2)
{
char str[20] = { 0 };
strcpy(str, s1);
strcpy(s1, s2);
strcpy(s2, str);
}
int main()
{
char str1[20] = { 0 };
char str2[20] = { 0 };
char str3[20] = { 0 };
gets(str1);
gets(str2);
gets(str3);
if (strcmp(str1, str2) > 0)
swap(str1, str2);
if (strcmp(str1, str3) > 0)
swap(str1, str3);
if (strcmp(str2, str3) > 0)
swap(str2, str3);
printf("%s %s %s\n", str1, str2, str3);
return 0;
}
void exchange(int* arr, int num)
{
int* max = arr;
int* min = arr;
int i = 0;
for (i = 0; i < num; i++)
{
if (*(arr + i) >= *max)
{
max = arr + i;
}
if (*(arr + i) <= *min)
{
min = arr + i;
}
}
//若最大值就是首元素,为了避免最小值与首元素交换后找不到最大值
if (max == arr)
{
max = min;
}
int tmp = *arr;
*arr = *min;
*min = tmp;
tmp = *(arr + num - 1);
*(arr + num - 1) = *max;
*max = tmp;
}
int main()
{
int arr[10] = { 0 };
int i = 0;
for (i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
exchange(arr, 10);
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
其实就是左旋
void rotate(int* parr, int m, int n)
{
for (int j = 0; j < m; j++)
{
int tmp = *(parr + n - 1);
//旋转一次
for (int i = n - 1; i > 0; i--)
{
*(parr + i) = *(parr + i - 1);
}
//最后一个数放在前面
*parr = tmp;
}
}
int main()
{
int arr[10] = { 0 };
int i = 0;
int m = 3;
for (i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
rotate(arr, m, 10);
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
int main()
{
int arr[100] = { 0 };//定义一个数组,存放每个人喊得数字
int count = 0;//几个人
scanf("%d", &count);
int digit = 1;//要喊得数字
int remain = count; //剩余人数
while (remain > 1) //还未找出赢家
{
for (int i = 1; i <= count; i++)
{
if (*(arr+i) == 3)
{
continue;//当前位置是3,跳过
}
*(arr + i) = digit;
if (digit == 3)//喊完3就要喊1了,又因为下面还有个++,所以赋值为0
{
digit = 0;
remain--;//剩余人数-1
}
digit++;
}
}
for (int j = 1; j <= count; j++)
{
if (*(arr + j) != 3)
{
printf("%d\n", j);
break;
}
}
return 0;
}
int my_strlen(char* str)
{
int count = 0;
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
char* str = "abcdef";
printf("%d\n", my_strlen(str));
return 0;
}
void my_m_strcpy(char* str1, char* str2, int m)
{
int count = 0;
while (count < m - 1)
{
count++;
str1++;
}
while (*str1 != '\0')
{
*str2 = *str1;
str2++;
str1++;
}
*str2 = '\0';
}
int main()
{
char str1[20] = "hello world";
char str2[20] = { 0 };
int m = 0;
scanf("%d", &m);
my_m_strcpy(str1, str2, m);
printf("%s\n", str2);
return 0;
}
int main()
{
int upc = 0;
int lowc = 0;
int digit = 0;
int space = 0;
int other = 0;
char str[50] = { 0 };
gets(str);
char* p = str;
while (*p != '\0')
{
if (*p >= 'A' && *p <= 'Z')
{
upc++;
}
else if (*p >= 'a' && *p <= 'z')
{
lowc++;
}
else if(*p >= '0' && *p <= '9')
{
digit++;
}
else if (*p == ' ')
{
space++;
}
else
{
other++;
}
p++;
}
printf("%d %d %d %d %d\n", upc, lowc, digit, space, other);
return 0;
}
void move1(int* parr)
{
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = i; j < 3; j++)
{
int tmp = 0;
tmp = *(parr + 3 * i + j);
*(parr + 3 * i + j) = *(parr + 3 * j + i);
*(parr + 3 * j + i) = tmp;
}
}
}
void move2(int (*parr)[3])
{
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
//此处应该是j=i,否则就会交换两次,变回原型了
for (j = i; j < 3; j++)
{
int tmp = 0;
tmp = *(*(parr + i) + j);
*(*(parr + i) + j) = *(*(parr + j) + i);
*(*(parr + j) + i) = tmp;
}
}
}
int main()
{
int arr[3][3] = { 1,2,3,1,2,3,1,2,3};
move1(&arr[0][0]); //普通指针,注意传的是第一个元素的地址
//int(*p)[3] = arr; //数组指针
//move2(p);
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 3; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
void operation(int(*p)[5], int row, int col)
{
//一、找最大值
//为了不记录最大值的下标,此处使用指针更加方便
int* mid =&p[row / 2][col/2];
int* max = &p[0][0];
//1.找最大值
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (*max < p[i][j])
{
max = &p[i][j];
}
}
}
//2.找到最大值,交换
int tmp = *mid;
*mid = *max;
*max = tmp;
//二、找四个最小值
//1.记录由四个角的位置
int* corner[4] = { &p[0][0], &p[0][col - 1],&p[row-1][0], &p[row-1][col-1] };
//2.遍历数组,寻找最小值
for (int k = 0; k < 4; k++)//需要寻找四次
{
int* min = mid; //每次都要将最小值初始化为数组的最大值
for (int n = 0; n < row; n++)
{
for (int m = 0; m < col; m++)
{
//3.判断该位置是否是角落位置
int t = 0;
for (t = 0; t < k; t++)
{
//找第0个最小数的时候,k=0,意味着没有角落被交换
if (&p[n][m] == corner[t])
{
break;
}
}
if (t != k) //说明是break出来的,该位置已经被交换过了,不需要交换了
{
continue;//
}
if (*min > p[n][m])
{
min = &p[n][m];
}
}
}
int tmp = *corner[k];
*corner[k] = *min;
*min =tmp;
}
}
int main()
{
int arr[5][5] =
{
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25},
};
operation(arr,5,5);
int i = 0;
for (i = 0; i < 5; i++)
{
int j = 0;
for (j = 0; j < 5; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
void sort(char* *p, int count)
{
int i = 0;
for (i = 0; i < count-1; i++)
{
int j = 0;
for (j = 0; j < count - 1 - i; j++)
{
//由大到小排序
if (strcmp(*(p + j), *(p + j + 1)) < 0)
{
char* tmp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = tmp;
}
}
}
}
int main()
{
int i = 0;
char str[10][20] = { 0 };
char* arr[10] = { 0 };
for (i = 0; i < 10; i++)
{
//指针数组要想初始化,必须先有一个数组
scanf("%s", str[i]);
arr[i] = str[i];
}
sort(arr, 10);
for (i = 0; i < 10; i++)
{
printf("%s\n", arr[i]);
}
return 0;
}
void reverse(int* arr, int count)
{
int* left = arr;
int* right = arr + count - 1;
while (left < right)
{
int tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
int main()
{
int arr[10] = { 0 };
int i = 0;
for (i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
reverse(arr, 10);
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
4个学生,5门课程
void average(int arr[4][5], int row, int col, int course)
{
int i = 0;
int j = 0;
int sum = 0;
for (i = 0; i < row; i++)
{
sum += arr[i][course - 1];
}
printf("课程序号:%d, average = %d\n", course, sum / row);
}
void find_student(int arr[4][5], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
int count = 0;
for (j = 0; j < col; j++)
{
if (arr[i][j] < 60)
{
count++;
}
}
if (count > 2)
{
printf("第%d名学生有两门以上不及格\n", i+1);
}
}
printf("\n");
}
void find_ave_85(int arr[4][5], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
int sum = 0;
int average = 0;
int count = 0;
for (j = 0; j < col; j++)
{
sum += arr[i][j];
if (arr[i][j] > 85)
{
count++;
}
}
average = sum / col;
//平均分大于90或所有全在85以上
if (average > 90 || count == col)
{
printf("第%d名学生优秀\n", i + 1);
}
}
printf("\n");
}
int main()
{
int arr[4][5] = { 0 };
int i = 0;
int j = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 5; j++)
{
scanf("%d", &arr[i][j]);
}
}
//计算某一门课程平均分
average(arr, 4, 5, 1);
//找2门以上不及格学生
find_student(arr, 4, 5);
//找优秀学生
find_ave_85(arr, 4, 5);
return 0;
}
int main()
{
char str[100] = { 0 };
char a[10][100] = { 0 };
gets(str);
char* ptr = str;
int row = 0;
int col = 0;
while (*ptr != '\0')
{
//当前字符是数字,开始读取数字字符串
if (*ptr >= '0' && *ptr <= '9')
{
while (*ptr >= '0' && *ptr <= '9' && *ptr != '\0')
{
//连续存储一个数字字符串
a[row][col] = *ptr;
col++;
ptr++;
}
a[row][col] = '\0';
//来到这里,说明非数字字符或者\0
//1.非数字字符,开始存储下一个数字字符串
row++;
col = 0;
//2.若是\0,跳出循环,停止访问,以防止越界
if (*ptr == '\0')
{
break;
}
}
//不是字符串,指针后移
else
{
ptr++;
}
}
printf("%d个数字\n", row);
for (int i = 0; i < row; i++)
{
printf("%s\n", a[i]);
}
return 0;
}
int main()
{
char* arr[13] = { NULL,"January","February","March","April","May",
"June","July","August","Septembet","October","November","December" };
int month = 0;
scanf("%d", &month);
if (month >= 1 && month <= 12)
{
printf("%s\n", arr[month]);
}
else
{
printf("illegal input\n");
}
return 0;
}