#include<stdlib.h>
int main()
{
int a=0,i=1
while(i<=100)
{
a=a+i
i++
}
printf("%d",a)
system("pause")
return o
}
利用循环结构编程序求1+2+3+······+n的和并输出,其中n由键盘输入 #include "stdio.h"void main()
{
int n,s,i
s=0
scanf("%d",&n)
for(i=1i<=ni++)
s+=i
printf("%d\n",s)
}
编程:请分别用do while、while两种循环结构编程计算: 1!+2!+3!+...+n! , n的值由键盘输入。
#include<stdio.h>
int fun(int a)
{
int i=1,sum=1
while(i<=a)
{
sum*=i
i++
}
return sum
}
main()
{
int n,i=1,sum=0
printf("请输入n的值:")
scanf("%d",&n)
while(i<=n)
{
sum+=fun(i)
i++
}
printf("结果是:%d",sum)
}
这是while循环。do while就自己改造了。
C语言:请分别用三种循环结构编程计算: 1!+2!+3!+...+n! , n的值由键盘输入。
C语言#include<stdio.h>
void main()
{
int n=1,i,sum=0,num=1
do
{
num=1
for(i=ni>0i--)
num*=i
sum=sum+num
n++
} while(n<=10)
printf("(1->10)! is %d\n",sum)
}
C++编写#include<iostream>
using namespace std
void main()
{
int n=1,i,sum=0,num=1
while(n<=10)
{
num=1
for(i=ni>0i--)
num*=i
sum+=num
n++
}
cout<<sum<<endl
}
vf 编程 利用子程序求1+1/2+1/3...+1/n的值,其中n由键盘输入.
input "请输入N" to n
s=0
for i=1 to n
s=s+1/n
endfor
求编程,谢谢。 由键盘输入N,编程求M=N! 其中N=1*2*3*..N
程序的实现的是这样的,先设置一个文本框,用于n值的出入。再设置一个命令按钮用于求解。代码如下:
Private Sub Command1_Click()
Dim n As Integer, i As Integer, m As Double
m = 1
n = CInt(Text1.Text)
If n <= 0 Then
Text1.Text = "0"
Else
For i = 1 To n
m = m * i
Next i
Text1.Text = m
End If
End Sub
程序又经过本人调试,程序正常。
编程计算s=1*2*3*4*...*N(N由键盘输入(1-9)(用汇编程序)
汇编和C++不一样的
首先中断读入数字
然后用LOOP循环累乘,输出结果
利用c语言编程:求1!/1+2!/(1+2)+3!/(1+2+3)+……+n!/(1+2+3+……+n)的值,其中n由键盘输入
#include<stdio.h>void main(){int i,n,he=0,ji=1 double sum=0 scanf("%d",&n) for(i=1i<=ni++){he+=i ji*=i sum+=(double)ji/he }printf("%lf\n",sum)}
C语言编程题:S=1!+2!+3!+4!+5!+…+n! n由键盘输入 求循环过程
这是我们以前做过的课后习题... #include<stdio.h>void main() { int n,ifloat s=0,t=1scanf("%d",&n)for(i=1i<=ni++) { t*=is+=t} printf("1+2!+3!...+n!=%f\n",s)}
编程求n!,n由键盘输入
这是之前用C 写过的.
#include<stdio.h>
#include<memory.h>main(){int f[10000],a[10000],i,j,k,t,nwhile(scanf("%d",&n)!=EOF){memset(f,0,sizeof(f))f[0]=1for(i=1i<n+1i++){memset(a,0,sizeof(a))for(t=0t<10000t++){ f[t]*=iif(t!=0) f[t]+=a[t-1]a[t]=f[t]/10f[t]%=10}}for(i=9999i>=0i--){if(f[i]!=0){k=ibreak}}for(i=ki>=0i--){printf("%d",f[i])}printf("\n")}}
等下补上一个用Java 写的
表示一个数字,使用科学计数法。如500表示IntegerString(5,2)
public class IntegerString { private int number = 0数字 private int length = 010的length次方 /** Creates a new instance of IntegerString */ public IntegerString(int number,int length) { this.number = numberthis.length = length} public int getNumber(){ return this.number} public int getLength(){ return this.length}}
/**
两个数相乘,结果用两个数来表示,一个是高位,一个是低位
**/
public class MultiplyResult { private IntegerString highprivate IntegerString low/** Creates a new instance of MultiplyResult */ public MultiplyResult(IntegerString a,IntegerString b) { int result = a.getNumber()*b.getNumber()int length = a.getLength()+b.getLength()if(result>=10){ high = new IntegerString(result/10,length+1)low = new IntegerString(result%10,length)}else{ high = new IntegerString(result,length)low = new IntegerString(0,length-1)} } public String toString(){ 打印方便,以便调试 StringBuffer *** = new StringBuffer() *** .append(high.getNumber()) *** .append(low.getNumber())for(int i=0i<low.getLength()i++) *** .append("0")return *** .toString()} public IntegerString getHigh(){ return high} public IntegerString getLow(){ return low} }
public class Factorial{
由大到小,由高到低,将一个字符的数表示成科学计数法private IntegerString[] getIntegerString(String str){ IntegerString[] is = new IntegerString[str.length()]for(int i=0i<str.length()i++){ char ch = str.charAt(i)int number = Character.getNumericValue(ch)is[i] = new IntegerString(number,str.length()-1-i)} return is}
获得数的最高位 private int getMaxLength(IntegerString[] a){ int max = 0for(int i=0i<a.lengthi++){ if(a[i].getLength()>max) max = a[i].getLength()} return max}
一个数与单个数字相乘 private IntegerString[] singleMultiply(IntegerString[] old,IntegerString gene){ MultiplyResult[] mr = new MultiplyResult[old.length]for(int i=0i<old.lengthi++){ mr[old.length-1-i] = new MultiplyResult(old[i],gene) System.out.println(mr[old.length-1-i])} mr是从最低位到最高位 java.util.ArrayList arrays = new java.util.ArrayList()int carry = 0int maxLength = mr[mr.length-1].getHigh().getLength()获得最高位的长度 for(int i=0i<=maxLengthi++){从个位到最高位一次加,如果有进位,那么存放到carry中 int number = carryfor(int j=0j<mr.lengthj++){ if(mr[j].getLow().getLength()==i) { number+=mr[j].getLow().getNumber()} if(mr[j].getHigh().getLength()==i){ number+=mr[j].getHigh().getNumber()} } if(number>=10){ 如果有进位,取余数,如果没有,取本身 arrays.add(new IntegerString(number%10,i))carry=1}else{ arrays.add(new IntegerString(number,i))carry=0} } if(carry==1){ 如果还有进位,那么加入到最高位 arrays.add(new IntegerString(carry,maxLength+1))} IntegerString[] results = new IntegerString[arrays.size()]java.util.Iterator ii = arrays.iterator()int index=0while(ii.hasNext()){ results[index++] = (IntegerString)ii.next()} return results}
private void print(IntegerString[] a){ System.out.println(getNumberic(a))} 将数字由IntegerString[]数组转换成字符串 private String getNumberic(IntegerString[] a){ StringBuffer *** = new StringBuffer()int max = getMaxLength(a)for(int i=0i<=maxi++){ boolean isFind = falsefor(int j=0j<a.lengthj++){ if(a[j].getLength()==i){ *** .insert(0,a[j].getNumber())isFind = truebreak} } if(!isFind){ *** .insert(0,0)} } return *** .toString()}
两个数相加 private IntegerString[] add(IntegerString[] a,IntegerString[] b){ if(a==null) return bif(b==null) return ajava.util.ArrayList arrays = new java.util.ArrayList()int aMax = getMaxLength(a)int bMax = getMaxLength(b)int max = aMax>bMax?aMax:bMaxint carry = 0for(int i=0i<=maxi++){ for(int j1=0j1<a.lengthj1++){ if(a[j1].getLength()==i) carry+=a[j1].getNumber()} for(int j2=0j2<b.lengthj2++){ if(b[j2].getLength()==i) carry+=b[j2].getNumber()} if(carry>0){ if(carry>=10){ arrays.add(new IntegerString(carry%10,i))carry=1if(i==max){ arrays.add(new IntegerString(carry,i+1))} }else{ arrays.add(new IntegerString(carry,i))carry=0} }else{ arrays.add(new IntegerString(0,i))carry=0} } IntegerString[] results = new IntegerString[arrays.size()]java.util.Iterator ii = arrays.iterator()int index=0while(ii.hasNext()){ results[index++] = (IntegerString)ii.next()} return results}
两个数相乘
private String stringMultiply(String a,String b){ IntegerString[] ais = this.getIntegerString(a)IntegerString[] bis = this.getIntegerString(b)IntegerString[] result = nullfor(int i=0i<bis.lengthi++){ IntegerString[] tmp = this.singleMultiply(ais,bis[i])result = add(result,tmp)} return this.getNumberic(result)}
打印N的阶乘
public void printFactorial(int n){
String total = "1"int n=100for(int i=1i<=ni++){ total = stringMultiply(i+"",total)}
System.out.println(total)
}
}
编程计算1!+3!+5!+…+(2N-1)!的值,其中N值由键盘输入
int fun(int n)
{
int mult = 1
while(n >= 1)
{
mult = mult * n
n--
}
return mult
}
void main()
{
int sum = 0
int i
int n
scanf("%d",&n)
for(i = 1i <= 2n-1i = i + 2)
{
sum = sum + fun(n)
}
printf("%d",sum)
}
#include "stdio.h"int main(int argc,char *argv[]){
__int64 s,t
int m,n
printf("Enter n(int 0<n<21)...\n")
if(scanf("%d",&n)!=1 || n<1 || n>20){
printf("Input error, exit...\n")
return 0
}
for(s=0,t=m=1m<=ns+=t*=m++)
printf("The result is %I64d\n",s)
return 0
}
运行样例:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)