求此题的C语言代码

作者&投稿:邵齿 (若有异议请与网页底部的电邮联系)
C语言的程序题~

#include
long fun(int n) /*此函数的功能是: 算n的阶乘 */
{ long fac=1; int i;
for(i=1; i<=n;i++) fac*=i;
return fac;
}
main( )
{ int i; long sum=0;
for(i=1;i<4; i+=2) /* 此循环执行的次数是: 2次 (i=1 和 i=3) */
sum=sum+fun(i);
printf("sum=%ld
",sum);
}
程序的运行结果是:sum=7(加回车)
2.请在题目指定的位置添加注释,并写出程序的运行结果。
#include
void fun(int x[],int n) /*此函数的功能是: 将x所指向的数组逆序 */
{ int i,j,temp;
i=0; j=n-1;
while(i<j)
{ temp=x[i]; x[i]=x[j];x[j]=temp;
/* 以上3条语句的作用是: 交换x[i]和x[j]的值 */
i++; j--; }
}
main()
{int x[]={1,2,3,4,5,6},i;
fun(x,6);
for(i=0;i<6;i++) printf("%2d",x[i]);
}
程序的运行结果是: 6 5 4 3 2 1

c++的代码你看得懂?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
int nNu, nMult = 1;
int i, j;
int max = 0;
int anArr[10] = {0};

if (argc == 1)
{
printf("Usag:\n\t<app> <Nu1> <Nu2> ... <Nux>\n");
return 0;
}

for (i = 1; i < argc; ++i)
{
nNu = atoi(argv[i]);
if (nNu == 0)
{
printf("Invalid digital %s\n", argv[i]);
return 0;
}
anArr[i-1] = nNu;
nMult *= nNu;
if (max < nNu)
max = nNu;
}
for (i = nNu; i <= nMult; ++i)
{
for (j = 0; j < argc-1; ++j)
{
if (i%anArr[j]!=0)
break;
}
if (j == argc-1)
{
printf("%4d\n", i);
break;
}
}

return 0;
}

我写了个,不过有少少麻烦,为了实现多组输入,我用了链表,程序还有不少缺陷

#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define MAX_NUM 50 //数组最大长度

typedef struct TABLE{
int value[MAX_NUM]; //value[0]存放有效数值的个数,从value[1]开始存放数据
struct TABLE *next;
}TABLE, *LPTABLE;

void Input(LPTABLE head);
int MaxDiv(int a, int b);
int MinMul(int a, int b);
void Output(TABLE head);

int main()
{
TABLE head;
printf("输入格式:b 3 1 2 3 (每组数据以b开头,回车键结束,第二个数是整数的个数)\n所有数据输入完后,再次按下回车开始求最小公倍数\n");
Input(&head);
Output(head);

return 0;
}

void Input(LPTABLE head)
{
int i = 0;
LPTABLE p = NULL, q = NULL;
p = head;
p->next = NULL;
while(getchar() == 'b')
{
i = 0;
q = (LPTABLE)malloc(sizeof(TABLE)); //这部分代码用于分配内存单元,建立链表
p->next = q;
q->next = NULL;
p = q;
scanf("%d", &(p->value[0]));
for(i = 1; i <= p->value[0]; i++)
{
scanf("%d", &(p->value[i]));
}
getchar();
}
}

int MaxDiv(int a, int b) //求两个数的最大公约数
{
int div, divend, res; //除数,被除数,余数

if(a > b)
{
div = a;
divend = b;
}else{
div = b;
divend = a;
}

res = div % divend;

while(res != 0)
{
div = divend;
divend = res;

res = div % divend;
}

return divend;
}

int MinMul(int a, int b) //求两个数的最小公倍数
{
int ret;

ret = MaxDiv(a, b);

return a * b / ret;
}

void Output(TABLE head)
{
int i, j = 0;
int temp;
LPTABLE p;
p = head.next;

while(p)
{
temp = p->value[1];
for(i = 2; i <= p->value[0]; i++)
{
temp = MinMul(temp, p->value[i]);
}
j++;
printf("第 %d 组数的最小公倍数为: %d \n", j, temp);
p = p->next;
}
}

/*
Name: LCM in Matrix
Copyright:
Author: yilonglucky
Date: 2011-8-15 15:00:05
Description:Calculate the Least Common Multiple in one matrix
*/
/*
Sample Input
2 4 6
3 2 5 7
0
Sample Output
12
70
*/
#include <stdio.h>
#include <stdlib.h>
#define ROWMAX 10 //max of rows
#define NUMMAX 10 //max of numbers in one row
unsigned long matrix[ROWMAX][NUMMAX+2];
//numbers of this line stored in matrix[i][0]
//lcm stored in matrix[i][matrix[i][0]+1], the last position
static unsigned short row=0;

//return the Greatest Common Divisor of a and b
unsigned long gcd(unsigned long a, unsigned long b){
unsigned long r;
while(b>0){
r=a%b;
a=b;
b=r;
}
return a;
}

//return the Least Common Multiple of a and b
unsigned long lcm(unsigned long a, unsigned long b)
{
return a*b/gcd(a,b);
}

//input numbers into the matrix
int input(void)
{
unsigned short i=0;
do
{
scanf("%d",&matrix[row][0]);
for(i=1;i<=matrix[row][0];i++)
scanf("%d",&matrix[row][i]);
row++; //ready to the next row
}while(0 != matrix[row-1][0]);
row=row-2; //correct number of last row
return 0;
}

//show the result
int output(void)
{
unsigned short i=0;
if(0 == matrix[0][0])
{
printf("No numbers in matrix!\n");
return 0;
}
for(i=0;i<=row;i++)
printf("%d\n",matrix[i][matrix[i][0]+1]);
return 0;
}

//calculate the Least Common Multiple of each line
int calculate(void)
{
unsigned short i=0; //row number
unsigned short j=0;
unsigned long temp=0;
if(0 == matrix[0][0])
return 0;
for(i=0;i<=row;i++) //each line
{
temp=matrix[i][1];
for(j=1;j<matrix[i][0];j++)
temp=lcm(temp,matrix[i][j+1]);
matrix[i][matrix[i][0]+1]=temp;
}
return 0;
}
int main(void)
{
printf("------ Input ------\n");
input();
calculate();
printf("------ Result -----\n");
output();
system("pause");
return 0;
}
//=====================================================================
//i can email the source file to you if your email address is left
/*
Name: LCM in Array
Copyright:
Author: yilonglucky
Date: 2011-8-15 15:14:15
Description:Calculate the Least Common Multiple in one array
*/
/*
Sample Input
2 4 6
Sample Output
12

Sample Input
3 2 5 7
Sample Output
70

*/
#include <stdio.h>
#include <stdlib.h>
#define NUMMAX 10 //max of numbers in one row
unsigned long Array[NUMMAX+2];

//return the Greatest Common Divisor of a and b
unsigned long gcd(unsigned long a, unsigned long b){
unsigned long r;
while(b>0){
r=a%b;
a=b;
b=r;
}
return a;
}

//return the Least Common Multiple of a and b
unsigned long lcm(unsigned long a, unsigned long b)
{
return a*b/gcd(a,b);
}

//input numbers into the array
int input(void)
{
unsigned short i=0;
scanf("%d",&Array[0]);
for(i=1;i<=Array[0];i++)
scanf("%d",&Array[i]);
return 0;
}

//show the result
int output(void)
{
unsigned short i=0;
if(0 == Array[0])
{
printf("No numbers in Array!\n");
return 0;
}
printf("%d\n",Array[Array[0]+1]);
return 0;
}

//calculate the Least Common Multiple of each line
int calculate(void)
{
unsigned short j=0;
unsigned long temp=0;
if(0 == Array[0])
return 0;
temp=Array[1];
for(j=1;j<Array[0];j++)
temp=lcm(temp,Array[j+1]);
Array[Array[0]+1]=temp;
return 0;
}
int main(void)
{
while(1)
{
printf("------ Input unless press Ctrl&C ------\n");
input();
calculate();
printf("------ Result -----\n");
output();
}
system("pause");
return 0;
}

我的天哪,就是十来个数就十几位了,自己用大乘吧。原理,把输入的 N 个数分解成因子模式
如 4 = 2^2 6 = 2^1 3^1
逐个素数因子比较,因为求公倍数,所以大着留,即 4 中 2^2 比6中 2^1多一个。
把所得因子相乘即所求:
参考测试:
in:
2 4 6
3 2 5 7
15 52 36 64 95 68 20 21 36 58 96 120 102 1111 1113 1117
5 69 32 12 14 36
7 89 36 2 69 55 5 7
9 52 36 64 95 68 20 21 36 58
8 52 36 64 95 68 20 21 36
7 52 36 64 95 68 20 21
6 52 36 64 95 68 20
5 52 36 64 95 68
4 52 36 64 95
3 52 36 64
2 52 36
1 52
out:
12
70
161464667268864960
46368
28371420
2454903360
84651840
84651840
12093120
12093120
711360
7488
468
52

参考算法:

#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX 10000

void Prime(int p[]);

int a[MAX], b[MAX], c[MAX];

int main()
{
int N, NE, i, j, TM, temp;
double ce;
Prime(b);
while (scanf("%d", &N) != EOF)
{
TM=0; temp=1;ce = 1.0;
memset(a, 0, sizeof(a));
for (i=0;i<N;i++)
{
scanf("%d", &NE);
if(NE > TM) TM=NE;
if(b[NE]==0 && a[NE]==0)
{ a[NE]++;
continue;
}
memset(c, 0, sizeof(int)*TM);
for(j=2;j<=NE;j++)
if(b[j]==0 &&(NE%j==0))
{c[j]++; NE/=j;j=1;}
for (j=2;j<=TM;j++)
if(c[j]>a[j]) a[j]=c[j];
}
for (i=2;i<=TM;i++)
{
if(a[i])
while(a[i]--)
ce*=i;
}
printf("%.0f\n",ce);
}

return 0;
}

void Prime(int p[])
{
int t=0;
int i, j;
memset(p, 0, sizeof(p));
p[0]=1;p[1]=1;
for (i=2; i<=sqrt(MAX*1.0);i++)
{
if(!p[i])
for (j=2; j*i<MAX;j++)
{
p[i*j]=1;
}
}
}

#include <stdio.h>

int gcd(int a,int b) // 最大公约数

{

int r;

do

{

r=a%b;

a=b;

b=r;

} while(r!=0);

return a;

}

int lcm(int a, int b) // 最小公倍数

{

return a/gcd(a,b)*b;

}

int main(void)

{

int n,num,Lcm;

while(scanf("%d",&n)!=EOF)

{

Lcm=1;

while(n--)

{

scanf("%d",&num);

Lcm=lcm(Lcm,num);

}

printf("%d
",Lcm);

}

return 0;

}

另外,比赛时间已过。提交上去都是 Out Of Contest Time (超出比赛时间)。

以下是我的提交截图:




C语言初学者请问这道题怎么做?
遍历字符串S,使用数组统计其中26个字母分别出现的次数 最终最少的字母出现次数即为可以召唤的神龙数 再乘以n就是可以实现的愿望数 C语言参考代码如下:include <stdio.h> int main(){ long long int n; \/\/ 注意n的取值范围超过了2^31-1,应取长整型 scanf("%lld", &n);char S[1000000];s...

这道题用C语言怎么编写,谢谢!不甚感激,我是大一的,望能够讲的明摆着,谢 ...
看代码注释:include <stdio.h>int main(){ float sum = 0; \/* 经过的总路程, 初始值0 *\/ float hight = 100; \/* 弹起的高度,初始值100 *\/ const int count = 10; \/* 要落地的次数为常量10 *\/ int i; \/* 计数器 *\/ for (i = 0; i < count; i++) { i...

请问下面这个实例用C语言程序怎样编程
函数输出等级。注意,在 switch 语句中,可以使用多个 case 标号来表示同一种情况,这样可以简化代码。例如,case 10 和 case 9 都表示成绩在 90 分以上的情况,因此可以将它们写在一起。另外,在 switch 语句中,必须使用 break 语句来结束每个 case 分支,否则程序将继续执行后面的 case 分支。

求教如何用c语言表示这个题 请写出具体的代码
直接定义好变量,并初始化好,再输出就可以了,这题目的应该是练习浮点数的格式输出。在手机上用易历知食软件里的微C程序设计来编写个示例,手机上的代码如下图:手机上的运行结果如下图:附文本代码:include<stdio.h>int main (){ \/\/先定义好各个变量,并初始化 int a = 3,b = 4,c = ...

C语言编程:如下要求的题目咋写代码?
代码文本:include "stdio.h"int max(int a[],int n,int *p){ for(n--,*p=0;n>=0;n--)if(a[*p]=0;n--)if(a[*p]>a[n])p=n;return a[*p];} int main(int argc,char *argv[]){ int a[20]={13,19,12,9,10,3,7,18,1,11,20,8,2,14,15,16,4,5,6,17},ma...

C语言编程题 求代码
include "stdafx.h"include <iostream> include using namespace std;define M 10 define N 10 int main(){ int m, n,x;float num[M][N],min;cout << "请输入m、n的值:" << endl;cin >> m >> n;void output(float a[][10], int b, int c);float findmin(float a[][10...

这道c语言题目怎么写?求大神
在手机上用易历知食软件里面的微C程序设计功能来编个示例代码并运行程序,手机上代码如下:运行结果如下:完整文本代码:include <stdio.h>double Sn( double a1,double an,double d);int main(){ double a1,an,d; printf("输入第一项、最后一项和公差(空格相隔)\\n"); scanf("%lf %lf %lf...

急!急!简单c语言编程题,求编写代码
\/\/第一题#include<stdio.h>void fun(int n){ int i,j,k,t;for(k=0;k<n;k++)\/\/控制总行数{ for(i=0;i<n-k-1;i++)\/\/控制输出空格,因为空格不好看见,就用*代替 printf("%c",'*'); for(j=0;j<2*k+1;j++)\/\/控制输出字母 { printf("%c",65+j)...

求高手解释这道C语言题的代码,,,讲解一下他处理如此大的数据的方...
define MAX 1000000 \/\/宏定义MAX=1000000 include <stdio.h>\/\/头文件 int main()\/\/主函数 { short a[MAX]= {0,1,2} ,l,j,p,t; \/\/定义short型数组及变量 unsigned n,i,m,b[500]={0} ,x,y;\/\/定义unsigned型变量数组 while(scanf("%u%u",&x,&y) != EOF)\/\/直到文件...

c语言输入题目的代码?
include<stdio.h> int main(){ int a,b,c,max;printf("请输入三个数:\\n");scanf("%d%d%d",&a,&b,&c);if(a>b)max=a;if(c>max)max = c;printf("三个数中最大的数为:%d",max);return 0;}

金口河区17225662827: 如何用c语言解此题? -
俟顷沐欣: int a,b //甲乙的实际年龄 for(a=4;a<67;a++) { for(b=4;b<a;b++) { if (a-b=b-4) if (a-b=64-a) print(d%d%,a,b); } }

金口河区17225662827: 用C语言编写下面题目的程序: -
俟顷沐欣: 第一个 #include <stdio.h> int main() {char ch,str;int i,j,k;printf("输入字母\n");scanf("%c",&ch);//获取字符str='A';//从大写字母A开始for(i=0;i<=ch-'A';i++){for(k=0;k<ch-'A'-i;k++)printf("%c",' ');//打印空格for(j=0;j<2*i+1;j++){...

金口河区17225662827: 求解此题,c语言 -
俟顷沐欣: 实现代码如下:#include<stdio.h> int main(){ char *mon[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; int n; scanf("%d",&n); if(n<=0 || n>12) printf("Illegal month\n"); else printf("%s\n",mon[n-1]); return 0; }

金口河区17225662827: c语言 求这道题的过程 -
俟顷沐欣:楼主,这题有规律,你会发现,两条对角线上都有1 头行,尾行,头列,尾列都有1所以程序如下#include int main() { int i, j; for(i = 0; i <...

金口河区17225662827: 一道c语言的题目 急求代码 -
俟顷沐欣: #include /*定义一个函数,用指向字符串的指针匹配子字符串*/ int match(char * str, char * str1, char * str2, char * a_str); int main() {/*定义两个字符数组,分别存储主字符串和子字符串*/ char mother[256],child[256]; /*定义一个字符串,用于替换...

金口河区17225662827: 求写一个C语言程序 求一元二次方程ax2+bx+c=0的跟.题目是C语言求一元二次方程ax2+bx+c=0的跟.当a=0,b=0时方程无解;当a=0时,b≠0是方程只有一个实... -
俟顷沐欣:[答案] #include void main() { float a,b,c,x,delt; couta>>b>>c; delt=b*b-4*a*c; if(delt>0) cout

金口河区17225662827: 帮我用c语言程序求出这道题目
俟顷沐欣: #include<stdio.h> void main() { float change,a[10]={100,50,20,10,5,2,1,0.5,0.2,0.1}; //定义当前正在发行的人民币面值. int i,count=0; printf("请输入工资金额(元):"); scanf("%f",&change); printf("最少找钱方案为: \n"); for(i=0;i...

金口河区17225662827: 求解这道题 c语言的 -
俟顷沐欣: 这里代码这样写更清楚:if(a>b) c=a; a=b; b=c; if判断复合语句内只包含了一句:c=a; 判断a>b显然不成立,这一句并不会执行.之后顺序执行剩余的两句:a=b; a即为10 b=c; b即为16 所以最终结果:a = 10, b = 16, c = 16 如果改为:if(a>b) { c=a; a=b; b=c; } 这里就是if复合语句包含3句,条件不成立3句都不执行,最终结果3个变量未变化.

金口河区17225662827: 求Sn=a+a*a+a*a*a+...+a*a*...*a(n个a)之值,其中a是一个数字,n表示a的位数此题为C程序 -
俟顷沐欣:[答案] 只需要改n和a的值就可以了: #include #include const int n=10;//这里改n const int a=2;//这里改a void main() { int sum=0; for(int i=1;i

金口河区17225662827: C语言的题,求代码鸭 -
俟顷沐欣: #include <stdio.h> int main() { char a ='c'; printf("character=%c, num=%d", a-32,a-32); return 0; } 可以了解一下ascii码

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网