大家好,又见面了,我是你们的朋友全栈君
【注】此题还有一些比较高级的解法,比如求最大公约数的相减法、欧几里德辗转相除法等,有兴趣的同学可以查询相关资料
#include<stdio.h>
//最大公约数
size_t GCD(size_t a, size_t b)
{
size_t gcd;
gcd = a > b ? b : a;
while(gcd > 1)
{
if((a % gcd == 0) && (b % gcd == 0))
return gcd;
gcd--;
}
return gcd;
}
//最小公倍数
size_t LCM(size_t a, size_t b)
{
size_t lcm;
lcm = a > b ? a : b;
while(1)
{
if((lcm % a==0) && (lcm % b==0))
break;
lcm++;
}
return lcm;
}
int main()
{
size_t a, b, result;
printf("请输入两个整数:>");
scanf("%d %d", &a, &b);
result = GCD(a, b);
printf("%d和%d的最大公约数为:%d\n", a, b, result);
result = LCM(a, b);
printf("%d和%d的最小公倍数为:%d\n", a, b, result);
return 0;
}
#include<stdio.h>
#include<math.h>
//x1为第一个根,x2为第二个根
float x1, x2, disc, p, q;
void greater_than_zero(float a, float b)
{
float m = sqrt(disc);
x1 = (-b + sqrt(disc)) / (2 * a);
x2 = (-b - sqrt(disc)) / (2 * a);
}
void equal_to_zero(float a, float b)
{
x1 = x2 = (-b) / (2 * a);
}
void smaller_than_zero(float a, float b)
{
p = -b / (2 * a);
q = sqrt(-disc) / (2 * a);
}
int main()
{
int a, b, c;
printf("请输入 a b c:");
scanf("%d %d %d", &a, &b, &c);
printf("表达式为: %d*x^2+%d*x+%d = 0\n", a, b, c);
disc = b*b - 4 * a*c;
if (disc > 0)
{
greater_than_zero(a, b);
printf("disc>0的根为: x1=%f x2=%f\n", x1, x2);
}
else if (disc == 0)
{
equal_to_zero(a, b);
printf("disc==0的根为:x1=%f x2=%f\n", x1, x2);
}
else
{
smaller_than_zero(a, b);
printf("disc<0的根为:x1=%f+%f x2=%f-%f\n", p, q, p, q);
}
return 0;
}
#include<stdio.h>
#include<stdbool.h>
bool IsPrime(int value)
{
for(int i=2; i<value/2; ++i)
{
if(value % i == 0) //说明除了1和本身之外,还能被其他数整除
return false;
}
return true;
}
int main()
{
int value;
bool flag;
printf("请输入 value :>");
scanf("%d", &value);
flag = IsPrime(value);
if(flag)
printf("%d 是素数.\n", value);
else
printf("%d 不是素数.\n", value);
return 0;
}
进行数组的行列互换,其关键在于数组互换的表达式 ar[i] [j] = ar[j] [i];其次在循环的时候,内层循环不能到达最大列,需要根据此时是第几行的交换来决定循环的次数,否则有可能数组行列交换之后最后又交换回原来的形状了。
#include<stdio.h>
void PrintArray(int ar[3][3])
{
for(int i=0; i<3; ++i)
{
for(int j=0; j<3; ++j)
{
printf("%d ", ar[i][j]);
}
printf("\n");
}
}
void ReverseArray(int ar[3][3])
{
int tmp;
for(int i=0; i<3; ++i)
{
for(int j=0; j<i; ++j)
{
if(i != j) //中间数不发生变化
{
//交换两个数
tmp = ar[i][j];
ar[i][j] = ar[j][i];
ar[j][i] = tmp;
}
}
}
}
int main()
{
int array[3][3] =
{
{
1,2,3},
{
4,5,6},
{
7,8,9}
};
printf("转置前:\n");
PrintArray(array);
//进行数组转置
ReverseArray(array);
printf("转置后:\n");
PrintArray(array);
return 0;
}
#include<stdio.h>
void ReverseString(char str[])
{
int start, end;
char tmp;
start = 0;
end = strlen(str)-1; //字符数组小标从0开始,所以-1
while(start < end)
{
tmp = str[start];
str[start] = str[end];
str[end] = tmp;
start++;
end--;
}
}
int main()
{
char str[100] = {
0};
printf("请输入一个字符串:>");
scanf("%s", str);
printf("原始字符串为:> %s\n", str);
ReverseString(str);
printf("反序字符串为:> %s\n", str);
return 0;
}
#include<stdio.h>
void ConcatStr(char string1[], char string2[], char string[])
{
int i, j;
for (i = 0; string1[i] != '\0'; i++)
string[i] = string1[i];
//找到字符串末尾,继续往后面链接字符串
for (j = 0; string2[j] != '\0'; j++)
string[i + j] = string2[j];
//字符串末尾加上结束符 \0
string[i + j] = '\0';
}
int main()
{
char s1[200] = {
0}, s2[100]= {
0}, s[100] = {
0};
printf("input string1:");
scanf("%s", s1);
printf("input string2:");
scanf("%s", s2);
ConcatStr(s1, s2, s);
printf("\nThe new string is %s\n", s);
return 0;
}
#include<stdio.h>
void cpy(char s[], char c[])
{
int i, j;
for (i = 0, j = 0; s[i] != '\0'; i++)
{
//判断元音字母
if (s[i] == 'a' || s[i] == 'A' || s[i] == 'e' || s[i] == 'E' ||
s[i] == 'i' ||s[i] == 'I' || s[i] == 'o' || s[i] == 'O' ||
s[i] == 'u' || s[i] == 'U')
{
c[j] = s[i];
j++;
}
}
c[j] = '\0';
}
int main()
{
char str[80], c[80];
printf("input string:");
gets(str);
cpy(str, c); //将str中的元音字母拷贝到c中
printf("The vowel letters are:%s\n", c);
return 0;
}
对字符串进行遍历输出,没输出一个字符,后面就跟着输出一个空格,关键点在于如果输出的是最后一个字符,则不能在输出字符,所以要对是否是最后一个字符的输出进行判断。
#include<stdio.h>
void OutString(char str[])
{
int i = 0;
while(str[i] != '\0')
{
printf("%c", str[i]);
if(str[i+1] == '\0') //清除最后一个空格不输出
break;
printf("%c", ' ');
i++;
}
printf("\n");
}
int main()
{
char str[5] = {0};
printf("input four digits:");
scanf("%s", str);
OutString(str);
return 0;
}
#include<stdio.h>
int letter, digit, space, others;
void CountChar(char str[])
{
int i;
for (i = 0; str[i] != '\0'; 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++; //统计其他字符
}
}
int main()
{
char text[80];
printf("input string:\n");
gets(text);
printf("string: %s\n", text);
CountChar(text);
printf("\nletter:%d\ndigit:%d\nspace:%d\nothers:%d\n", letter, digit, space, others);
return 0;
}
#include<stdio.h>
#include<string.h>
void LongestWord(char str[], char word[])
{
int max_len = 0;
int len = 0;
int i = 0;
while(str[i] != '\0')
{
if(str[i] == ' ')
{
str[i] = '\0';
len = strlen(str);
if(len > max_len)
{
max_len = len;
strcpy(word, str);
str = str + len + 1;
}
}
i++;
}
}
int main()
{
char line[100] = {0};
char word[100] = {0};
printf("input one line:\n");
gets(line);
LongestWord(line, word);
printf("The longest word is : %s\n", word);
return 0;
}
#include<stdio.h>
#include<string.h>
void BubbleSort(char str[])
{
int i, j;
char tmp;
int len = strlen(str);
for(i=0; i<len-1; ++i)
{
for(j=0; j<len-i-1; ++j)
{
if(str[j] > str[j+1])
{
tmp = str[j];
str[j] = str[j+1];
str[j+1] = tmp;
}
}
}
}
int main()
{
int i;
char str[11] = {
0};
printf("请输入10个字符:>");
for(i=0; i<10; ++i)
scanf("%c", &str[i]);
BubbleSort(str);
printf("string sorted: %s\n", str);
return 0;
}
#include<stdio.h>
#include<math.h>
float solut(int a, int b, int c, int d)
{
float x = 1, x0, f, f1;
do
{
x0 = x;
f = ((a*x0 + b)*x0 + c)*x0 + d;
f1 = (3 * a*x0 + 2 * b)*x0 + c;
x = x0 - f / f1;
} while (fabs(x - x0) >= 1e-3);
return(x);
}
int main()
{
int a, b, c, d;
printf("input a,b,c,d:");
scanf("%d %d %d %d", &a, &b, &c, &d);
printf("x=%10.7f\n", solut(a, b, c, d));
return 0;
}
#include<stdio.h>
double polya(int n,int x)
{
double result;
if(n == 0)
result = 1;
if(n == 1)
result = x;
if(n>1)
result = ((2*n-1)*x*polya(n-1,x)-(n-1)*polya(n-2,x))/n;
return result;
}
int main()
{
int x,n;
scanf("%d %d", &n, &x);
printf("%.2f\n", polya(n,x));
return 0;
}
①计算每个学生的平均分; ②计算每门课的平均分; ③找出所有50个分数中最高的分数所对应的学生和课程; ④计算平均分方差:
其中,x;为某一学生的平均分。
#include<stdio.h>
#define N 10
#define M 5
float score[N][M];
float a_stu[N], a_cour[M];
int r, c;
//输入学生成绩信息函数
void input_stu(void)
{
int i, j;
for (i = 0; i < N; i++)
{
printf("\ninput score of student%2d:\n", i + 1);
for (j = 0; j < M; j++)
scanf("%f", &score[i][j]);
}
}
//每个学生平均分
void aver_stu(void)
{
int i, j;
float s;
for (i = 0; i < N; i++)
{
for (j = 0, s = 0; j < M; j++)
s += score[i][j];
a_stu[i] = s / 5.0;
}
}
//每门课程平均分
void aver_cour(void)
{
int i, j;
float s;
for (j = 0; j < M; j++)
{
s = 0;
for (i = 0; i < N; i++)
s += score[i][j];
a_cour[j] = s / (float)N;
}
}
//最高分函数
float highest()
{
float high;
int i, j;
high = score[0][0];
for (i = 0; i < N; i++)
for (j = 0; j<M; j++)
if (score[i][j]>high)
{
high = score[i][j];
r = i + 1;
c = j + 1;
}
return(high);
}
//方差函数
float s_var(void)
{
int i;
float sumx, sumxn;
sumx = 0.0;
sumxn = 0.0;
for (i = 0; i < N; i++)
{
sumx += a_stu[i] * a_stu[i];
sumxn += a_stu[i];
}
return(sumx / N - (sumxn / N)*(sumxn / N));
}
int main()
{
int i, j;
float h;
input_stu();
aver_stu();
aver_cour();
printf("\n NO. cour1 cour2 cour3 cour4 cour5 aver\n");
for (i = 0; i < N; i++)
{
printf("\n NO %2d ", i + 1);
for (j = 0; j < M; j++)
printf("%8.2f", score[i][j]);
printf("%8.2f\n", a_stu[i]);
}
printf("\naverage:");
for (j = 0; j < M; j++)
printf("%8.2f", a_cour[j]);
printf("\n");
h = highest();
printf("highest:%7.2f NO. %2d course %2d\n", h, r, c);
printf("variance %8.2f\n", s_var());
return 0;
}
利用二分查找的关键在于数据一定要先有序,所以在查找前我们需要对数据进行排序。
#include<stdio.h>
#define N 10
void input(int num[], char name[N][8])
{
int i;
for (i = 0; i < N; i++)
{
printf("input NO.: ");
scanf("%d", &num[i]);
printf("input name: ");
getchar();
gets(name[i]);
}
}
void sort(int num[], char name[N][8])
{
int i, j, min, templ;
char temp2[8];
for (i = 0; i < N - 1; i++)
{
min = i;
for (j = i; j<N; j++)
if (num[min]>num[j]) min = j;
templ = num[i];
strcpy(temp2, name[i]);
num[i] = num[min];
strcpy(name[i], name[min]);
num[min] = templ;
strcpy(name[min], temp2);
}
printf("\n result:\n");
for (i = 0; i < N; i++)
printf("\n %5d%10s", num[i], name[i]);
}
void search(int n, int num[], char name[N][8])
{
int top, bott, mid, loca, sign;
top = 0;
bott = N - 1;
loca = 0;
sign = 1;
if ((n<num[0]) || (n>num[N - 1]))
loca = -1;
while ((sign == 1) && (top <= bott))
{
mid = (bott + top) / 2;
if (n == num[mid])
{
loca = mid;
printf("NO. %d , his name is %s.\n", n, name[loca]);
sign = -1;
}
else if (n < num[mid])
bott = mid - 1;
else
top = mid + 1;
}
if (sign == 1 || loca == -1)
printf("%d not been found.\n", n);
}
int main()
{
int num[N], number, flag = 1, c;
char name[N][8];
input(num, name);
sort(num, name);
while (flag == 1)
{
printf("\ninput number to look for:");
scanf("%d", &number);
search(number, num, name);
printf("continue ot not(Y/N)?");
getchar();
c = getchar();
if (c == 'N' || c == 'n')
flag = 0;
}
return 0;
}
#include<stdio.h>
size_t HextoDec(char s[])
{
size_t i, n;
n = 0;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] >= '0'&& s[i] <= '9')
n = n * 16 + s[i] - '0';
if (s[i] >= 'a' && s[i] <= 'f')
n = n * 16 + s[i] - 'a' + 10;
if (s[i] >= 'A' && s[i] <= 'F')
n = n * 16 + s[i] - 'A' + 10;
}
return n;
}
int main()
{
size_t result = 0;
char hex[9] = {
0};
printf("input a HEX number:");
scanf("%s", hex);
result = HextoDec(hex);
printf("0x%s = %u\n", hex, result);
return 0;
}
#include<stdio.h>
void Convert(int n)
{
int i;
if ((i = n / 10) != 0)
Convert(i);
putchar(n % 10 + '0');
}
int main()
{
int number;
printf("input an integer: ");
scanf("%d", &number);
printf("output: ");
if (number < 0)
{
putchar('-'); //先输出一个负号'-'
number = -number;
}
Convert(number);
printf("\n");
return 0;
}
#include <stdio.h>
#include<stdio.h>
/* 函数sum_day:计算日期 */
int sum_day(int month, int day)
{
int day_tab[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int i;
for (i = 1; i < month; i++)
day += day_tab[i]; /* 累加所在月之前天数 */
return day;
}
/* 函数leap:判断是否为闰年 */
int leap(int year)
{
int leap;
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
return leap;
}
int main()
{
int year, month, day, days;
printf("input date(year,month,day):");
scanf("%d %d %d", &year, &month, &day);
printf("%d/%d/%d ", year, month, day);
days = sum_day(month, day); /* 调用函数sum_day */
if (leap(year) && month >= 3) /* 调用函数leap */
days = days + 1;
printf("is the %dth day in this year.\n", days);
return 0;
}
C语言程序设计第五版谭浩强 第七章答案
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/204555.html原文链接:https://javaforall.cn