本文目錄一覽:
- 1、2道c語言編程問題
- 2、2道c語言問題求教 !
- 3、兩道c語言題
- 4、2道c語言題目
- 5、兩道簡單的C語言題目
2道c語言編程問題
第一題:
#include iostream
#include stdlib.h
#includestdio.h
#includestring.h
using namespace std;
int main()
{
int num;
printf(“Enter the number of stock value:”);
while(scanf(“%d”,num)!= EOF)
{
int res = 0;
int max = 0;
if(num == -1)
{
return 0;
}
if(num == 0)
{
printf(“Enter the number of stock value:”);
continue;
}
int a[num-1];
for(int i = 0;inum-1;i++)
{
scanf(“%d “,a[i]);
}
fflush(stdin);//刷新緩衝區
for(int i = 0;inum;i++)
{
if(maxa[i])
max = a[i];
if(i0)
{
if((a[i] – max)res)
res = a[i] – max;
}
}
printf(“%d”,res);
printf(“\nEnter the number of stock value:”);
}
return 0;
}
第二題
#include iostream
#include stdlib.h
#includestdio.h
#includestring.h
using namespace std;
void printSelection(int money,int bread,int ice,int coke,int coffee)
{
if(money=0)
{
if(money =700)
{
ice = money/700;
money = money%700;
printSelection(money, bread, ice, coke, coffee);
while(ice–)
{
if(ice=0)
{
money = money + 700 – 100;
coffee++;
printSelection(money, bread, ice, coke, coffee);
}
}
}
else if(money=500)
{
bread = money/500;
money = money%500;
printSelection(money, bread, ice, coke, coffee);
while(bread–)
{
if(bread=0)
{
money = money + 500 – 100;
coffee++;
printSelection(money, bread, ice, coke, coffee);
}
}
}
else if(money=400)
{
coke = money/400;
money = money%400;
printSelection(money, bread, ice, coke, coffee);
while(coke–)
{
if(coke=0)
{
coffee = coffee+4;
printSelection(money, bread, ice, coke, coffee);
}
}
}
else {
coffee = coffee + money/100;
printf(“Bread(%d),Icecream(%d),Coke(%d),Coffee(%d).\n”,bread+1,ice+1,coke+1,coffee+1);
}
}
}
int main()
{
int money;
printf(“Enter your money (=2,000):”);
while(scanf(“%d”,money)!= EOF)
{
if(money == -1)
{
return 0;
}
if(money 2000)
{
printf(“You must enter more than 2,000.\n”);
}
else if((money%100) != 0)
{
printf(“The monetary unit should be 100 won.\n”);
}
else
{
money = money – (500*1 + 700*1 + 400*1 + 100*1);
printSelection( money, 0, 0, 0, 0);
}
printf(“Enter your money (=2000):”);
}
return 0;
}
2道c語言問題求教 !
第一題
第一個循環的時候,把x[i].a(i取0 1 2)分別賦值為1,2,3,x[i].b分別賦值為5,6,7
而x[i].next總是指向下一個數值,最後一個則指向第一個,是一個循環
第二個循環里輸出一個x[0].a,然後輸出下一個數x[1].b,然後是下一個x[2].a,然後是x[0].b…..以此循環
為了形象點,我們把x[i].a和x[i].b寫成兩行,
3 4 5 3 4 5
5 6 7 5 6 7
輸出就是
按這張圖的紅線依次輸出,循環3次,每次輸出兩個,就是輸出6個數,結果是365547
第二題
是求一個數能不能表示成兩個素數相加,pr函數的功能是判斷一個數是否是素數,每次循環判斷k和m-k是不是都是素數,都是素數說明該數可以表示成兩個素數相加
輸入30,輸出就是30=7+23
兩道c語言題
第一道:65535,0
我們首先要求-1的補碼:一個負數的補碼, 是它對應的正數的補碼按位取反並在末位加1.
求-1的補碼, 先看正1的補碼。
正1的二進制數是0000000000000001, 它的補碼是
1111111111111110, 然後末位加1, 就變成了1111111111111111,這正是65535對應的二進制數(PS:unsigned int 型的取值範圍是0~65535),因此a輸出是65535.
對於b我們知道65536超出了範圍,因此裝不下的那部分會被截斷。
————————————————————————————————————
詳細解釋:
第二道:123456.123
123.123
首先我們要知道:%5.3f是控制輸出格式的
f表示輸出浮點數,
5表示最小輸出字符寬度為5位數,
3表示浮點數輸出小數點後為3位數
因此答案不難理解了。
有什麼不懂得,歡迎追問啊~~~~
2道c語言題目
同學你這是100分的題啊,難怪沒人回答。你等等吧,我有空幫你寫
#include “stdio.h”
#include “stdafx.h”
#includeiostream
#includeiomanip
using namespace std;
float average();
int findfail(int i);
int findgood(int i);
struct student
{
int no;
int score[5];
float vag;
};
student stu[3];//定義學生人數(本列為3人)
void main()
{
int i,j;
cout”請輸入學生學號及其五門成績!”endl;
for(i=0;i3;i++)
{
cinstu[i].no;
stu[i].vag=0;
for(j=0;j5;j++)
{
cinstu[i].score[j];
stu[i].vag+=stu[i].score[j];
}
stu[i].vag=stu[i].vag/5;
}
cout”******第一門成績的平均分******”endl;
coutsetprecision(3)average()endlendlendl;
cout”有2門以上(含2門)課程不及格的學生”endl;
cout.width(5);
cout” 學號 “”********成績列********”” 平均成績”endl;
for(i=0;i3;i++)
if(findfail(i)!=-1)
{
cout.width(5);
coutstu[i].no;
for(j=0;j5;j++)
{
cout.width(5);
coutstu[i].score[j];
}
cout.width(10);
coutstu[i].vagendl;
}
coutendlendl;
cout”平均成績在90分以上或全部課程成績在85分以上的學生”endl;
cout” 學號 “endl;
for(i=0;i3;i++)
if(findgood(i)!=-1)
{
cout.width(5);
coutstu[i].noendl;
}
coutendl;
system(“pause”);
}
float average()
{
int i;
float vag1=0;
for(i=0;i3;i++)
vag1+=stu[i].score[0];
vag1=vag1/3;
return vag1;
}
int findfail(int i)
{
int j,cl=0;
for(j=0;j5;j++)
if(stu[i].score[j]60)
cl++;
if(cl=2)
return i;
else
return -1;
}
int findgood(int i)
{
int j,f1=0,f2=1;
if(stu[i].vag=90)
f1=1;
for(j=0;j5;j++)
if(stu[i].score[j]85)
f2=0;
if(f1||f2)
return i;
else
return -1;
}
#include “stdio.h”
#include “stdafx.h”
#includeiostream
#includeiomanip
using namespace std;
void multiply();
int a[5][5],b[5][5],c[5][5],m,n;
void main()
{
int i,j;
cout”input m n:”;
coutendl;
cinmn;
cout”input A(mxn)”endl;
for(i=0;im;i++)
for(j=0;jn;j++)
cina[i][j];
cout”input B(nxm)”endl;
for(i=0;in;i++)
for(j=0;jm;j++)
cinb[i][j];
multiply();
cout”C(mxm) is:”endl;
for(i=0;im;i++)
{
for(j=0;jm;j++)
coutsetw(5)c[i][j];
coutendl;
}
system(“pause”);
}
void multiply()
{
int i,j,k,sum;
for(k=0;km;k++)
for(i=0;im;i++)
{
sum=0;
for(j=0;jn;j++)
sum+=a[k][j]*b[j][i];
c[k][i]=sum;
}
}
這是我寫好的,不過我是用C++寫的(我電腦里沒有C的環境),你如果有C++的話,就可以直接運行了,如果是C的話,就把那些輸入輸出改一下就行了。比如C++里輸入用的是”cin”你就把那一行改成C的scanf,C++的輸出是用”cout”,你也是把相應的行改成C的printf就行了
兩道簡單的C語言題目
一、首先分析這段函數實現的功能
main()
{int op1,op2,res;
char opertor;
scanf (“%d”,op1); //op1為輸入的整數
opertor=getchar(); //opertor為輸入的字符
while(opertor!=’=’) //如果輸入的字符不是『=』則繼續
{
scanf (“%d”,op2); //op2 為輸入的第二個整數
switch(opertor) //判斷opertor為何種運算,並求出結果,如果opertor不符合4則運算op1=0,重新等待輸入運算符,重複以上過程,直到輸入『=』,求出最終結果
{
case ‘+’:res=op1+op2;break;
case ‘-‘:res=op1-op2;break;
case ‘*’:res=op1*op2;break;
case ‘/’:res=op1/op2;break;
default:res=0;
}
op1=res;
opertor=getchar();
}
printf(“%d\n”,res);
}
可以看出,這個程序實現的運算是從左到右,而不去分運算優先級,加減和乘除都是平等,從左向右計算即可。
二、分析程序功能
#include stdio.h
#define MAXLEN 80
main()
{int k=0,number=0;
char str[MAXLEN];
while((str[k]=getchar())!=’#’) //讀取字符,直到『#』結束循環
{
k++;
str[k]=’\0′; //將讀取的字符加入到字符數組中,尾部加上『\0』
for(k=0;str[k]!=’\0′;k++)
{
if(str[k]=’0′ str[k]=’9’||str[k]==’A’||str[k]==’B’) //12進制數轉換為10進制,注意A B大寫,小寫的不合法
if(str[k]=’0′ str[k]=’9′)
number=number*12+str[k]-‘0’;
else if(str[k]==’A’||str[k]==’B’)
number=number*12+str[k]-‘A’+10;
else ;//第15行 //字符串中含有不合法的字符即,不是0——9,A,B,忽略,繼續執行
else break; //第16行 // 字符串中含有不合法的字符,即,不是0——9,A,B,則跳出循環
}
printf(“%d\n”,number);
}
你的程序少打了一些『{}』,我填上去了。你如果會12進制轉換為10進制的方法,那麼上面的轉換過程就很簡單了。
程序理解了,題目就很容易了
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/300862.html