Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

level1_1-6 #27

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions P01_runningletter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<stdio.h>
#include<windows.h>
#define SPACE 50
void show(char *pos);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

适当加些空行

int main()
{
char pos[102] = { " " };
for(int i=0;i<100;i++)
{
pos[i] = 'R';
show(pos);
pos[i] = ' ';
if(i==99) //字母碰到边界时变为递减
for(;i>=0;i--)
{
pos[i] = 'R';
show(pos);
pos[i] = ' ';
}
}
return 0;
}
void show(char *pos) {
printf("%s", pos);
Sleep(SPACE);
system("cls");
}
34 changes: 34 additions & 0 deletions P02_isprime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
#include<stdlib.h>
int IS(int); //判断是否为素数
int main() {
int x;
printf("输入一个正整数,程序判断它是否为素数(q to quit):");
while (scanf_s("%d", &x))
{
if (x == 1 || x == 2) {
puts("是素数");
}
else if (IS(x))
puts("是素数.");
else puts("不是素数.");
printf("输入一个正整数,程序判断它是否为素数(q to quit):");
}
system("pause");
return 0;
}
int IS(int x)
{
int i;
int mark = 0; //创建标记
for (i = 2; i < x; i++)
{
if (x%i == 0)
{
mark = 1; //若被整除,标记mark为1
break;
}
}if (mark == 1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

格式有点乱

return 0;
else return 1;
}
14 changes: 14 additions & 0 deletions p03_Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<stdio.h>
#include<stdlib.h>

void main()
{
int i;
for (i = 1; 28 * i != 21 * i + 4 * i + 252; i++)
{
;
}
printf("丢番图的年纪是%d\n", i);
system("pause");
}

19 changes: 19 additions & 0 deletions p04_ narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a, b, c;
for (a = 0; a <= 9; a++)
{
for (b = 0; b <= 9; b++)
{
for (c = 0; c <= 9; c++)
{
if (a*a*a + b*b*b + c*c*c == a + 10 * b + 100 * c && !( b == 0 && c == 0)) //排除非三位数(0开头)的情况
printf("%d%d%d\n", c, b, a);
}
}
}
system("pause");
return 0;
}
35 changes: 35 additions & 0 deletions p05_allPrimes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int isprinme(int a);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prime拼写错了;P应该大写;
参数名a,不如改为n

int IS(int);
int main()
{
clock_t t1, t2;
t1 = clock();
for(int i=2;i<=1000;i++)
{
if (isprinme(i))
printf("%5d", i);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加大括号

}
t2 = clock();
printf("打印时间:%f秒",(double)(t2-t1)/CLOCKS_PER_SEC);
system("pause");
return 0;
}
int isprinme(int a)
{
int mark = 0;
for (int i = 2; i < a; i++)
{
if (a%i == 0) //用mark标记a已被整除
{
mark = 1;
break;
}

}
if (mark == 1) //检查标记
return 0;
else return 1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里改为?表达式更清真一点(这是我们项目组内部的说法,意思是代码漂亮干净)

}
57 changes: 57 additions & 0 deletions p06_Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int loadPrime(int* a);
int isPrime(int x);
int main()
{
int prime[100] = {0};
int sum;
sum=loadPrime(prime);
for (int a = 4; a <= 100;a+=2 )
{
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < sum; j++)
{
if (a == prime[i] + prime[j]) //遍历两个100以内的素数组合之和,与a作比较
{
printf("%d满足哥德巴赫猜想\n",a);
goto b; //跳出遍历循环
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

语句标号名取得不太好

}
}
}

b: ;
}
system("pause");
return 0;
}
int isPrime(int x)
{
int i;
int mark = 0; //创建标记
for (i = 2; i < x; i++)
{
if (x%i == 0)
{
mark = 1; //若被整除,标记mark为1
break;
}
}if (mark == 1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

换行,缩进。。。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实,这里用?表达式也很爽

return 0;
else return 1;
}
int loadPrime(int* a)
{
int x = 0;
for (int i = 1; i < 100; i++)
{
if (isPrime(i))
{
a[x] = i; //把素数赋值给数组
x++;
}
}
return x; //返回100以内的素数个数
}
57 changes: 57 additions & 0 deletions p07_encrypt_decrypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define WIDTH 500
void encrypt(char* pt, int* pt2, int n); //加密函数
void decrypt(char* pt, int* pt2, int n); //解密函数
char* s_gets(char* st, int n); //获取输入
int main()
{
char input[WIDTH] = { " " };
char num[WIDTH] = { 0 };
srand(time(NULL)); //用时间初始化随机数种子
for (int i = 0; i < WIDTH - 1; i++) //随机数组,供加密解密使用
{
num[i] = (int)rand()%10;
}

printf("输入待加密内容:");
s_gets(input, WIDTH - 1);
encrypt(input,num,WIDTH-1);
printf("加密后内容:%s\n", input);
decrypt(input, num,WIDTH - 1);
printf("解密后内容:%s\n", input);
system("pause");
return 0;
}
void encrypt(char* pt,int* pt2,int n)
{
for (int i = 0; i < n; i++)
{
pt[i]+=pt2[i];
}
}
void decrypt(char* pt, int* pt2, int n)
{
for (int i = 0; i < n; i++)
{
pt[i] -=pt2[i];
}
}
char* s_gets(char* st, int n)
{
char* ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n'&&st[i] != '\0')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加大括号

i++;
if (st[i] == '\n')
st[i] = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
Loading