已知0≤X≤27,请写出一个函数,给定X值时,即算出数字的个数。
8 个解决方案
#1
#include <stdio.h>
void main()
{
int i,a,b,c;
int count=0;
int x;
scanf("%d",&x);
for(i=0;i<1000;i++)
{
a=i/100;
b=i%100/10;
c=i%10;
if (a+b+c==x)
count++;
}
printf("count=%d",count);
}
void main()
{
int i,a,b,c;
int count=0;
int x;
scanf("%d",&x);
for(i=0;i<1000;i++)
{
a=i/100;
b=i%100/10;
c=i%10;
if (a+b+c==x)
count++;
}
printf("count=%d",count);
}
#2
不错,只是效率是不是有点低啊,需要循环1000次进行比对。
有没有效率更高的算法?
有没有效率更高的算法?
#3
Function getnum(ByVal x As Integer) As Integer
Dim i As Integer
For i = 0 To 999
If i Mod 10 + (i \ 10) Mod 10 + i \ 100 = x Then getnum = getnum + 1
Next
End Function
Dim i As Integer
For i = 0 To 999
If i Mod 10 + (i \ 10) Mod 10 + i \ 100 = x Then getnum = getnum + 1
Next
End Function
#4
有没有效率更高的算法?
Function getnum(ByVal x As Integer) As Integer
getnum = Array(1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1)(x)
End Function
Function getnum(ByVal x As Integer) As Integer
getnum = Array(1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1)(x)
End Function
#5
晕~
散分!
散分!
#6
to northwolves(狼行天下)
vb不太熟,不过感觉Array的初始化和对index检索似乎都有改进的余地 :)
试了一下:
Public Function getnum(ByVal x As Integer) As Integer
Dim a() As Integer = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1}
Return a(x)
End Function
Public Function getnum2(ByVal x As Integer) As Integer
'Dim a() As Integer = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1}
If x < 10 Then Return (x + 1) * (x + 2) / 2
If x > 17 Then Return (28 - x) * (29 - x) / 2
If x = 13 Or x = 14 Then Return 75
If x = 12 Or x = 15 Then Return 73
If x = 11 Or x = 16 Then Return 69
If x = 10 Or x = 17 Then Return 63
End Function
For j = 0 To 100000000
'getnum(27) 24秒
'getnum(17) 24秒
'getnum2(27) 20秒
'getnum2(17) 4秒
Next
有没有效率更高的算法? :)
vb不太熟,不过感觉Array的初始化和对index检索似乎都有改进的余地 :)
试了一下:
Public Function getnum(ByVal x As Integer) As Integer
Dim a() As Integer = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1}
Return a(x)
End Function
Public Function getnum2(ByVal x As Integer) As Integer
'Dim a() As Integer = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1}
If x < 10 Then Return (x + 1) * (x + 2) / 2
If x > 17 Then Return (28 - x) * (29 - x) / 2
If x = 13 Or x = 14 Then Return 75
If x = 12 Or x = 15 Then Return 73
If x = 11 Or x = 16 Then Return 69
If x = 10 Or x = 17 Then Return 63
End Function
For j = 0 To 100000000
'getnum(27) 24秒
'getnum(17) 24秒
'getnum2(27) 20秒
'getnum2(17) 4秒
Next
有没有效率更高的算法? :)
#7
看不懂
哪位大哥解释一下
哪位大哥解释一下
#8
我来解释一下 ,
可能是那两个算式把人搞糊涂了。实际上由于那个数列有较特殊的关系,对于x<10时,f(x)=f(x-1)+x ,故写成了那个形式。
之所以比较27和17,因为那是该算法中可能的,最慢的值。
其实在这个case中,象 If x = 13 Or x = 14 那样直接的一个个写出来,效果应该更好
另外,象northwolves(狼行天下) 那样用Array,但将他声明为静态的,只初始化一次,可能就是最快的,Vb这个我不太会写,有兴趣的可以试试,别忘了告诉一下结果 :)
可能是那两个算式把人搞糊涂了。实际上由于那个数列有较特殊的关系,对于x<10时,f(x)=f(x-1)+x ,故写成了那个形式。
之所以比较27和17,因为那是该算法中可能的,最慢的值。
其实在这个case中,象 If x = 13 Or x = 14 那样直接的一个个写出来,效果应该更好
另外,象northwolves(狼行天下) 那样用Array,但将他声明为静态的,只初始化一次,可能就是最快的,Vb这个我不太会写,有兴趣的可以试试,别忘了告诉一下结果 :)
#1
#include <stdio.h>
void main()
{
int i,a,b,c;
int count=0;
int x;
scanf("%d",&x);
for(i=0;i<1000;i++)
{
a=i/100;
b=i%100/10;
c=i%10;
if (a+b+c==x)
count++;
}
printf("count=%d",count);
}
void main()
{
int i,a,b,c;
int count=0;
int x;
scanf("%d",&x);
for(i=0;i<1000;i++)
{
a=i/100;
b=i%100/10;
c=i%10;
if (a+b+c==x)
count++;
}
printf("count=%d",count);
}
#2
不错,只是效率是不是有点低啊,需要循环1000次进行比对。
有没有效率更高的算法?
有没有效率更高的算法?
#3
Function getnum(ByVal x As Integer) As Integer
Dim i As Integer
For i = 0 To 999
If i Mod 10 + (i \ 10) Mod 10 + i \ 100 = x Then getnum = getnum + 1
Next
End Function
Dim i As Integer
For i = 0 To 999
If i Mod 10 + (i \ 10) Mod 10 + i \ 100 = x Then getnum = getnum + 1
Next
End Function
#4
有没有效率更高的算法?
Function getnum(ByVal x As Integer) As Integer
getnum = Array(1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1)(x)
End Function
Function getnum(ByVal x As Integer) As Integer
getnum = Array(1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1)(x)
End Function
#5
晕~
散分!
散分!
#6
to northwolves(狼行天下)
vb不太熟,不过感觉Array的初始化和对index检索似乎都有改进的余地 :)
试了一下:
Public Function getnum(ByVal x As Integer) As Integer
Dim a() As Integer = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1}
Return a(x)
End Function
Public Function getnum2(ByVal x As Integer) As Integer
'Dim a() As Integer = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1}
If x < 10 Then Return (x + 1) * (x + 2) / 2
If x > 17 Then Return (28 - x) * (29 - x) / 2
If x = 13 Or x = 14 Then Return 75
If x = 12 Or x = 15 Then Return 73
If x = 11 Or x = 16 Then Return 69
If x = 10 Or x = 17 Then Return 63
End Function
For j = 0 To 100000000
'getnum(27) 24秒
'getnum(17) 24秒
'getnum2(27) 20秒
'getnum2(17) 4秒
Next
有没有效率更高的算法? :)
vb不太熟,不过感觉Array的初始化和对index检索似乎都有改进的余地 :)
试了一下:
Public Function getnum(ByVal x As Integer) As Integer
Dim a() As Integer = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1}
Return a(x)
End Function
Public Function getnum2(ByVal x As Integer) As Integer
'Dim a() As Integer = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1}
If x < 10 Then Return (x + 1) * (x + 2) / 2
If x > 17 Then Return (28 - x) * (29 - x) / 2
If x = 13 Or x = 14 Then Return 75
If x = 12 Or x = 15 Then Return 73
If x = 11 Or x = 16 Then Return 69
If x = 10 Or x = 17 Then Return 63
End Function
For j = 0 To 100000000
'getnum(27) 24秒
'getnum(17) 24秒
'getnum2(27) 20秒
'getnum2(17) 4秒
Next
有没有效率更高的算法? :)
#7
看不懂
哪位大哥解释一下
哪位大哥解释一下
#8
我来解释一下 ,
可能是那两个算式把人搞糊涂了。实际上由于那个数列有较特殊的关系,对于x<10时,f(x)=f(x-1)+x ,故写成了那个形式。
之所以比较27和17,因为那是该算法中可能的,最慢的值。
其实在这个case中,象 If x = 13 Or x = 14 那样直接的一个个写出来,效果应该更好
另外,象northwolves(狼行天下) 那样用Array,但将他声明为静态的,只初始化一次,可能就是最快的,Vb这个我不太会写,有兴趣的可以试试,别忘了告诉一下结果 :)
可能是那两个算式把人搞糊涂了。实际上由于那个数列有较特殊的关系,对于x<10时,f(x)=f(x-1)+x ,故写成了那个形式。
之所以比较27和17,因为那是该算法中可能的,最慢的值。
其实在这个case中,象 If x = 13 Or x = 14 那样直接的一个个写出来,效果应该更好
另外,象northwolves(狼行天下) 那样用Array,但将他声明为静态的,只初始化一次,可能就是最快的,Vb这个我不太会写,有兴趣的可以试试,别忘了告诉一下结果 :)