2020-03-26
while循环 scanf的赋值 switch
1.
#include<stdio.h>
int main(){
char a;
int i=1;
while(scanf("%c",&a)){
if(a=='#'){
i=i-1;
}else{
if(a>='A'&&a<='Z'){
a=a+32;
printf("%c\n",a);
}else if(a>='a'&&a<='z'){
a=a-32;
printf("%c\n",a);
}
while((a>='A'&&a<='Z')&&(a>='a'&&a<='z')||a!='#'){
printf("请输入字母!\n");
break;
}
scanf("%c",&a);
}
}
}
2.
#include<stdio.h>
int main(){
int a;
int i=1;
int b;
int x=0;
while(scanf("%d",&a)){
for(b=1;b<=a/2;b=b+1){
if(a%b==0){
x=x+b;
}
}
if(a==x){
printf("%d是完数\n",a);
}else{
printf("%d不是完数\n",a);
}
x=0;
}
}
3.
#include<stdio.h>
int main(){
float a;
float b;
float c;
while(scanf("%f%f",&a,&b)){
if(a>b){
float d;
d=(a-b)/(a*b);
printf("%f",d);
}else{
c=a;
a=b;
b=c;
float d;
d=(a-b)/(a*b);
printf("%f",d);
}
}
}
4.
#include<stdio.h>
int main(){
int a;
int b;
int m;
int i;
printf("请输入上限和下限整数:");
while(scanf("%d%d",&a,&b)&&a>b) {
int x=0;
m=a-b;
for(i=1;i<=m+1;i++){
x=x+b*b;
b++;
}
printf("%d\n",x);
}
}
5.
#include<stdio.h>
int main()
{
int year,month;
scanf("%d%d",&year,&month);
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:printf("%d月有31天",month);break;
case 4:
case 6:
case 9:
case 11:printf("%d月有30天",month);break;
case 2:if((year%100==0&&year%400==0)||(year%100!=0&&year%4==0))
{
printf("%d月有29天",month);
}
else
{
printf("%d这个月有28天",month);
}
break;
}
}
6.
#include<stdio.h>
int main(){
int i=1;
int s=0;
while(s<1000){
s=s+i;
i=i+1;
}
printf("第%d项",i-1);
}