mooc-程式設計-第一週and 第二週and第三週
**描述
輸入三個整數,把第二個輸入的整數輸出。**
輸入
只有一行,共三個整數,整數之間由一個空格分隔。整數是32位有符號整數。
輸出
只有一行,一個整數,即輸入的第二個整數。
樣例輸入
123 456 789
# include <iostream>
# include <cstdio>
using namespace std;
int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
printf("%d",b);
return 0;
}
**描述
給定一個字元,用它構造一個對角線長5個字元,傾斜放置的菱形。**
輸入
輸入只有一行, 包含一個字元。
輸出
該字元構成的菱形。
樣例輸入
*
樣例輸出
# include <iostream>
# include <cstdio>
using namespace std;
int main()
{
int a=1;
int i;
int j;
for(a=1;a <=5;a= a+2)
{
for(j=5;j>a;j=j-2)
{
printf (" ");
};
for(i=1;i<=a;i++)
{
printf("*");
};
printf("\n");
}
for(a=3;a <=5;a= a+2)
{
for(j=3;j<=a;j=j+2)
{
printf(" ");
};
for(i=5;i>=a;i--)
{
printf("*" );
};
printf("\n");
}
return 0;
}
總時間限制: 1000ms 記憶體限制: 65536kB
描述
輸入一個ASCII碼,輸出對應的字元。
輸入
一個整數,即字元的ASCII碼,保證存在對應的可見字元。
輸出
一行,包含相應的字元。
樣例輸入
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char a;
scanf("%c",&a);
int k = a;
printf("%d",k);
int b;
scanf("%d",&b);
char m = b;
printf("%c",m);
return 0;
}
第二週的程式碼
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%8d %8d %8d",a,b,c);
double d;
scanf("%lf",&d);
printf("%.12f",d);
空格分隔
總時間限制: 1000ms 記憶體限制: 65536kB
描述
讀入一個字元,一個整數,一個單精度浮點數,一個雙精度浮點數,然後按順序輸出它們,並且要求在他們之間用一個空格分隔。輸出浮點數時保留6位小數。
輸入
共有四行:
第一行是一個字元;
第二行是一個整數;
第三行是一個單精度浮點數;
第四行是一個雙精度浮點數。
輸出
輸出字元、整數、單精度浮點數和雙精度浮點數,之間用空格分隔。
樣例輸入
a
12
2.3
3.2
樣例輸出
a 12 2.300000 3.200000
int f;
char e;
float g;
double h;
scanf("%c%d%f%lf",&e,&f,&g,&h);
printf("%c %d %.6f %.6f",e,f,g,h);
return 0;
4、計算球的面積
總時間限制: 1000ms 記憶體限制: 65536kB
描述
對於半徑為r的球,其體積的計算公式為V=4/3*πr3,這裡取π= 3.14。
現給定r,求V。
輸入
輸入為一個不超過100的非負實數,即球半徑,型別為double。
輸出
輸出一個實數,即球的體積,保留到小數點後2位。
樣例輸入
#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
int main()
{
double i;
# define pi 3.14
double k;
scanf("%lf",&i);
double a = 4/3;
double m=pi*pow(i,3);
k=(4*(pi*pow(i,3)))/3;
printf("%lf",k);
printf("%lf",a);
printf("%lf",m);
return 0;
}
5、大象喝水
# define pi 3.14
int h,r;
scanf("%d%d",&h,&r);
int k = 20/((pi*r*r*h)/1000);
printf("%d",k);
return 0;
第三週
第一題奇數和偶數判斷
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
if (n%2==0)
printf("even");
else
printf("odd");
return 0;
}
第2題求解一元二次函式
#include<iostream>
#include<cstdio>
# include<math.h>
using namespace std;
int main()
{
double a,b,c;
scanf("%lf%lf%lf",&a,&b,&c);
double x1=(-b + sqrt(b*b-4*a*c))/(2*a);
double x2= (-b - sqrt(b*b-4*a*c))/(2*a);
if (b*b==(4*a*c))
if (x1==0.00000)
printf("x1=x2=%.5f",0.00000);
else
printf("x1=x2=%.5f",x1);
else if(b*b>(4*a*c))
printf("x1=%.5f;x2=%.5f",x1,x2);
else
if (-b / (2*a)==0.00000)
printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi", 0.00000,sqrt(4*a*c-b*b) / (2*a),0.00000,sqrt(4*a*c-b*b) / (2*a));
else
printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi", -b / (2*a),sqrt(4*a*c-b*b) / (2*a),-b / (2*a),sqrt(4*a*c-b*b) / (2*a));
return 0;
}
第三題會使用邏輯運算子號
#include<iostream>
#include<cstdio>
# include<math.h>
using namespace std;
int main()
{
int a,b;
scanf("%d%d",&a,&b);
if( (a>=-1 && a<=1) &&(b>=-1 && b<=1))
printf("yes");
else
printf("no");
return 0;
}
第四個問題
#include<iostream>
#include<cstdio>
# include<math.h>
using namespace std;
int main()
{
int n,x,y;
scanf("%d%d%d",&n,&x,&y);
float y1 = y;
float x1 = x;
float m = y1/x1;
int k = ceil(m);
int t = n-k;
if (t<=0)
printf("%d",0);
else
printf("%d",n-k);
return 0;
}
第五題 簡單的計算器
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int a,b,c;
char ys;
cin >> a >> b >> ys;
switch(ys){
case'+':c = a+b;
printf("%d",c);
break;
case'-':c = a-b;
printf("%d",c);
break;
case'*':c = a*b;
printf("%d",c);
break;
case'/':
if(b==0)
printf("Divided by zero!");
else
printf("%d",a/b);
break;
default:printf("Invalid operator!");
break;
}
}
第四周
include
include
using namespace std;
char Left[3][7];
char Right[3][7];
char result[3][7];
bool IsFake(char c,bool light){
for(int i = 0; i<3;++i){
char *pLeft,*pRight;
if(light){
pLeft = Left[i];
pRight = Right[i];
}
else{
pLeft = Right[i];
pRight = Left[i];
}
switch(result[i][0]){
case ‘u’:
if(strchr(pRight,c)==NULL)
return false;
break;
case’e’:
if(strchr(pLeft,c)||strchr(pRight,c))
return false;
break;
case’d’:
if(strchr(pRight,c)==NULL)
return false;
break;
}
}
return true;
}
int main(){
int t;
cin >> t;
while(t--){
for(int i = 0;i<3;++i)
cin >> Left[i] >>Right[i]>>result[i];
for(char c = 'A';c<='L';c++){
if(IsFake(c,true)){
cout << c <<"it is heavy";
break;
}
else if(IsFake(c,false)){
cout << c << "it is light";
break;
}
}
}
}
第一題 計算求整數的和和平均數
include
include
using namespace std;
int main(){
int n;
int s=0;
double a[10000];
scanf(“%d”,&n);
for(int i =0; i
第二題求最大跨度
include
include
using namespace std;
int main(){
int n;
int a[1000];
scanf(“%d”,&n);
for(int i =0; i
第三題奧運獎盃計數
include
include
using namespace std;
int main(){
int n;
int a[17];
int b[17];
int c[17];
int d[17];
int s = 0;
int a1 = 0;
int b1 = 0;
int c1 = 0;
scanf(“%d”,&n);
for(int k =0; k
第四題乘方計算
include
include
using namespace std;
int main(){
int a,n,b=1;
scanf(“%d %d”,&a,&n);
for(int i =0;i
最後一體
include
include
using namespace std;
int main(){
int n,a,b;
int c[30];
int d[30];
scanf(“%d”,&n);
if(n<=1&&n>20)
exit(0);
scanf(“%d %d”,&a,&b);
if(a<=0&&n>10000)
exit(0);
for(int k=0;k