请教c语言编程题目

分拆素数和
把一个偶数拆成两个不同素数的和,有几种拆法呢?
Input

输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。
Output

对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。
Sample Input
30260
Sample Output
32
#include<stdio.h>
#include<math.h>
int fun(int m)
{
int i,k=1;
for(i=2;i<=sqrt(m);i++)
{
if(m%i==0)
k=0;
}
return k;
}

int main()
{
int m,i,k=0;
while(scanf("%d",&m)!=EOF)
{
if(m==0)
break;
for(i=2;i<=m;i++)
{
if(fun(i)==1&&fun(m-i)==1)
k++;
}
if(fun(m/2)!=1)
printf("%d\n",k/2);
else
printf("%d\n",(k+1)/2);

k=0;
}
}
结果是Time Limit Exceeded

2.
Problem Description

lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.

Input

There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)

Output

For each test case, you should output the a^b's last digit number.

Sample Input
7 668 800

Sample Output
96
我的
#include<stdio.h>
int main()
{
int a,b,i;
int T=1;
while(scanf("%d%d",&a,&b)!=EOF)
{
for(i=1;i<=b;i++)
{
T=T*a;
T=T%10;
}
printf("%d\n",T);
T=1;
}
}
还是超时
求大神帮我修改!!!
初学c语言

给你下思路吧

第一个 素数算法不好 改用素数筛 没听说过的话 自己搜 很常用的算法


第二个 对于a先求个位数 然后按照你的算法求值 

如果还是超时的话

那么进行折半递归

类似于

int get_value(int a, int b)
{
    int t;
    if(b == 1) return a;
    t = get_value(a, b/2);
    if(b&1)return t*t*a%10;
    else return t*t%10;
}

int main()
{
    ....
    get_value(a%10, b);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-02-09
打表法求素数,速度快,不会超时。
#include<stdio.h>
int main()
{
int a[10001],i,j,x,sum;
for (i=0;i<10001;i++) a[i]=1;

a[0]=0;a[1]=0;
for (i=2;i<10001;i++)
for (j=i;j<=10000/i;j++)
a[i*j]=0; /*打表*/

scanf("%d",&x);
while (x!=0)
{
sum=0;
for (i=2;i<=x/2;i++)
if ((a[i])&&(a[x-i])&&(i!=(x-i))) sum++;
printf("%d\n",sum);
scanf("%d",&x);
}
}
第二题找规律即可
0 | 只能是0
1 | 只能是1
2 | 2 4 8 6
3 | 3 9 7 1
4 | 4 6
5 | 只能是5
6 | 只能是6
7 | 7 9 3 1
8 | 8 4 2 6
9 | 9 1本回答被提问者采纳