Exercise2
2018-03-30 15:44
141 查看
1.将数组A中的内容和数组B中的内容进行交换。(数组一样大)2. 计算1/1-1/2+1/3-1/4+1/5 ⋯⋯ + 1/99 - 1/100 的值。3. 编写程序数一下 1到 100 的所有整数中出现多少次数字9
//1将数组A中的内容和数组B中的内容进行交换。(数组一样大) #include<stdio.h> #include<math.h> #include<windows.h> #define len 10 void swap_array() { int j = 0; int i = 0; int arr1[len] = { 1, 3, 8, 6, 5,56,56,56,5634,345}; int arr2[len] = { 5, 3, 6, 9, 3 ,56,2,134,6,54}; for (j = 0; j < len; j++) { arr1[j] ^= arr2[j]; arr2[j] ^= arr1[j]; arr1[j] ^= arr2[j]; } for (i = 0; i < len; i++) { printf("%d ", arr1[i]); } printf("\n"); for (i = 0; i < len; i++) { printf("%d ", arr2[i]); } printf("\n"); } int main() { swap_array(); system("pause"); return 0; } //2计算1 / 1 - 1 / 2 + 1 / 3 - 1 / 4 + 1 / 5 ⋯⋯ + 1 / 99 - 1 / 100 的值。 #include<stdio.h> #include<Windows.h> double regular_calculation() { int i = 1; int flag = 1; double sum =0;//注意 sum 的类型 double for (; i <= 100; i++) { sum += (flag*1.0) / i; flag = -flag; } printf("sum=%f\n", sum); } int main() { regular_calculation(); system("pause"); return 0; } //3. 编写程序数一下 1到 100 的所有整数中出现多少次数字9。 #include<stdio.h> #include<Windows.h> void count_number() { int i = 1; int count = 0; for (; i <= 100; i++) { if (i % 10 == 9)//99计算两次 { count++; } if (i / 10 == 9) { count++; } } printf("%d\n",count); } int main() { count_number(); system("pause"); return 0; }
相关文章推荐
- Selected solutions to exercise of "The C Programming Language" 2e (Part 5)
- Think in java 答案_Chapter 2_Exercise 7
- Think in java 答案_Chapter 4_Exercise 1
- Think in java 答案_Chapter 4_Exercise 17
- Think in java 答案_Chapter 5_Exercise 11
- Exercise One in Carnegie Course SSD Six
- icarnegie网站测试题(exercise 1)
- emacs学习 6.3 Exercise with Narrowing
- 算法导论 Exercise 3.1-3
- SICP Exercise 3.37
- SICP Exercise 4.4
- google code jam exercise——Store Credit
- google code jam exercise——File Fix It
- Exercise 1: Introduction to Code Analysis
- APUE2e之Exercise 10.6 Solution B
- Analysis by Its History_exercise 1.4
- matrix theory_basic results and techniques_exercise_1.2.10
- [Liferay]liferay hooks exercise startup
- Exercise 3: Multivariate Linear Regression
- btrace exercise