c语言问题,急求解答!!!
问题1:WriteaCprogramthatprintsoutalldifferentpossibilitiesofobtaining£1(1pound)us...
问题1:
Write a C program that prints out all different possibilities of obtaining £1(1 pound)
using coins of values 2 pence, 5 pence, and 10 pence. Indicate how many
possibilities have been found. The output of your program may look like:
£1 = 50 x 2p
£1 = 45 x 2p + 2 x 5p
£1 = 40 x 2p + 4 x 5p
…
In total, there are 66 possibilities to make £1.
Hint: You may use loops to generate the number of coins n2, n5, and n10 of value 2,
5, and 10 pence respectively and use the formula £1=100p=2*n2+5*n5+10*n10.
问题2:
Write a program that takes as input a sequence of scores (integer numbers between 0 and 20)
and finds the followings:
1. The highest score for that sequence as well as the number of times that maximum has
been attained.
2. The lowest score for that sequence as well as the number of times that minimum has
been attained.
We assume the number of scores is not known in advance and that the program will stop when
we enter the score -1. An execution of the program may look like the following:
Give a score (-1 to end): 12
Give a score (-1 to end): 8
Give a score (-1 to end): 13
Give a score (-1 to end): 7
Give a score (-1 to end): 11
Give a score (-1 to end): 12
Give a score (-1 to end): 7
Give a score (-1 to end): 9
Give a score (-1 to end): -1
Highest score: 13 attained 1 times
Lowest score: 7 attained 2 times
Hint: Use a while loop to read in the scores and keep track of the minimum and maximum
scores as well as their number of occurrences. 展开
Write a C program that prints out all different possibilities of obtaining £1(1 pound)
using coins of values 2 pence, 5 pence, and 10 pence. Indicate how many
possibilities have been found. The output of your program may look like:
£1 = 50 x 2p
£1 = 45 x 2p + 2 x 5p
£1 = 40 x 2p + 4 x 5p
…
In total, there are 66 possibilities to make £1.
Hint: You may use loops to generate the number of coins n2, n5, and n10 of value 2,
5, and 10 pence respectively and use the formula £1=100p=2*n2+5*n5+10*n10.
问题2:
Write a program that takes as input a sequence of scores (integer numbers between 0 and 20)
and finds the followings:
1. The highest score for that sequence as well as the number of times that maximum has
been attained.
2. The lowest score for that sequence as well as the number of times that minimum has
been attained.
We assume the number of scores is not known in advance and that the program will stop when
we enter the score -1. An execution of the program may look like the following:
Give a score (-1 to end): 12
Give a score (-1 to end): 8
Give a score (-1 to end): 13
Give a score (-1 to end): 7
Give a score (-1 to end): 11
Give a score (-1 to end): 12
Give a score (-1 to end): 7
Give a score (-1 to end): 9
Give a score (-1 to end): -1
Highest score: 13 attained 1 times
Lowest score: 7 attained 2 times
Hint: Use a while loop to read in the scores and keep track of the minimum and maximum
scores as well as their number of occurrences. 展开
2个回答
展开全部
哎,写了一下午,全部正确
第一题:
#include<stdio.h>
void main()
{
int i,j,k,num=0;
for(k=0;k<=10;k++)
for(j=0;j<=20;j++)
for(i=0;i<=50;i++)
if(2*i+5*j+10*k==100)
{
printf("£1 =100p=2*%d+5*%d+10*%d\n",i,j,k);
num++;
}
printf("In total, there are %d possibilities to make £1",num);
}
第二题:
#include<stdio.h>
#include<string.h>
void main()
{
int a[15],i,max,min,nums=1,numb=1; /*nums最小数出现次数,numb最大数出现次数*/
printf("please input 15 scores (end with -1,less then 15 number intotal.\n shoulde be integer numbers between 0 and 20)\n");
for(i=0;i<=14;i++)
scanf("%d",&a[i]);
for(i=1,max=min=a[0];a[i]!=-1;i++)
{if(a[i]>max)
{max=a[i];numb=1;
}
else if(a[i]==max)numb++;
if(a[i]<min)
{min=a[i];nums=1;
}
else if(a[i]==min)nums++;
}
printf("Highest score: %d attained %d times\nLowest score: %d attained %d times\n",max,numb,min,nums);
}
第一题:
#include<stdio.h>
void main()
{
int i,j,k,num=0;
for(k=0;k<=10;k++)
for(j=0;j<=20;j++)
for(i=0;i<=50;i++)
if(2*i+5*j+10*k==100)
{
printf("£1 =100p=2*%d+5*%d+10*%d\n",i,j,k);
num++;
}
printf("In total, there are %d possibilities to make £1",num);
}
第二题:
#include<stdio.h>
#include<string.h>
void main()
{
int a[15],i,max,min,nums=1,numb=1; /*nums最小数出现次数,numb最大数出现次数*/
printf("please input 15 scores (end with -1,less then 15 number intotal.\n shoulde be integer numbers between 0 and 20)\n");
for(i=0;i<=14;i++)
scanf("%d",&a[i]);
for(i=1,max=min=a[0];a[i]!=-1;i++)
{if(a[i]>max)
{max=a[i];numb=1;
}
else if(a[i]==max)numb++;
if(a[i]<min)
{min=a[i];nums=1;
}
else if(a[i]==min)nums++;
}
printf("Highest score: %d attained %d times\nLowest score: %d attained %d times\n",max,numb,min,nums);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询