當前位置:首頁 » 編程語言 » c語言編程問題

c語言編程問題

發布時間: 2022-07-10 06:42:30

1. c語言編程問題

1、輸入有范圍,兩個變數輸入都需要驗證,方法變數處置范圍以外,循環判斷輸入。

2、天數循環計數,日期0~6周期自增。用變數累加計數結果。

下面是演示代碼:

#include<stdio.h>

int main()

{

int i,j,a=7,n=366,cnt=0;

while(a<0 || a>6)

printf("請輸入a的值(0~6):"),scanf("%d",&a);

while(n<0 || n>365)

printf("請輸入n的值(0~365):"),scanf("%d",&n);

for(i=0,j=a;i<n+1;i++,j++)

{

if(j==4) cnt++;

if(j==6) j=-1;

}

printf("上機次數%d次 ",cnt);

return 0;

}

2. C語言編程問題,急求答案

使用冒泡的演算法,將p後面的n-p-1個元素向前交換p+1次:


#include "stdafx.h"

#include <iostream>

using namespace std;

#define N 1000

int main()

{

int n, p;

int num[N];

cout << "請輸入n值和p值:" << endl;

cin >> n >> p;

cout << "請輸入" << n << "個數:" << endl;

for (int i = 0; i < n; i++)

cin >> num[i];

for (int i = 0; i <= p; i++)

{

for (int j = 0; j < n - p - 1; j++)

{

int temp = num[p+j-i];

num[p + j-i] = num[p + j + 1-i];

num[p + j + 1-i] = temp;

}

}

for (int i = 0; i < n; i++)

cout << num[i] << " ";

cout << endl;

system("pause");

return 0;

}

3. 關於c語言編程問題!

long
fun(int
n)//返回值是長整型,輸入為整型
{
int
i;long
s;//此句應該給s賦初值1,改為long
s=1;
for(i=1;i<=n;i++)//n控制循環次數
s*=i;//s=s*i完成1到n的累乘,即
s=1*2*3*4....*n
return(s);
}
main()
{int
k,n;
long
s;
scanf("%d",&n);//鍵盤輸入n的值
s=0;
for(k=0;k<=n;k++)
s+=fun(k);//s=s+fun(k),完成累加功能,即s=f(0)+f(1)+f(2)....+f(n),f(x)是x的階乘
printf("%d\n",s);
}
如果輸入n=5的話,最後結果為s=0!+1!+2!+3!+4!+5!不知道你明白了沒
a-=2是
a=a-2的簡寫變數運算符=常量相當於變數=變數運算符常量

4. C語言編程問題

#include<stdio.h>

#include<string.h>

#include<ctype.h>

int main()

{ char s[1000],s1[100],*p,*q,*q1,*t,s2[100];

int n;

gets(s);

q=s+strlen(s);

while(1)

{ gets(s1);

if(strcmp(s1,"#")==0)break;

for(q=s+strlen(s)-1; isalpha(*q); q--)

if(islower(*q))*q-=32;

q=s+strlen(s);

for(p=s1; isalpha(*p); p++);

strcpy(q,p);

}

puts(s);

return 0;

}

5. c語言編程問題

#include<iostream>

usingnamespacestd;

constPi=3.1415926;
typedefstructpoint{
doublex,y;
}point;

classShape{
public:
virtualdoublegetArea()const=0;
};
classRectangle:publicShape{
doublelength,width;
pointx,y;
public:
Rectangle(doublelen,doublewid):length(len),width(wid){}
Rectangle(pointp1,pointp2){
length=p2.x-p1.x;
width=p2.y-p1.y;
}
doublegetArea()const{returnlength*width;}
};

classCircle:publicShape{
pointcenter;
doubleradius;
public:
Circle(doubler):radius(r){}
Circle(pointcen,doubler){
center=cen;
//center.x=cen.x;
//center.y=cen.y;
radius=r;
}
doublegetArea()const{returnPi*radius*radius;}
};
intmain(){
Shape*s[4];
doublelen=4,wid=5,r=1.0;
pointpos0={1,1},pos1={0,0},pos2={5,6};
s[0]=newCircle(pos0,r);
s[1]=newRectangle(pos1,pos2);
s[2]=newRectangle(len,wid);
s[3]=newCircle(r);
for(inti=0;i<4;++i)
cout<<s[i]->getArea()<<endl;
return0;
}

6. C語言編程問題

程序運行時先進入第一個switch語句,即switch(a>0),你可能認為它是值是2,所以直接執行default了吧?其實它的值不是2,而是1,因為a>0是一個表達式,它返回的是真或假的值,因為a=2,是大於0的,所以這個式子成立,所以返回的值為真,在C中"真"用1表示,所以當a=2時switch(a>2)就等價於switch(1),執行第二個switch語句,即執行switch(b<0)因為b=7,是不小於0的,返回值為假(C中用0表示),所以這個語句等價於switch(0),而這個switch里沒有case
0,所以跳出這個switch,不過要注意switch(a>0)中的case
1:中是一個switch語句,而這個語句後面是沒有break的,所以它要再執行

case
0:
switch(c==5)

{
case
0: printf("*");
break;

case
1: printf("#");
break;

case
2: printf("$");
break;

}

這個部分,c==5的返回值是1,所以輸出一個"#",而case
0:switch(c==0)這個語句後面還是沒有break,所以就要再執行default,即輸出一個"&",兩次輸出後顯示在屏幕上的當然就是"#&"了

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:432
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:743
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:537
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:146
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:235
java駝峰 發布:2025-02-02 09:13:26 瀏覽:651
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:538
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726