|
查看: 1282|回复: 15
|
请教一题C# 或 VB.Net 问题
[复制链接]
|
|
|
今天 interview 被一题难倒了,
Write a function code in any language/platform (specify), it receive a integer parameter and process to generate it factorial number.
EG: factorial(120), result = 1*2*3*4*5
请问如何用 C# 或 VB.Net solve?thx |
|
|
|
|
|
|
|
|
|
|
发表于 28-2-2008 09:20 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 28-2-2008 09:25 PM
|
显示全部楼层
回复 2# hcong85 的帖子
factorial(5) = 1*5
factorial(24) = 1*2*3*4
factorial(120) = 1*2*3*4*5
factorial(55)=1*5*11
会难吗?想了很久结果没答  |
|
|
|
|
|
|
|
|
|
|
发表于 28-2-2008 11:23 PM
|
显示全部楼层
朋友。
factorization 跟 factorial 很不一样了leh |
|
|
|
|
|
|
|
|
|
|
发表于 13-3-2008 07:39 PM
|
显示全部楼层
刚刚写的....
public int[] Fac(int input)
{
int last_number = 1;
int i = 1;
int currentNumber = 0;
while (currentNumber < input)
{
last_number++;
i = last_number;
currentNumber = 1;
while (i != 1)
{
currentNumber *= i;
i--;
}
}
if (currentNumber == input)
{
// found, construc the array
int[] result = new int[last_number];
for (i = 1; i <= last_number; i++)
{
result[i - 1] = i;
}
return result;
}
else
{
// not found, the input has no factorial
return null;
}
}
会得到答案的Integer array。如果input是120,return {1, 2, 3, 4, 5}。
应该还有更短的方法吧,比如Recursive 之类的,但我想不到.... |
|
|
|
|
|
|
|
|
|
|
发表于 15-3-2008 09:54 PM
|
显示全部楼层
原帖由 yhchan 于 13/3/2008 07:39 PM 发表 
刚刚写的....
public int[] Fac(int input)
{
int last_number = 1;
int i = 1;
int currentNumber = 0;
while (currentNumber < input)
{
last_number++;
i = la ...
如果input是55,会不会 return (1, 5, 11) |
|
|
|
|
|
|
|
|
|
|
发表于 16-3-2008 08:50 PM
|
显示全部楼层
是FACTORIAL,不是FACTORS吧。
55的FACTORS = 1,5,11 (可以将55除出整数的)
但55没有FACTORIAL。
FACTORIAL 指的是必须从一开始乘,每次增加一的数目。
FACTORIAL(6) = 3, 2, 1 (3 X 2 X 1)
FACTORIAL(720) = 6, 5, 4, 3, 2, 1 (6 X 5 X 4 X 3 X 2 X 1)
但是55没有FACTORIAL。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 17-3-2008 06:29 PM
|
显示全部楼层
搂主的问题假如是找 Factors 的话,答案应该是2,3,5 ;
可是那个题目的答案是1,2,3,4,5
所以这题问题是要找出Factorial。 |
|
|
|
|
|
|
|
|
|
|
发表于 17-3-2008 09:09 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 18-3-2008 09:04 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 21-3-2008 08:43 PM
|
显示全部楼层
回复 10# hcong85 的帖子
请问这里谁可以帮下我??Assignment 有够难,身边的朋友都还做不到,DEADLINE 在下个月头,给下我掺考。谢谢.
希望有人能帮下我。应为不喜欢VB所以去读EE,到头来还是碰到。PROGRAM不知为什么都吃不到我的CODE,明明都dui,还是一直有ERROR。
写个程序可以算考试分数。有十个考生,二十题
char answer[] = {“AABADCBADCABDBCDDADD”};
The student’s matrix number and their answers are stored in a file called exam.dat in the following format:
Sammy 1700 AABADCBADCABDBCDDADD
Ahmad 1488 DAABDCCABCDBACDCCADD
Dave 2123 DAACABDCCDBACDBACDBA
.....................
对的答案有5分
Marks Grade
100 to 70 - A
69 to 60 - B
59 to 50 - C
49 to 40 - D
39 and below - F
Read the data from the file exam.dat
calculate the marks and determine the grade the student obtained.
Store the results in another file called result.txt in the following format.
Sample Output:
Name Matrix Number Total Marks Grade
Sammy 1700 60 C
……
……
students who scored A : 1
students who scored B : 2
students who scored C .3
students who scored D :3
students who scored F :1
Passes : 80%
Failures : 20% |
|
|
|
|
|
|
|
|
|
|
发表于 23-3-2008 07:19 PM
|
显示全部楼层
原帖由 physicholy 于 21/3/2008 08:43 PM 发表 
请问这里谁可以帮下我??Assignment 有够难,身边的朋友都还做不到,DEADLINE 在下个月头,给下我掺考。谢谢.
希望有人能帮下我。应为不喜欢VB所以去读EE,到头来还是碰到。PROGRAM不知为什么都吃不到我的CODE,明 ...
VB6 还是 VB.Net ??
你把整题问题都写出来了,
该不会是希望我们把整题答案做给你吧?? |
|
|
|
|
|
|
|
|
|
|
发表于 23-3-2008 08:44 PM
|
显示全部楼层
对你们来说应该很简单,没法,应为不喜欢VB所以去读EE,到头来还是碰到。是用VC++ /VB6也可以,C++ SOURCE CODE。我的朋友的SENIOR说我们老师教的不够用。不可能做到!!!可以做到CALCULATION 但做不到READ 和 int to str Conversion。我也不知道有CONVERTION。他讲我才知。所以希望大哥们可以帮下我。。。。 当自我练习lo.....有机会我会报答的。谁叫我不是美女。。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 24-3-2008 09:24 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 25-3-2008 09:43 AM
|
显示全部楼层
#include <stdio.h>
#include <string.h>
main ()
{
int x, y , number , marks=0, pass,i ;
char name[30][30], answer[22], answers[]= "DAACBBCADCBAABDCDADD",grade,Aa=0,Bb=0,Cc=0,Dd=0,Ff=0,temp[30];
FILE *cfPtr;
if ( (cfPtr = fopen("E://assignment.txt","r" ) == NULL ) {
printf( "FILE COULD NOT BE OPENED\n" );
}
else
{printf("%s\t%5s\t%12s\t%12s%8s\n","Name","MNumber","Answer","Marks","Grade" ;
for (y=0; y <30 ; y++)
{ marks = 0;
fscanf(cfPtr, "%s%i%s", name, &number, answer);
for (x=0 ; x <20 ; x++)
{
if ( answer[x] == answers [x] )
marks+=5 ;
}
////////////////////////////////////
for(int j=i+1;j<30;++j)
if(strcmp(name,name[j])>0)
{
strcpy(temp,name);
strcpy(name,name[j]);
strcpy(name[j],temp);
}
///////////////////////////////////////////
if ( marks>=70)
{grade='A';
++Aa;}
else if (marks>=60 )
{grade='B';
++Bb;}
else if (marks>=50 )
{grade='C';
++Cc;}
else if (marks>=40 )
{grade='D';
++Dd;}
else {grade='F';
++Ff;}
printf ("%s\t%-5i\t%s\t%i\t%c\n",name, number,answer, marks,grade);
}
printf ("\n%s%i\n","Total number of student who scored A :",Aa);
printf ("%s%i\n","Total number of student who scored B :",Bb);
printf ("%s%i\n","Total number of student who scored C :",Cc);
printf ("%s%i\n","Total number of student who scored D :",Dd);
printf ("%s%i\n","Total number of student who scored F :",Ff);
printf (" asses :%i\n",(Aa+Bb+Cc+Dd)*100/30);
printf ("Failures :%i\n\n",Ff*100/30);
fclose(cfPtr);
}
}
做到OUTPUt,REFER 书本,但不会SORT。可以给吓列子吗??sorting 那PART没ERROR,但跑的时候就ERROR,不支错在哪里。  |
|
|
|
|
|
|
|
|
|
|
发表于 26-3-2008 08:59 AM
|
显示全部楼层
原帖由 physicholy 于 25/3/2008 09:43 AM 发表 
#include
#include
main ()
{
int x, y , number , marks=0, pass,i ;
char name[30][30], answer[22], answers[]= "DAACBBCADCBAABDCDADD",grade,Aa=0,Bb=0,Cc=0,Dd=0,Ff=0,temp[30];
FILE *cfPtr; ...
你的sort在那里? 怎么我看不到的?
printf ("\n%s%i\n","Total number of student who scored A :",Aa);
建议你换成
printf ("\nTotal number of student who scored A :%d\n", Aa);
[ 本帖最后由 hcong85 于 26-3-2008 09:00 AM 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|