add
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main() {
|
||||
long timea, timeb, timec;//clock_t timea;
|
||||
long a = 1LL, b = 100000000LL;
|
||||
long long result1 = 0LL, result2 = 0LL;
|
||||
|
||||
timea = clock();
|
||||
for (long long i = a; i <= b; i++) {
|
||||
result1 += i;
|
||||
}
|
||||
timeb = clock();
|
||||
printf("累加结果: %lld, 所用时间: %lf 毫秒\n", result1, (double)(timeb - timea) / CLOCKS_PER_SEC * 1000);
|
||||
|
||||
result2 = (a + b) * (b - a + 1) / 2;
|
||||
timec = clock();
|
||||
printf("高斯结果: %lld, 所用时间: %lf 毫秒\n", result2, (double)(timec - timeb) / CLOCKS_PER_SEC * 1000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
// 函数声明
|
||||
long long sum_leijia(long long a, long long b);
|
||||
long long sum_gaosi(long long a, long long b);
|
||||
|
||||
int main() {
|
||||
clock_t before, middle, after;
|
||||
long long a = 1LL, b = 100000000LL;
|
||||
long long result1 = 0LL, result2 = 0LL;
|
||||
before = clock();
|
||||
result1 = sum_leijia(a, b);
|
||||
middle = clock();
|
||||
printf("累加计算结果: %lld, 所用时间: %lf 毫秒\n", result1, (double)(middle - before) / CLOCKS_PER_SEC * 1000);
|
||||
|
||||
// 计算公式方式的和并计时
|
||||
result2 = sum_gaosi(a, b);
|
||||
after = clock();
|
||||
printf("高斯求和结果: %lld, 所用时间: %lf 毫秒\n", result2, (double)(after - middle) / CLOCKS_PER_SEC * 1000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
long long sum_leijia(long long a, long long b) {
|
||||
long long sum = 0LL;
|
||||
for (long long i = a; i <= b; i++) {
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
long long sum_gaosi(long long a, long long b) {
|
||||
return (a + b) * (b - a + 1) / 2;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
// row 表示当前要打印的行号(从1开始),n 表示总行数
|
||||
void printAt(int row, int n) {
|
||||
if (row > n)
|
||||
return;
|
||||
for (int i = 0; i < row; i++)
|
||||
printf("@");
|
||||
printf("\n"); // 打印完一行后换行
|
||||
printAt(row + 1, n);
|
||||
}
|
||||
int main() {
|
||||
int n = 3;
|
||||
printAt(1, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user