二道c語言題目,二級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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-29 14:18
下一篇 2024-12-29 14:18

相關推薦

  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • 學堂雲Python語言程序設計答案

    學堂雲Python語言程序設計是一門重要的計算機專業課程。它涵蓋了Python語言及其應用,包括基礎語法、函數、文件處理、數據結構、圖形界面和網絡編程等內容。在學習中,我們經常會需…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28
  • Python作為中心語言,在編程中取代C語言的優勢和挑戰

    Python一直以其簡單易懂的語法和高效的編碼環境而著名。然而,它最近的發展趨勢表明Python的使用範圍已經從腳本語言擴展到了從Web應用到機器學習等廣泛的開發領域。與此同時,C…

    編程 2025-04-28

發表回復

登錄後才能評論