还有能帮我回答一下9楼的问题吗?谢谢了!
该学习了。。。
又写了一个,好像用时更多了!
#include <stdio.h>
int main()
{
long s, m, i;
long a(long n);
scanf("%ld", &s);
for(i = 1;i <= s;i ++)
{
scanf("%ld", &m);
if(m == 1)
printf("%ld %ld %d\n", i, m, 3);
else
printf("%ld %ld %ld\n", i, m, a(m));
}
return 0;
}
long a(long n)
{
long sum = 0;
if(n == 1)
sum = 3;
else if(n == 2)
sum = 5;
else if(n == 3)
sum = 9;
else
sum = a(n - 1) + a(n - 2) - 1;
return sum;
}
又写了一个,还是不对啊!
#include <stdio.h>
int main()
{
int s, m, i, j, k;
int sum;
scanf("%d", &s);
for(i = 1;i <= s;i ++)
{
sum = 0;
scanf("%d", &m);
if(m == 1)
printf("%d %d %d\n", i, m, 3);
else if(m == 2)
printf("%d %d %d\n", i, m, 5);
else if(m == 3)
printf("%d %d %d\n", i, m, 9);
else
{
for(k = 4;k <= m;k ++)
for(j = 2;j <= i - 2;j ++)
{
if(i%j == 0)
continue;
else if(i%2 == 0&&j%2 == 0)
continue;
else
sum ++;
}
sum += (3 + 2*(2*m -3));
printf("%d %d %d\n", i, m, sum);
}
}
return 0;
}
这回结果对了!又超时了!真是愁死我了!
#include <stdio.h>
int main()
{
int s, m, i, j, k;
int sum;
int f(int a,int b);
scanf("%d", &s);
for(i = 1;i <= s;i ++)
{
sum = 0;
scanf("%d", &m);
if(m == 1)
printf("%d %d %d\n", i, m, 3);
else if(m == 2)
printf("%d %d %d\n", i, m, 5);
else if(m == 3)
printf("%d %d %d\n", i, m, 9);
else
{
for(k = 4;k <= m;k ++)
for(j = 2;j <= k - 2;j ++)
if(f(k, j))
sum ++;
sum = (3 + 2*(2*m -3)) + 2*sum;
printf("%d %d %d\n", i, m, sum);
}
}
return 0;
}
int f(int a,int b)
{
int i, c;
c = a > b ? b : a;
for(i = 2;i <= c;i ++)
if(a%i == 0&&b%i == 0)
return 0;
return 1;
}