❶ c#編程題目 高手進
這不是小學題目么 就是讓你把小穴的演算法翻譯成 c#而已。。。。 還tm演算法。。。。我以為啥呢。 直接上代碼。。。
classProgram
{
staticvoidMain(string[]args)
{
//傳遞分蘋果數據
Dictionary<int,double>appdata=newDictionary<int,double>();
appdata.Add(0,2);
appdata.Add(1,3);
appdata.Add(2,4);
Dictionary<int,double>wmdata=newDictionary<int,double>();
wmdata.Add(0,1D/3D);
wmdata.Add(1,1D/4D);
wmdata.Add(2,1D/5D);
SubDivApplemyapple=newSubDivApple();
myapple.SetAmount(appdata,100);
SubDivWaterMelonmywm=newSubDivWaterMelon();
mywm.SetAmount(wmdata,10);
varresult=SubDivFruits.GetPerGroupCount(newSubDivFruits[]{myapple,mywm});
foreach(variteminresult){
Console.WriteLine("{0}年級共有{1}人",item.Key,item.Value);
}
Console.ReadKey();
}
}
{
publicvirtualstringNameMemo{get;protectedset;}
double_totalfruitcount;
publicDictionary<int,double>GroupAndPerPersonCount;
(Dictionary<int,double>groupandamountperperson,doubletotalfruitcount){
if(groupandamountperperson==null)
thrownewArgumentNullException("groupandamountperperson");
if(groupandamountperperson.Count<2)
("groupandamountperperson","方法傳遞的分組數不能少於2個分組");
GroupAndPerPersonCount=groupandamountperperson;
_totalfruitcount=totalfruitcount;
returnthis;
}
publicstaticDictionary<int,int>GetPerGroupCount(IEnumerable<SubDivFruits>groups){
//檢查每個分水果的分配方法是不是都是同樣的分組數量
varpercount=(int)groups.Average(s=>s.GroupAndPerPersonCount.Count);
if(groups.Count(s=>s.GroupAndPerPersonCount.Count!=percount)>0){
thrownewException("錯誤,水果的分組數量不一致");
}
//第一步找到最多的人數可能設定為上限,即分最少數量的水果那麼全體人員的數量也不會多於那個數字
intmaxpeople=(int)groups.Max(s=>s._totalfruitcount/s.GroupAndPerPersonCount.Min(t=>t.Value));
Dictionary<int,int>toout=newDictionary<int,int>();
//初始化准備輸出的結果
foreach(varcitemingroups.First().GroupAndPerPersonCount){
toout.Add(citem.Key,-1);
}
varapplemethod=groups.Single(s=>s.NameMemo.Equals("分蘋果")).GroupAndPerPersonCount;
varwatermelonmethod=groups.Single(s=>s.NameMemo.Equals("分西瓜")).GroupAndPerPersonCount;
//由於題目三元一次方程無固定解,只有正整數解,所以不能用高斯消列只能排列組合窮舉,不然更簡單一點
//將所有人數排列
int[]peoples=newint[maxpeople];
for(inti=0;i<maxpeople;i++){
peoples[i]=i;
}
varresp=GetPermutation(peoples,3);
foreach(varcrespinresp){
if(applemethod[0]*cresp[0]+applemethod[1]*cresp[1]+applemethod[2]*cresp[2]==groups.Single(s=>s.NameMemo.Equals("分蘋果"))._totalfruitcount)
{
if(watermelonmethod[0]*cresp[0]+watermelonmethod[1]*cresp[1]+watermelonmethod[2]*cresp[2]==groups.Single(s=>s.NameMemo.Equals("分西瓜"))._totalfruitcount)
{
//找到了第一個解return這組數據
toout[0]=cresp[0];
toout[1]=cresp[1];
toout[2]=cresp[2];
break;
}
}
}
returntoout;
}
staticvoidSwap(refinta,refintb)
{
inttemp=a;
a=b;
b=temp;
}
staticList<int[]>GetCombination(int[]t,intn)
{
if(t.Length<n)
{
returnnull;
}
int[]temp=newint[n];
List<int[]>list=newList<int[]>();
GetCombination(reflist,t,t.Length,n,temp,n);
returnlist;
}
staticvoidGetCombination(refList<int[]>list,int[]t,intn,intm,int[]b,intM)
{
for(inti=n;i>=m;i--)
{
b[m-1]=i-1;
if(m>1)
{
GetCombination(reflist,t,i-1,m-1,b,M);
}
else
{
if(list==null)
{
list=newList<int[]>();
}
int[]temp=newint[M];
for(intj=0;j<b.Length;j++)
{
temp[j]=t[b[j]];
}
list.Add(temp);
}
}
}
staticList<int[]>GetPermutation(int[]t,intn)
{
if(n>t.Length)
{
returnnull;
}
List<int[]>list=newList<int[]>();
List<int[]>c=GetCombination(t,n);
for(inti=0;i<c.Count;i++)
{
List<int[]>l=newList<int[]>();
GetPermutation(refl,c[i],0,n-1);
list.AddRange(l);
}
returnlist;
}
//求數組排列
staticvoidGetPermutation(refList<int[]>list,int[]t,intstartIndex,intendIndex)
{
if(startIndex==endIndex)
{
if(list==null)
{
list=newList<int[]>();
}
int[]temp=newint[t.Length];
t.CopyTo(temp,0);
list.Add(temp);
}
else
{
for(inti=startIndex;i<=endIndex;i++)
{
Swap(reft[startIndex],reft[i]);
GetPermutation(reflist,t,startIndex+1,endIndex);
Swap(reft[startIndex],reft[i]);
}
}
}
}
publicclassSubDivApple:SubDivFruits{
publicSubDivApple(){
NameMemo="分蘋果";
}
}
publicclassSubDivWaterMelon:SubDivFruits
{
publicSubDivWaterMelon()
{
NameMemo="分西瓜";
}
}
❷ c語言編程_分西瓜
自己的作業要自己做的
從條件可知這個數肯定是個整數
你可以考慮用一個從1到100得for循環,在循環中將滿足條件的數printf
❸ C語言約瑟夫環切西瓜問題
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int x,i=0,c=0,y,left=0,*arr;
printf("x和y:");
scanf("%d %d",&x,&y);
y++;
arr=(int*)calloc(x,sizeof(int));
memset(arr,0,x*sizeof(int));
while(left<x)
{
if(0==arr[i])
c++;
if(y==c)
{
arr[i]=1;
left++;
//不需要列印切掉的西瓜的編號的話,
//注釋掉這句
printf("%d ",i+1);
if(left==x)
printf("\n最後留下來的西瓜的編號是:%d",i+1);
c=0;
}
if(++i==x)
i=0;
}
free(arr);
return 0;
}
❹ 用c語言編寫這道題的程序
#include<stdio.h>
intmain(void){
intnum=1020,days=0;
while((num=num/2-2)>0)
++days;
printf("%ddays ",days);
return0;
}
❺ c語言程序 有100個西瓜,每天賣出所有西瓜的四分之一多一個,幾天後能將西瓜賣完,並輸出最後結果
#include<stdio.h>
#include<stdlib.h>
intmain()
{
inti=0,n=100;
while(n>0)
{
n=n/4-1;
i++;
printf("%d天後剩餘西瓜%d
",i,n);
}
printf("
%d天後賣完西瓜
",i);
system("pause");
return0;
}
❻ c語言西瓜問題
其實想法相對好理解 就是12個數中任意選取其中6個數相加等於總和的一半,然後篩選出來,難點是如何窮舉組合。
我大概寫了個:(結果得到6組,實際上第一堆和第二堆位置顛倒,本質只有3組,可以看下)
#include <stdio.h>
#include <stdlib.h>
void zuhe(int a[],int n,int m,int s)//從n個元素的數組a中,取m個元素的組合
{
int index,i,j,x=0,p[6],q[6];
int s2,l=1;
int item1[6],item2[6];
index=0;
p[index]=0;
while(1)
{
if(p[index]>=n)
{
if(index==0)
{
break;
}
index--;
p[index]++;
}
else if(index==m-1)//滿足12選6組合
{
s2=0;
x=0;
for(i=0;i<m;i++)s2+=a[p[i]];
if(s2==s)//滿足平分,得出第一堆p[6]
{
for(i=0;i<12;i++)
{
for(j=0;j<m;j++)
if(i==p[j])break;
if(j==m)q[x++]=i;//篩選第二堆q[6]
}
printf("%d.",l);
printf("第一堆:");
for(i=0;i<m;i++)printf("%3d",a[p[i]]);
printf(" ");
printf("第二堆:");
for(i=0;i<x;i++)printf("%3d",a[q[i]]);
printf("\n");
l++;
}
p[index]++;
}
else
{
index++;
p[index]=p[index-1]+1;
}
}
}
main()
{
int i,s=0;
int items[12]={98,93,57,64,50,82,18,34,69,56,16,61};
for(i=0;i<12;i++)s+=items[i];
s/=2;//算出平分值
zuhe(items,12,6,s);
}
❼ c語言的問題
#include<stdio.h>
#defineNUM1000/*這只是自定義一個盡量大的數作為數組的個數,簡單點,也可以
選擇用malloc定義*/
intin_data(float*,float*);
voidout_list(float*,float*,int);
intmain()
{
intc,i,j;
floatp1[NUM]={0};
floatp2[NUM]={0};
printf("請輸入若干西瓜的實際重量和猜測重量,以-1為結束標記: ");
j=in_data(p1,p2);
out_list(p1,p2,j);
return0;
}
intin_data(float*pa,float*pb)
{
inti,n;
intj;
j=0;
for(i=0;;i++)
{
scanf("%f",&pa[i]);
if(pa[i]!=-1){
j++;
}else{
break;
}
scanf("%f",&pb[i]);
}
returnj;
}
voidout_list(float*pc,float*pd,inti)
{
floatc[i],d[i];
intl,j,k;
for(l=0;l<i;l++)
c[l]=*(pd+l)-*(pc+l);
for(j=0;j<i;j++)
d[j]=100*c[j]/pc[j];
printf("西瓜編號實際重量猜測重量絕對誤差相對誤差 ");
for(k=0;k<i;k++)
{
printf("西瓜%d",k+1);
printf("%19.4fkg",*(pc+k));
printf("%15.4fkg",*(pd+k));
printf("%15.4fkg",c[k]);
printf("%15.4f%%",d[k]);
printf(" ");
}
❽ C語言編程序:有1020個西瓜,第一天賣一半多兩個,以後每天賣剩下的一半多兩個,問幾天以後能賣完
#include「stdio.h」
main()
{
int i,a=1020;
for(i=1;a;i++)
a=a-(a/2+2);
printf("%d",i);
getch();
}
(8)c語言編程分西瓜擴展閱讀:
for循環小括弧里第一個「;」號前為一個為不參與循環的單次表達式,其可作為某一變數的初始化賦值語句, 用來給循環控制變數賦初值; 也可用來計算其它與for循環無關但先於循環部分處理的一個表達式。
執行的中間循環體可以為一個語句,也可以為多個語句,當中間循環體只有一個語句時,其大括弧{}可以省略,執行完中間循環體後接著執行末尾循環體。
執行末尾循環體後將再次進行條件判斷,若條件還成立,則繼續重復上述循環,當條件不成立時則跳出當下for循環。