一道简单的英文述题的C语言编程题。要求答案思路精确。谢!

题目描述
The first line integer means the number of input integer a and b. Your task is to Calculate a + b.

输入
Your task is to Calculate a + b. The first line integer means the numbers of pairs of input integers.

输出
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

样例输入
2
1 5
10 20

样例输出
6
30

首先要把题目看明白.
第一行的输入是指要输入a和b的对数,从第二行开始,每一行就是一对a,b,你的任务是计算a+b的结果,并在最后输出出来。
我定义1个整形变量n用来保存第一个输入,然后用它动态生成一个数组,用这个数组来保存每一行a+b的值,代码如下:
int main()
{
int n;
cin>>n;
int x,y;
int *p=new int[n];
for(int i=0;i<n;i++)
{
cin>>x>>y;
p[i]=x+y;
}
for(int i=0;i<n;i++)
{
cout<<p[i]<<endl;
}
delete p;
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-16
我用C语言写的一个
#include <stdio.h>
#define MaxSize 100
void main()
{
int count;
int m,n;
int i=0;
int sum[MaxSize];
scanf("%d",&count);;
while((count>0) && (scanf("%d%d",&m,&n))==2 )
{
sum[i]=m+n;
i++;
count--;
}
for(int j=0;j<i;j++)
{
printf("%d\n",sum[j]);
}
}
//count是用来输入的计算的数的对数
//sum是用来存储同一行上的两个数的和
第2个回答  2011-04-16
题目描述
第一行的整数表示输入整型变量a和b的组数。你的任务是计算a + b。

#include<malloc.h>
#include<stdlib.h>

struct cal
{
int a;
int b;
int sum;
};

void main()
{
int n,a,b,i;
struct cal *p;
scanf("%d",&n);
p=(struct cal *)malloc(n*sizeof(struct cal));
for(i=0;i<n;i++)
{
scanf("%d %d",&(p+i)->a,&(p+i)->b);
(p+i)->sum=(p+i)->a+(p+i)->b;
}
for(i=0;i<n;i++)
{
printf("%d\n",(p+i)->sum);
}

}

已运行通过了,还有什么问题请指教吧。。。
第3个回答  2011-04-20
IS 125 PER POUND
输出 ABC DDEEE EFHIINO OP 125 PPR RRSTU
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char str[]=;
char buffer[100],sign[100];
int cmp(const void* a,const void* b)
{
return *(char*)a-*(char*)b;
}
void init()
{
int i,j=0;
for(i=0;str[i]!='\0';i++)
{
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')) buffer[j++]=str[i];
}
buffer[j]='\0';
return ;
}
int main()
{
int i=0,j=0;
init();
qsort(buffer,strlen(buffer),sizeof(char),cmp); //先把字母捡出来排序,然后再把排好序的字母还回去

//puts(buffer);
for(i=0;str[i]!='\0';i++)
{
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')) str[i]=buffer[j++];
}
puts(str);
return 0;
}
另外,站长团上有产品团购,便宜有保证
相似回答