一个C语言的编程题目,请高手帮忙!谢谢!

c语言的
从键盘输入任意两个正整数x和y,编程求出两数的最大公因子和最小公倍数
.输入】10个数,用起泡法对十个数排序(有小到大)
.输入一行字符、统计其中分别有多少个单词和空格,比如: how are ,you有三个单词和两个空格

/*第一题:从键盘输入任意两个正整数x和y,编程求出两数的最大公因子和最小公倍数
*/
#include <stdio.h>

int main(void)
{
unsigned int x,y,i,a,b;
printf("请输入两个正整数x和y:");
scanf("%d %d", &x,&y);
if (x < 2 || y < 2) {
printf("无效的输入\n");
return 0;
}
a = 0; b = 0;
for(i = 2; i <= x * y; i++) {
if(a == 0 && x % i == 0 && y % i == 0) a = i;
if(i % x == 0 && i % y == 0) { b = i; break; }
}
if(a == 0)
printf("%d 和 %d 没有公因子。最小公倍数是 %d。\n", x, y, b);
else
printf("%d 和 %d 的最大公因子是%d,最小公倍数是 %d。\n", x, y, a, b);
return 1;
}

/*第二题 10个数,用起泡法对十个数排序(有小到大)
*/
#include<stdio.h>

int main(void)
{
int i,j,k;
int a[]={3,9,7,5,4,1,6,2,0,8};
for( j = 0 ; j < 10 ; j++ ) {
for ( i=0; i < 10-j; i++)
if (a[i]>a[i+1]) {
k=a[i];
a[i]=a[i+1];
a[i+1]=k;
}
}
for(i = 0; i < 10 ; i++)
printf("%d ",a[i] );
printf("\n");
return 0;
}

/*第三题
输入一行字符、统计其中分别有多少个单词和空格
*/
#include<stdio.h>

int main(void)
{
char line[80];
int a = 0, b = 0, word = 0;
char * s = line;
gets(line);
while(*s) {
if((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z')) {
if(word == 0) {
word = 1;
a ++;
}
s ++;
continue;
}
if(*s == ' ' || *s == '\t' ) {
if(word) word = 0;
b++;
}
s++;
}
printf("%d words and %d spaces\n", a, b);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-07-03
1.
#include <stdio.h>
main()
{
int m,n,flag=1,r,a,b;
printf("please input m and n(m>n):");
scanf("%d%d",&m,&n);
a=m;
b=n;
while(flag)
{
r=m%n;
if(r==0)
flag=0;
else
{
m=n;
n=r;
}

}
printf("最大公约数为:%d\n",n);
printf("最小公倍数为:%d\n",a*b/n);

}
2.
#include <stdio.h>
main()
{
int a[10];
int i,j,t;
printf("please input 10 numbers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n");
for(j=0;j<9;j++)
for(i=0;i<9-j;j++)
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
printf("从小到大顺序:\n");
for(i=0;i<10;i++)
printf("%-3d",a[i]);
printf("\n");
}
3.
#include"stdio.h"
main()
{
char str[81];
int i,num=0,word=0,space=0;
char c;
printf("please input a string :\n");
gets(str);
for(i=0;(c=str[i])!='\0';i++)
{
if(c==' ')
{
word=0;
space++;
}
else if(word==0)
{
word=1;
num++;
}
}
printf("thers are %d words and %d spaces in the line.\n",num,space);
}
第2个回答  2010-07-03
作业贴吗???这么简单,不予回答。
还是好好学学吧
但可以提示你一下,这三个程序谭浩强的C里面都有,自己翻翻就好了