周四要机测了,555,QAQ
周四晚更新:目前我进入自闭状态~
C/C++练习7---求某个范围内的所有素数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
求小于n的所有素数,按照每行10个显示出来。
Input
输入整数n(n<10000)。
Output
每行10个依次输出n以内(不包括n)的所有素数。如果一行有10个素数,每个素数后面都有一个空格,包括每行最后一个素数。
Sample Input
100
Sample Output
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
Hint
请注意题目中求的是小于n的所有素数。
#include<stdio.h>
int main()
{
int n,i,j,count=0;
scanf("%d",&n);
for (i=2;i<n;i++)
{
for (j=2;j<i;j++)
{
if (i%j==0) break;//降低运算量;不是素数就break
}
if (j==i) {printf("%d",i);count++;}//循环完成,是素数,输出
else continue;
if(count%10==0) printf("\n");
else printf(" ");
}
return 0;
}
完美的素数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
素数又称质数。指一个大于1的自然数,除了1和此整数自身外,不能被其他自然数整除的数。我们定义:如果一个素数是完美的素数,当且仅当它的每一位数字之和也是一个素数。现在给你一个正整数,你需要写个程序判断一下这个数按照上面的定义是不是一个完美的素数。
Input
输入包含多组测试数据。
每组测试数据只包含一个正整数 n (1 < n <= 10^6)。
Output
对于每组测试数据,如果 n 是完美的素数,输出“YES”,否则输出“NO”(输出均不含引号)。
Sample Input
11
13
Sample Output
YES
NO
#include<stdio.h>
int main()
{
int n,flag,sum,n1,i,n2,n3;
while(scanf("%d",&n)!=EOF)
{
n2=0;
flag=1;
for (i=2;i<n;i++)
{
if (n%i==0) flag=0;
}
if (n==1) printf("NO\n");
else if(flag==1) n2=n;
else printf("NO\n");
sum=0;
n3=n2;
while (n2!=0)
{
n1=n2%10;
n2=n2/10;
sum=sum+n1;
}
flag=1;
if (n3!=0)
{
for (i=2;i<sum;i++)
{
if (sum%i==0) flag=0;
}
if (sum==1) printf("NO\n");
else if(flag==1) printf("YES\n");
else printf("NO\n");
}
}
return 0;
}
水仙花数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+33。
现在要求输出所有在m和n范围内的水仙花数。
Input
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。
Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
Sample Input
100 120
300 380
Sample Output
no
370 371
#include<stdio.h>
int main()
{
int m,n,one,two,three,i,temp,h;
while (scanf("%d %d",&m,&n)!=EOF)
{
i=0;
temp=m;
h=0;
while (temp<=n)
{
three=temp/100;
two=temp%100/10;
one=temp%100%10;
if (temp==three*three*three+two*two*two+one*one*one)
{
if (h==1) printf(" ");
h=1;
printf("%d",temp);
}
else i++;
temp++;
}
if (i==n-m+1) printf("no");
printf("\n");
}
return 0;
}
C语言实验——打印菱形
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
从键盘输入一个整数n(1≤n≤9),打印出指定的菱形。
Input
正整数n(1≤n≤9)。
Output
指定的菱形。
第一行前面有n-1个空格,第二行有n-2个空格,依此类推。
Sample Input
5
Sample Output
*
***
*****
*******
*********
*******
*****
***
*
#include<stdio.h>
int main()
{
int n,i,j,k;
scanf("%d",&n);
for (i=1;i<=n;i++)
{
for (j=1;j<=n-i;j++)
{
printf(" ");
}
for (k=1;k<=2*i-1;k++)
{
printf("*");
}
printf("\n");
}
for (i=n-1;i>=1;i--)
{
for (j=1;j<=n-i;j++)
{
printf(" ");
}
for (k=1;k<=2*i-1;k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
C语言实验——余弦
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入n的值,计算cos(x)。
Input
输入数据有多行,每行两个数,包括x和n。第一数据为x,第二个数据为n。
Output
输出cos(x)的值,保留4位小数。
Sample Input
0.0 100
1.5 50
Sample Output
1.0000
0.0707
#include<stdio.h>
int main()
{
double x,n,sum,i,t,temp;
while(scanf("%lf %lf",&x,&n)!=EOF)
{
t=x*x;
sum=1.0;
temp=1.0;
for(i=1;i<=n;i++)
{
temp=-t*temp;//求出分子,每次乘X平方*-1
temp=temp/(i*2-1)*(i*2);//分子除分母,前面已经除过(i*2-1)之前的部分,现在只需要求后边的部分即可
sum=sum+temp;
}
printf("%.4lf\n",sum);
}
return 0;
}
C/C++训练1---最大公约数与最小公倍数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入两个正整数,求它们的最大公约数与最小公倍数。
Input
输入两个正整数,两个整数之间用空格分开。
数据保证在 int 范围内。
Output
第一行输出最大公约数;
第二行输出最小公倍数。
答案保证在 int 范围内。
Sample Input
64 48
Sample Output
16
192
#include<stdio.h>
int main()
{
int m,n,r,temp,max,min,m1,n1;
scanf("%d %d",&m,&n);
m1=m;
n1=n;
if (n>m) {temp=m;m=n;n=temp;}
while (n!=0)
{
r=m%n;
m=n;
n=r;
max=m;
}
min=m1*n1/max;
printf("%d\n%d",max,min);
return 0;
}
九九乘法表
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
九九乘法表是数学学习的基础,今天我们就来看看乘法表的相关问题。《九九乘法歌诀》,又常称为“小九九”,如下图所示。你的任务是写一个程序,对于给定的一个正整数 n ,输出“九九乘法表”的前 n 行。例如,输入 n 为 9,你的程序的输出将为下图:
Input
输入包含多组测试数据,以 EOF 结束。每组测试数据只包含一个正整数 n (0 < n < 10)。
Output
对于每组测试数据,输出上图所示“九九乘法表”的前 n 行。
Sample Input
2
3
Sample Output
1*1=1
12=2 22=4
1*1=1
12=2 22=4
13=3 23=6 3*3=9
Hint
必须使用for循环,如果你的代码中出现例如
if(n == 1) printf(“1*1=1\n”);
if(n == 2) printf(“11=1\n12=2 2*2=4\n”);
或其类似语句,本题不得分。
#include<stdio.h>
int main()
{
int n,i,j;
while (scanf("%d",&n)!=EOF)
{
for (i=1;i<=n;i++)
{
for (j=1;j<=i;j++)
{
if (i==j) printf("%d*%d=%d\n",j,i,i*j);
else printf("%d*%d=%d ",j,i,i*j);
}
}
}
return 0;
}
多亏之前做了记录,现在学Java,遗忘的算法就可以借此重新拾起~
收束测试即将结束 :i_f25: