课后答案见:https://blog.csdn.net/Mr_Cat123/article/details/84800905
C实现
#include
int main()
{
int i=2, p=1;
while (i<=5)
{
p = p*i;
i++;
}
printf("answer=%d\n", p);
return 0;
}
ans = 1
for i in range(1,6):
ans*=i
print('answer = ',ans)
python代码:
import numpy as np
import xlrd #读取Excel文件
workbook = xlrd.open_workbook('C:/users/lenovo/desktop/student_score.xlsx')
sheet = workbook.sheet_by_name('Sheet1')
data_name = sheet.col_values(0)
data_st_ID = sheet.col_values(1)
data_st_score = sheet.col_values(2)
n = data_st_ID #学号
g = data_st_score #分数
itera = len(data_name) #人数
for i in range(1,itera):
if g[i] >= 80:
print('学号是:%d,分数是:%.2f'%(n[i],g[i]))
leap_year = []
for year in range(2000,2501):
if year%4 == 0:
if year%100 != 0:
leap_year.append(year)
elif year%400 == 0:
leap_year.append(year)
elif year%400 == 0:
leap_year.append(year)
上面的程序可以通过布尔算符表示如下:
leap_year = []
for year in range(2000,2501):
if (year%4 == 0 and year%100 != 0) or (year%400 == 0):
leap_year.append(year)
C++/C程序
#include
int main()
{
int year;
year = 2000;
while (year <= 2500)
{
if (year % 4 == 0)
if (year % 100 != 0)
printf("%d是闰年", year);
else
if (year % 400 == 0)
printf("%d是闰年", year);
year += 1;
}
return 0;
}
通过布尔算符改为:
#include
int main()
{
int year;
year = 2000;
while (year <= 2500)
{
if ((year % 4 == 0 && year % 100 != 0) || (year%400==0))
printf("%d是闰年", year);
year += 1;
}
return 0;
}
python代码
sum = 1
sign = 1
for i in range(2,101):
sign *= (-1)
sum+=sign/i
print(sum)
#include
int main()
{
int sign=1;
double sum_ans = 1, i = 2.0;
while (i <= 100)
{
sign = (-1)*sign;
sum_ans = sum_ans + sign / i;
i=i+1;
}
printf("%f\n", sum_ans);
return 0;
}
C++和C相比python比较坑的是,一定要声明sum_ans和i为double,否则结果是1,有时候忘记这个,那么就等着哭泣吧。
#include
#include
int main()
{
float a, b,t;
printf("Please enter two numbers:");
scanf_s("%f %f", &a, &b);
if (a > b)
{
t = a;
a = b;
b = t;
}
printf("%6.1f\n%6.1f\n", a, b);
return 0;
}
如果是输入的多个数,可以使用algorithm中的sort函数。如下:
#include
#include
#include
using namespace std;
int main()
{
float a[3];
printf("Please enter three numbers:");
scanf_s("%f %f %f", &a[0], &a[1],&a[2]);
sort(a, a + 3);
printf("%6.1f\n%6.1f\n%6.1f\n", a[0],a[1],a[2]);
return 0;
}
注意:上面使用了sort函数,因此在预处理那儿要加入算法库algorithm,同时,由于不同库之间会有一些标识符等的冲突,于是要使用using namespace std,即命名空间是标准命名空间。
#include
#include
#include
using namespace std;
int main()
{
char ch;
scanf_s("%c", &ch,sizeof(ch));
ch = ('A' <= ch <= 'Z') ? (ch + 32) : ch;
printf("%c\n", ch);
return 0;
}
#include
#include
#include
#define SUM 10000
using namespace std;
int main()
{
clock_t timeStart, timeEnd;
timeStart = clock();
int i=0;
float n = 1.0;
double pi = 0.0;
int sign = 1;
for (; fabs(1.0 / n) >= 1e-6; i++)
{
pi = pi + sign / n;
sign = -sign;
n = n + 2;
}
timeEnd = clock();
printf("%f\n%d\n", 4*pi,i);
printf("time interval=%.4f\n", (float)(timeEnd - timeStart) / CLOCKS_PER_SEC);
return 0;
}
这里,当将1e-6改成1e-8时一定要记得将float改成double,因为float 4个字节不够储存会出现溢出导致无法给出结果的现象。
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int i,count=0;
char str[30];
gets_s(str);
for (i = 0; i <= strlen(str); i++)
if (str[i] == ' ') count++;
printf("there are %d words", count+1);
return 0;
}
这个没有书上的严谨,一旦前面有多个空格或者两个单词之间有多个空格就无法用这个方法。
文章使用的方法有点麻烦,还要定义max()函数,下面是不定义max函数的情况
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int i,a[10],max,n=1;
printf("Please enter 10 integer numbers:");
for (i = 0; i < 10; i++)
scanf_s("%d",&a[i]);
for (i = 1, max = a[0]; i < 10; i++)
{
if (max < a[i]) max = a[i],n=i+1;
}
printf("The largest number is %d\nIt is the %dth number.", max,n);
return 0;
}
这一章很重要,要点也很多,因此我将一些网上搜索的试题卷放在此处
1,试题卷1
#include
int main()
{
int a = 100, b = 9;
int * pointer_1, *pointer_2;
pointer_1 = &a, pointer_2 = &b;
printf("%d,%d",* pointer_1,* pointer_2);
return 0;
}
#include
int main()
{
int a, b;
int * pointer_1, *pointer_2;
scanf_s("%d,%d", &a, &b);
pointer_1 = &a, pointer_2 = &b;
if (a < b)
{
pointer_1 = &b, pointer_2 = &a;
}
printf("%d,%d\n",* pointer_1,* pointer_2);
return 0;
}
#include
int main()
{
void swap(int *p1,int *p2);
int a, b;
int * pointer_1, *pointer_2;
scanf_s("%d,%d", &a, &b);
pointer_1 = &a, pointer_2 = &b;
if (a < b)
{
swap(pointer_1, pointer_2);
}
printf("%d,%d\n",* pointer_1,* pointer_2);
return 0;
}
void swap(int *p1,int *p2)
{
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}