算法竞赛--简单数学

1.守形数

守形数是这样一种整数,它的平方的低位部分等于它本身。

比如25的平方是625,低位部分是25,因此25是一个守形数。

编一个程序,判断N是否为守形数。

输入:

输入包括1个整数N,2<=N<100。

输出:

可能有多组测试数据,对于每组数据,输出”Yes!”表示N是守形数。输出”No!”表示N不是守形数。

样例输入:

1
2
6
11
复制

样例输出:

1
2
Yes!
No!
复制

我的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <string.h>
int main()
{
int n;
char a[4],b[4];
while(scanf("%d",&n)!=EOF)
{
memset(a,0,4);
memset(b,0,4);
int count=0;
int m=n*n;
do{
a[count++]=n%10+'0';
n=n/10;
}while(n);
for(int i=0;i<count;i++)
{
b[i]=m%10+'0';
m=m/10;
}
if(strcmp(a,b)==0)
printf("Yes!\n");
else
printf("No!\n");
}
return 0;
}
复制

注意将数组进行初始化,WA50%

参考代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int m=n*n;
int cnt=1;
while(cnt<n)
{
cnt=cnt*10;
}
if(m%cnt==n)
printf("Yes!\n");
else
printf("No!\n");
}
return 0;
}
复制

2.反序数

设N是一个四位数,它的9倍恰好是其反序数(例如:1234的反序数是4321求N的值

input:

程序无任何输入数据。

output:

输出题目要求的四位数,如果结果有多组,则每组结果之间以回车隔开。

我的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <string.h>
int main()
{
char a[4],b[4];
for(int i=1000;i<=1112;i++)
{
memset(a,0,4);
memset(b,0,4);
int t=i,temp=9*i;
for(int j=0;j<4;j++)
{
a[j]=t%10;
b[4-j-1]=temp%10;
temp=temp/10;
t=t/10;
}
if(strcmp(a,b)==0)
printf("%d\n",i);
}
return 0;
}
复制

参考代码如下:

利用逆置函数解题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
int reverse(int n)
{
int j=0;
for(int i=0;i<4;i++)
{
j=j*10+n%10;
n=n/10;
}
return j;
}
int main()
{
for(int i=1000;i<1112;i++)
{
if((9*i)==reverse(i))
printf("%d\n",i);
}
return 0;
}
复制

3.百鸡问题

用小于等于n元去买100只鸡,大鸡5元/只,小鸡3元/只,还有1/3元每只的一种小鸡,分别记为x只,y只,z只。编程求解x,y,z所有可能解。

输入:

测试数据有多组,输入n。

输出:

对于每组输入,请输出x,y,z所有可行解,按照x,y,z依次增大的顺序输出。

样例输入:

1
45
复制

样例输出:

1
2
3
4
5
6
7
8
9
x=0,y=0,z=100
x=0,y=1,z=99
x=0,y=2,z=98
x=0,y=3,z=97
x=0,y=4,z=96
x=1,y=0,z=99
x=1,y=1,z=98
x=1,y=2,z=97
x=2,y=0,z=98
复制

我的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<=n/5;i++)
{
for(int j=0;j<=(n-i*5)/3&&i+j<=100;j++) //注意该处i+j<=100,否则WA50%
if(i*15+j*9+(100-i-j)<=3*n)
printf("x=%d,y=%d,z=%d\n",i,j,(100-i-j));
}
}
return 0;
}
复制

4.众数

输入20个数,每个数都在1-10之间,求1-10中的众数(众数就是出现次数最多的数,如果存在一样多次数的众数,则输出权值较小的那一个)。

输入:

测试数据有多组,每组输入20个1-10之间的数。

输出:

对于每组输入,请输出1-10中的众数。

注意如果存在一样多次数的众数,则输出权值较小的那一个。

样例输入:

1
8 9 6 4 6 3 10 4 7 4 2 9 1 6 5 6 2 2 3 8
复制

样例输出:

1
6
复制

我的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int a[11],n;
while(scanf("%d",&n)==1)
{
fill(a,a+11,0);
a[n]++;
for(int i=1;i<20;i++)
{
scanf("%d",&n);
a[n]++;
}
int max=0;
for(int i=10;i>0;i--)
if(a[i]>=a[max])
max=i;
printf("%d\n",max);
}
return 0;
}
复制

4.abc

设a、b、c均是0到9之间的数字,abc、bcc是两个三位数,且有:abc+bcc=532。求满足条件的所有a、b、c的值

输入:

题目没有任何输入。

输出:

请输出所有满足题目条件的a、b、c的值,a、b、c之间用空格隔开。每个输出占一行。

我的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main()
{
for(int i=100;i<=532;i++)
{
int j=532-i;
int a=i/100;
int b=i/10%10;
int c=i%10;
if(j/100==b&&j/10%10==c&&j%10==c)
printf("%d %d %d\n",a,b,c);
}
return 0;
}
复制
小礼物走一个哟

Related Issues not found

Please contact @Daybreak-Zheng to initialize the comment

0%