int sum=0;
for(int i=1;i<=1000;i++){
sum+=i%2?i:0;
}
printf("%d\n",sum/10);
int sum=0;
for(int i=1;i<=1000;i++){
sum+=i%2?i:0;
}
printf("%d\n",sum/10);
int sum=0;
for(int i=1;i<1000;i+=2){
sum+=i;
}
printf("%d\n",sum/10);
int sum=0;
for(int i=1;i<1000;i+=2){
sum+=i;
}
printf("%d\n",sum/10);
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a*a+b*b);
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a*a+b*b);
3.已知函数y=f(x),编程实现输入一个x值,输出y值。
2x+1 (x<0)
y= 0 (x=0)
2x-1 (x>0)
double x,y;
scanf("%lf",&x);
if(x<0){
y=2*x+1;
}else if(x>0){
y=2*x-1;
}else{
y=0;
}
printf("%f\n",y);//10
double x,y;
scanf("%lf",&x);
if(x<0){
y=2*x+1;
}else if(x>0){
y=2*x-1;
}else{
y=0;
}
printf("%f\n",y);//10
printf输出float和double都可以用%f,double还可以用%lf。
scanf输入float用%f,double输入用%lf,不能混用
printf输出float和double都可以用%f,double还可以用%lf。
scanf输入float用%f,double输入用%lf,不能混用
#include <stdio.h>
int main(){
int x,y,max(int a,int b);
scanf("%d%d",&x,&y);
printf("最大值=%d\n",max(x,y));
}
int max(int a,int b){
return a>b?a:b;
}
#include <stdio.h>
int main(){
int x,y,max(int a,int b);
scanf("%d%d",&x,&y);
printf("最大值=%d\n",max(x,y));
}
int max(int a,int b){
return a>b?a:b;
}
6. 从键盘输入10个整数,统计其中正数、负数和零的个数,并在屏幕上输出。
int a[10],x=0,y=0,z=0;
for(int i=0;i<10;i++){
scanf("%d",&a[i]);
if(a[i]>0){
x++;
}else if(a[i]<0){
y++;
}else{
z++;
}
}
printf("正数个数=%d;\n负数个数=%d;\n零的个数=%d;\n",x,y,z);
return 0;
int a[10],x=0,y=0,z=0;
for(int i=0;i<10;i++){
scanf("%d",&a[i]);
if(a[i]>0){
x++;
}else if(a[i]<0){
y++;
}else{
z++;
}
}
printf("正数个数=%d;\n负数个数=%d;\n零的个数=%d;\n",x,y,z);
return 0;
#include <stdio.h>
void main( ) {
int a, i,p=0,n=0,z=0;
printf("please input number");
for(i=0;i<10;i++){
scanf("%d,",&a);
if (a>0){
p++;
}else if (a<0){
n++;
} else{
z++;
}
}
printf("正数:%5d, 负数:%5d,零:%5d\n",p,n,z);
}
#include <stdio.h>
void main( ) {
int a, i,p=0,n=0,z=0;
printf("please input number");
for(i=0;i<10;i++){
scanf("%d,",&a);
if (a>0){
p++;
}else if (a<0){
n++;
} else{
z++;
}
}
printf("正数:%5d, 负数:%5d,零:%5d\n",p,n,z);
}
10.有一个分数序列:2/1,3/2,5/3,8/5,13/8,……编程求这个序列的前20项之和。
解法1:
double x[20],y[20];
x[0]=2.0;
x[1]=3.0;
y[0]=1.0;
y[1]=2.0;
double sum=x[0]/y[0]+x[1]/y[1];
for(int i=2;i<20;i++){
x[i]=x[i-1]+x[i-2];
y[i]=y[i-1]+y[i-2];
sum+=x[i]/y[i];
}
printf("%f\n",sum);
double x[20],y[20];
x[0]=2.0;
x[1]=3.0;
y[0]=1.0;
y[1]=2.0;
double sum=x[0]/y[0]+x[1]/y[1];
for(int i=2;i<20;i++){
x[i]=x[i-1]+x[i-2];
y[i]=y[i-1]+y[i-2];
sum+=x[i]/y[i];
}
printf("%f\n",sum);
font-family:"Times New Roman";
}
@font-face{
font-family:"宋体";
}
@font-face{
font-family:"微软雅黑";
}
@font-face{
font-family:"Tahoma";
}
@font-face{
font-family:"Symbol";
}
@font-face{
font-family:"Arial";
}
@font-face{
font-family:"黑体";
}
@font-face{
font-family:"Courier New";
}
@font-face{
font-family:"Wingdings";
}
@font-face{
font-family:"Calibri";
}
@font-face{
font-family:"Verdana";
}
@font-face{
font-family:"Cambria";
}
p.p0{
margin:0pt;
margin-bottom:0.0001pt;
margin-bottom:0pt;
margin-top:0pt;
text-align:justify;
font-size:10.5000pt; font-family:'Calibri'; }
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}
解法二:
//10.有一个分数序列:2/1,3/2,5/3,8/5,13/8,……编程求这个序列的前20项之和。
#include <stdio.h>
void main()
{
double a=1,b=2,sum=0;
for(int i=1;i<=20;i++){
double temp = b;
sum+= b/a;
b=a+b;
a=temp;
}
printf("%20f",sum);
}
//10.有一个分数序列:2/1,3/2,5/3,8/5,13/8,……编程求这个序列的前20项之和。
#include <stdio.h>
void main()
{
double a=1,b=2,sum=0;
for(int i=1;i<=20;i++){
double temp = b;
sum+= b/a;
b=a+b;
a=temp;
}
printf("%20f",sum);
}
求100~300间能被3整除的数的和。
int sum=0;
for(int i=100;i<300;i++){
if(i%3==0){
sum+=i;
}
}
printf("%5d\n",sum);
int sum=0;
for(int i=100;i<300;i++){
if(i%3==0){
sum+=i;
}
}
printf("%5d\n",sum);
font-family:"Times New Roman";
}
@font-face{
font-family:"宋体";
}
@font-face{
font-family:"微软雅黑";
}
@font-face{
font-family:"Tahoma";
}
@font-face{
font-family:"Symbol";
}
@font-face{
font-family:"Arial";
}
@font-face{
font-family:"黑体";
}
@font-face{
font-family:"Courier New";
}
@font-face{
font-family:"Wingdings";
}
@font-face{
font-family:"Calibri";
}
@font-face{
font-family:"Verdana";
}
@font-face{
font-family:"Cambria";
}
p.p0{
margin:0pt;
margin-bottom:0.0001pt;
margin-bottom:0pt;
margin-top:0pt;
text-align:justify;
font-size:10.5000pt; font-family:'Calibri'; }
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}
计算:1+12+123+1234+12345。
int sum=0,temp=0;
for(int i=1;i<=5;i++){
temp=temp*10+i;
sum+=temp;
printf("%5d\n",temp);
}
printf("%5d\n",sum);
int sum=0,temp=0;
for(int i=1;i<=5;i++){
temp=temp*10+i;
sum+=temp;
printf("%5d\n",temp);
}
printf("%5d\n",sum);
font-family:"Times New Roman";
}
@font-face{
font-family:"宋体";
}
@font-face{
font-family:"微软雅黑";
}
@font-face{
font-family:"Tahoma";
}
@font-face{
font-family:"Symbol";
}
@font-face{
font-family:"Arial";
}
@font-face{
font-family:"黑体";
}
@font-face{
font-family:"Courier New";
}
@font-face{
font-family:"Wingdings";
}
@font-face{
font-family:"Calibri";
}
@font-face{
font-family:"Verdana";
}
@font-face{
font-family:"Cambria";
}
p.p0{
margin:0pt;
margin-bottom:0.0001pt;
margin-bottom:0pt;
margin-top:0pt;
text-align:justify;
font-size:10.5000pt; font-family:'Calibri'; }
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}
求x的y次方
#include <stdio.h>
int main(){
int power(int x, int y);
printf("%d\n",power(2,3));
}
//求x的y次方
int power(int x, int y){
int sum=1;
for(int i =1;i<=y;i++){
sum*=x;
}
return sum;
}
#include <stdio.h>
int main(){
int power(int x, int y);
printf("%d\n",power(2,3));
}
//求x的y次方
int power(int x, int y){
int sum=1;
for(int i =1;i<=y;i++){
sum*=x;
}
return sum;
}
font-family:"Times New Roman";
}
@font-face{
font-family:"宋体";
}
@font-face{
font-family:"微软雅黑";
}
@font-face{
font-family:"Tahoma";
}
@font-face{
font-family:"Symbol";
}
@font-face{
font-family:"Arial";
}
@font-face{
font-family:"黑体";
}
@font-face{
font-family:"Courier New";
}
@font-face{
font-family:"Wingdings";
}
@font-face{
font-family:"Calibri";
}
@font-face{
font-family:"Verdana";
}
@font-face{
font-family:"Cambria";
}
p.p0{
margin:0pt;
margin-bottom:0.0001pt;
margin-bottom:0pt;
margin-top:0pt;
text-align:justify;
font-size:10.5000pt; font-family:'Calibri'; }
@page{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}@page Section0{
}
div.Section0{page:Section0;}
4.从键盘输入一个字符,判断它是否是英文字母。
#include <stdio.h>
void main()
{
char c;
printf("input a character:");
c=getchar();
if(c>='A'&&c<='Z'||c>='a'&&c<='z') {
printf("Yes\n");
}else{
printf("No\n");
}
}
#include <stdio.h>
void main()
{
char c;
printf("input a character:");
c=getchar();
if(c>='A'&&c<='Z'||c>='a'&&c<='z') {
printf("Yes\n");
}else{
printf("No\n");
}
}
//13、从键盘输入n个数存放在数组中,将最小值与第一个数交换,输出交换后的n个数。
#include <stdio.h>
void main()
{
int n=0, a[n], temp,index=0;
printf("请输入需要输入数字的个数\n");
scanf("%d",&n);
printf("请输入需要输入数字序列\n");
//输入并查找最小值
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
if(a[i]<a[index]){
index=i;
}
}
temp=a[0];
a[0]=a[index];
a[index]=temp;
printf("下面输出交换后数组\n");
for(int i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
//13、从键盘输入n个数存放在数组中,将最小值与第一个数交换,输出交换后的n个数。
#include <stdio.h>
void main()
{
int n=0, a[n], temp,index=0;
printf("请输入需要输入数字的个数\n");
scanf("%d",&n);
printf("请输入需要输入数字序列\n");
//输入并查找最小值
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
if(a[i]<a[index]){
index=i;
}
}
temp=a[0];
a[0]=a[index];
a[index]=temp;
printf("下面输出交换后数组\n");
for(int i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}