❶ c语言 四个小组,每个小组五个人,求每个小组的总成绩,平均成绩。(用双循环编程,用while循环)
#include<stdio.h>
int main()
{
int a[4][5]={{1,2,3,4,11},{5,6,7,8,22},{9,10,11,12,33},{13,14,15,16,44}};
int i,j,line=0,sum=0;
double aver;
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
line+=a[i][j];
printf("第%d小组总成绩为%d\n",i+1,line);
sum+=line;
}
aver=sum/4.0;
printf("全体成员平均成绩为%.2f\n",aver);
return 0;
}
❷ C语言打印结构体的成员名以及它的值。
你说的是反射,C语言没有这个语法机制。
❸ c语言结构体数组的输入输出
C
语言中,结构体(struct)是一种数据结构,是C语言中聚合数据类型(aggregate data type)的一类。结构体可以被声明为变量、指针或数组等,用以实现较复杂的数据结构。
结构体同时也是一些元素的集合,这些元素称为结构体的成员(member),且这些成员可以为不同的类型,成员一般用名字访问。
定义与声明
结构体的定义如下所示,struct为结构体关键字,tag为结构体的标志,member-list为结构体成员列表,其必须列出其所有成员;variable-list为此结构体声明的变量。
在实际应用中,C语言结构体数组常被用来表示一个拥有相同数据结构的群体,比如一个班的学生、一个车间的职工等。
在C语言中,定义结构体数组和定义结构体变量的方式类似;
例:
struct stu{
char*name;//姓名
int num;//学号
int age;//年龄
char group;//所在小组
float score;//成绩
}class[5];
表示一个班级有5个学生。
(3)c语言输出小组成员扩展阅读:
结构体数组的引用与引用一个结构体变量在原理上是一样的。只不过结构体数组中有多个结构体变量,我们只需利用for循环一个一个地使用结构体数组中的元素。
下面编写一个程序,编程要求:从键盘输入5个学生的基本信息,如姓名、年龄、性别、学号,然后将学号最大的学生的基本信息输出到屏幕。
#include<stdio.h>
#include<string.h>
struct STU
{
char name[20];
int age;
char sex;
char num[20];
};
void OutputSTU(struct STU stu[5]);
//函数声明,该函数的功能是输出学号最大的学生信息
int main(void)
{
int i;
struct STU stu[5];
for(i=0;i<5;++i)
{
printf("请输入第%d个学生的信息:",i+1);
scanf("%s%d%c%s",
stu<i>.name,
&stu<i>
.age,&stu<i>
.sex,stu<
i>.num);/*%c
前面要加空格,不然输入时会将空格赋给%c*/
}
OutputSTU(stu);
return 0;
}
void OutputSTU(struct STU stu[5])
{
struct STU stumax=stu[0];
int j;
for(j=1;j<5;++j)
{
if(strcmp(stumax.num,stu[j]
.num)<0)//strcmp函数的使用
{
stumax=stu[j];
}
}
printf("学生姓名:%s学生年龄:%d学生性别:%c学生学号:
%s ",stumax.name,
stumax.age,stumax.
sex,stumax.num);
}
输出结果是:
请输入第1个学生的信息:小红22 F Z1207031
请输入第2个学生的信息:小明21 M Z1207035
请输入第3个学生的信息:小七23 F Z1207022
请输入第4个学生的信息:小欣20 F Z1207015
请输入第5个学生的信息:小天19 M Z1207024
学生姓名:小明学生年龄:21学生性别:M学生学号:Z1207035
❹ c语言作业求答案
在主界面,即在主函数里面,用输出函数求出这些选择信息,并用编号对其编号,根据选择,如果输入编号等于某个值,调用对应的函数即可。
❺ C语言小组有五人,信息有,学号,姓名和成绩。要从键盘入他们的信息,并求出平均成绩以及最高成绩者的信息
#include "stdio.h"
int main()
{
string stu[5][3];
printf("请按学号、姓名、成绩顺序输入\n");
for(int i=0;i<5;i++)
{
printf("请输入第 "i+1"个人的信息:\n");
for(int j=0;j<3;j++)
{
scanf("%s",&stu[i][j]);
}
}
int score=0; //记录最高分
int flag=0; //记录最高分人的编号
for(int i=0;i<5;i++)
{
int sum=(int)stu[i][2];
if(score<( int)stu[i][2])
{
score=(int)stu[i][2];
flag=i;
}
}
printf("平均成绩为:"sum/5);
printf("成绩最优学生的信息为:\n 学号:%s,姓名:%s,成绩:%s",stu[flag][0],stu[flag][1],stu[flag][2]);
return 0;
}
好久没编了,现在也没编辑器,直接写的 可能数组转换有误,你可以参考一下!
❻ C语言如何将结构体中的所有成员按照其中一个成员的排序方式输出
将结构体数组SI[MAX]使用排序算法然后输出即可。
以下给题主列出对SI[MAX]的冒泡排序代码:
voidbubbleSort(structSalary_Infoarr[],intlen){
inti,j
structSalary_Infotemp;
for(i=0;i<len-1;i++)
for(j=0;j<len-1-i;j++)
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
调用bubbleSort函数:
bubbleSort(SI,MAX)/*注意这里的MAX需要换成实际的数组长度(职工人数)*/
❼ 急求!C语言帮忙编程!
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define szSTR 256
#define szNAME 10
#define szGROUP 5 /*每个小组最多允许5名组员,包括组长*/
#define fileAssignments "c:\\0.txt"
#define fileProjects "c:\\1.txt"
#define cntTYPE 4
/*题目*/
char Types[cntTYPE][11] = {"数据结构类", "趣味类", "管理类", "自选类"};
struct TAssignment {
int number; /*题目编号*/
int level; /*难度系数*/
int type; /*类别*/
char title[szSTR];
char requirements[szSTR]; /*技术要求*/
char features[szSTR]; /*技术特点*/
};
time_t Date_Input(char * msg)
{
struct tm t; memset(&t, 0, sizeof(t));
printf("请输入%s日期,格式为年/月/日:", msg);
scanf("%d/%d/%d", &(t.tm_year), &(t.tm_mon), &(t.tm_mday));
t.tm_year -= 1900;
t.tm_mon --;
return mktime(&t);
}
void Date_Print(time_t t)
{
struct tm * ft = localtime(&t);
printf("%04d/%02d/%02d", ft->tm_year + 1900, ft->tm_mon + 1, ft->tm_mday);
}
void Number_Input(char * msg, int * x)
{
for(;;) {
printf("请输入%s,以回车结束:", msg);
scanf("%d", x);
if(*x < 0) *x = -*x;
if(*x) break;
printf("错误的输入,请重新输入。\n");
}
}
void String_Input(char * msg, char * str)
{
printf("请输入%s,以回车结束:", msg);
fflush(stdin);
fgets(str, szSTR, stdin);
}
int Type_Input(void)
{
int t = 0, i = 0;
for(;;) {
printf("请输入题目类别:");
for(i = 0; i < cntTYPE; i++) printf("%d - %s\t", i, Types[i]);
printf("\b\n");
t = getch() - '0';
if(t >= 0 && t <= cntTYPE) break;
printf("错误的输入\n");
}
return t;
}
fpos_t ASSG_Query(int number)
{
FILE * f = 0; fpos_t p = 0;
struct TAssignment a;
if(number < 1) return 0;
f = fopen(fileAssignments, "rb");
if(!f) {
fprintf(stderr, "题目文件无法打开\n");
return 0;
}
while(!feof(f)) {
fgetpos(f, &p);
fread(&a, sizeof(a), 1, f);
if(a.number == number) {
fclose(f);
return p + 1;
}
}
fclose(f);
return 0;
}
void ASSG_Append(void)
{
FILE * f = 0; fpos_t p = 0;
struct TAssignment a;
memset(&a, 0, sizeof(a));
Number_Input("题目编号", &(a.number ));
p = ASSG_Query(a.number );
if(p) {
fprintf(stderr, "该题目已存在。\n");
return ;
}
Number_Input("难度系数", &(a.level));
String_Input("题目名称", a.title );
a.type = Type_Input();
String_Input("技术要求", a.requirements);
String_Input("技术特点", a.features);
f = fopen(fileAssignments, "ab");
if(!f) {
fprintf(stderr, "题目文件无法打开\n");
return ;
}
fwrite(&a, sizeof(a), 1, f);
fclose(f);
}
void ASSG_Delete(void)
{
fpos_t p = 0; FILE * f = 0;
struct TAssignment a;
Number_Input("题目编号", &(a.number ));
p = ASSG_Query(a.number );
if(p == 0) {
fprintf(stderr, "该题目不存在。\n");
return ;
}
p--;
f = fopen(fileAssignments, "rb+");
if(!f) {
fprintf(stderr, "题目文件无法打开!\n");
return ;
}
fsetpos(f, &p);
memset(&a, 0, sizeof(a));
fwrite(&a, sizeof(a), 1, f);
fclose(f);
printf("删除成功!\n");
}
void PrintLine(void)
{
int i = 0; for(i=0; i<79; i++) putchar('-');
putchar('\n');
}
void ASSG_Report(void)
{
FILE * f = 0; struct TAssignment a; int r = 0;
f = fopen(fileAssignments, "rb");
if(!f) {
fprintf(stderr, "目前数据库中没有题目。\n");
return ;
}
printf("\n题目数据库\n");
while(!feof(f)) {
r = fread(&a, sizeof(a), 1, f);
if(r < 1) break;
if(a.number == 0) continue;
PrintLine();
printf("【编号】%d\t【类别】%s\t【难度系数】%d\t【题目】%s【技术要求】%s【技术特点】%s",
a.number , Types[a.type ], a.level ,
a.title , a.requirements , a.features );
}
fclose(f);
printf("\n\n");
}
void ASSG_Brief(int number)
{
struct TAssignment a; FILE * f = 0;
fpos_t p = ASSG_Query(number);
if(!p) {
printf("【尚未选题】\n");
return ;
}
p--;
f = fopen(fileAssignments, "rb");
fsetpos(f, &p);
fread(&a, sizeof(a), 1, f);
fclose(f);
printf("【题目编号】%d【类别】%s【难度系数】%d\t【题目】%s",
a.number , Types[a.type ], a.level ,a.title );
}
int ASSG_Choose(void)
{
int number = 0; fpos_t p = 0;
Number_Input("题目编号", &number);
p = ASSG_Query(number);
return p?number:0;
}
void ASSG_Menu(void)
{
int cmd = 0;
for(;;) {
printf("*****题目管理*****\n0-查看\t1-新增\t2-删除题目\t任意键返回\n");
cmd = getch() - '0';
switch(cmd) {
case 0: ASSG_Report(); break;
case 1: ASSG_Append(); break;
case 2: ASSG_Delete(); break;
default: return ;
}
}
}
/*项目信息*/
struct TProject {
int group_number; /*组号*/
char members[szGROUP][szNAME]; /*小组成员,编号0为组长*/
int assignment; /*选题*/
time_t date_open; /*开题日期*/
time_t date_close; /*结题日期*/
char teacher[szNAME];/*指导教师*/
int process; /*进度0~100%*/
char jobs[szSTR]; /*分工*/
int score; /*评分*/
};
void PROJ_Report(void)
{
struct TProject p; int r = 0;
FILE * f = fopen(fileProjects, "rb");
if(!f) {
fprintf(stderr, "目前没有开放项目信息。\n");
return;
}
printf("*****项目及小组信息*****\n");
while(!feof(f)) {
r = fread(&p, sizeof(p), 1, f);
if(r < 1) break;
if(p.group_number ==0) continue;
PrintLine();
printf("【组号】%d\t【指导教师】%s【组长】%s\n",
p.group_number , p.teacher , p.members[0]);
printf("【组员】%s %s %s %s\n", p.members[1], p.members[2], p.members[3], p.members[4]);
printf("【分工】%s", p.jobs );
ASSG_Brief(p.assignment );
if(p.assignment ) {
printf("【开题日期】"); Date_Print(p.date_open);
printf("【结题日期】"); Date_Print(p.date_close);
printf("【进度】%d%%【评分】%d\n", p.process, p.score );
}
}
fclose(f);
PrintLine();
}
fpos_t PROJ_Query(int number)
{
struct TProject p; fpos_t q = 0; int r = 0;
FILE * f = fopen(fileProjects, "rb");
if(!f) return 0;
while(!feof(f)) {
fgetpos(f, &q);
r = fread(&p, sizeof(p), 1, f);
if(r < 1) break;
if(p.group_number == number) {
fclose(f);
return q+1;
}
}
fclose(f);
return 0;
}
void PROJ_Append(void)
{
struct TProject p; fpos_t q = 0; FILE * f = 0; int i = 0;
memset(&p, 0, sizeof(p));
Number_Input("项目小组编号", &(p.group_number));
q = PROJ_Query(p.group_number );
if(q) {
fprintf(stderr, "该小组已经存在。\n");
return ;
}
printf("请录入组员信息,第一个组员是组长:");
for(i = 0; i< 5; i++) scanf("%s", p.members [i]);
printf("请输入指导教师姓名:"); scanf("%s", p.teacher );
String_Input("分工信息", p.jobs );
f = fopen(fileProjects, "ab");
fwrite(&p, sizeof(p), 1, f);
fclose(f);
printf("项目小组已创建,可以从菜单进行选题。\n");
}
void PROJ_Delete(void)
{
struct TProject p; fpos_t q = 0; FILE * f = 0;
memset(&p, 0, sizeof(p));
Number_Input("项目小组编号", &(p.group_number));
q = PROJ_Query(p.group_number );
if(q == 0) {
fprintf(stderr, "没有关于该小组的信息。\n");
return ;
}
q--;
f = fopen(fileProjects, "rb+");
fsetpos(f, &q);
memset(&p, 0, sizeof(p));
fwrite(&p, sizeof(p), 1, f);
fclose(f);
printf("该小组信息已删除!\n");
}
int PROJ_Invalid(int assignment)
{
struct TProject p; int sum = 0; int r = 0;
FILE * f = fopen(fileProjects, "rb");
if(!f) return 1;
while(!feof(f)) {
r = fread(&p, sizeof(p), 1, f);
if(r < 1) break;
if(p.group_number == 0) continue;
if(p.assignment == assignment) sum++;
}
fclose(f);
return sum<5?0:1;
}
void PROJ_Select(void)
{
struct TProject p; fpos_t q = 0; FILE * f = 0; time_t t = 0;
memset(&p, 0, sizeof(p));
Number_Input("项目小组编号", &(p.group_number));
q = PROJ_Query(p.group_number );
if(q == 0) {
fprintf(stderr, "没有关于该小组的信息。\n");
return ;
}
q--;
f = fopen(fileProjects, "rb");
fsetpos(f, &q);
memset(&p, 0, sizeof(p));
fread(&p, sizeof(p), 1, f);
fclose(f);
p.assignment = ASSG_Choose();
if(p.assignment==0) {
fprintf(stderr, "错误的题目编号。请在题库中查证后重新选题\n");
return ;
}
if(PROJ_Invalid(p.assignment)) {
fprintf(stderr, "已经有5个小组选择了该题目,请重新选题\n");
return ;
}
p.date_open = Date_Input("开题");
p.date_close= Date_Input("结题");
if(p.date_open > p.date_close) {
t = p.date_close ;
p.date_close = p.date_open;
p.date_open = t;
}
p.process = 0;
f = fopen(fileProjects, "rb+");
fsetpos(f, &q);
fwrite(&p, sizeof(p), 1, f);
fclose(f);
}
void PROJ_Progress(void)
{
struct TProject p; fpos_t q = 0; FILE * f = 0;
memset(&p, 0, sizeof(p));
Number_Input("项目小组编号", &(p.group_number));
q = PROJ_Query(p.group_number );
if(q == 0) {
fprintf(stderr, "没有关于该小组的信息。\n");
return ;
}
q--;
f = fopen(fileProjects, "rb");
if(!f) {
fprintf(stderr, "数据库无记录!\n");
return ;
}
fsetpos(f, &q);
memset(&p, 0, sizeof(p));
fread(&p, sizeof(p), 1, f);
fclose(f);
if(p.assignment < 1) {
fprintf(stderr, "尚未选题,请选题后再补登进度\n");
return ;
}
Number_Input("进度(0~100)", &(p.process ));
if(p.process <0 || p.process > 100) {
fprintf(stderr, "错误的进度\n");
return ;
}
f = fopen(fileProjects, "rb+");
fsetpos(f, &q);
fwrite(&p, sizeof(p), 1, f);
fclose(f);
printf("进度已经登记完成!\n");
}
void PROJ_Evaluate(void)
{
struct TProject p; fpos_t q = 0; FILE * f = 0;
memset(&p, 0, sizeof(p));
Number_Input("项目小组编号", &(p.group_number));
q = PROJ_Query(p.group_number );
if(q == 0) {
fprintf(stderr, "没有关于该小组的信息。\n");
return ;
}
q--;
f = fopen(fileProjects, "rb");
fsetpos(f, &q);
memset(&p, 0, sizeof(p));
fread(&p, sizeof(p), 1, f);
fclose(f);
if(p.assignment < 1) {
fprintf(stderr, "尚未选题,请选题后再进行评分\n");
return ;
}
Number_Input("评分(0~100)", &(p.score ));
if(p.score <0 || p.score > 100) {
fprintf(stderr, "错误的评分\n");
return ;
}
f = fopen(fileProjects, "rb+");
fsetpos(f, &q);
fwrite(&p, sizeof(p), 1, f);
fclose(f);
printf("成绩已经评定完成!\n");
}
void PROJ_Menu(void)
{
int cmd = 0;
for(;;) {
printf("*****项目管理*****\n0-查看\t1-新增\t2-删除\t3-选题\t4-登记进度\t5-打分\t任意键返回\n");
cmd = getch() - '0';
switch(cmd) {
case 0: PROJ_Report(); break;
case 1: PROJ_Append(); break;
case 2: PROJ_Delete(); break;
case 3: PROJ_Select(); break;
case 4: PROJ_Progress(); break;
case 5: PROJ_Evaluate(); break;
default: return ;
}
}
}
int main(void)
{
int cmd = 0;
for(;;) {
printf("*****欢迎使用学生实践管理软件*****\n0-题目管理\n1-项目管理\n其他任意键退出\n");
cmd = getch() - '0';
switch(cmd) {
case 0: ASSG_Menu(); break;
case 1: PROJ_Menu(); break;
default: return 0;
}
}
return 0;
}
❽ C语言中,puts可以用来一次输出结构变量的各个的成员吗
不可以,C是面向过程语言,除非你自己自定义函数来进行输出,如果你学了C++就可以用面向对象的思想做到
❾ C语言中如何实现多组数据输入输出
仔细认真看看下面的会对你有帮助的,嘿嘿
输入格式:有多个case输入,直到文件结束
输出格式:一行一个结果
Problem Description
Your task is to Calculate a + b.
Too easy?! Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output
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.
Sample Input
1 5
10 20
Sample Output
6
30
Author
lcy
Recommend
JGShining
#include <stdio.h>
int main()
{
int a,b;
while( scanf( "%d%d" , &a , &b ) != EOF ) //输入直到文件结尾
{
printf( "%d\n" , a+b ); //一行一个结果
}
return 0;
}
HDOJ1090
输入格式:先输入有case数,再依次输入每个case
输出格式:一行一个结果
#include <stdio.h>
Problem Description
Your task is to Calculate a + b.
Input
Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.
Output
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.
Sample Input
2
1 5
10 20
Sample Output
6
30
Author
lcy
Recommend
JGShining
int main()
{ int n,a,b;
scanf( "%d" , &n ); //输入的case数
while( n-- ) //控制输入
{ scanf( "%d%d" , &a , &b );
printf( "%d\n" , a+b ); //一行一个结果
}
return 0;
}
HDOJ1091
输入格式:每行输入一组case,当case中的数据满足某种情况时退出
输出格式:一行一个结果
Problem Description
Your task is to Calculate a + b.
Input
Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.
Output
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.
Sample Input
1 5
10 20
0 0
Sample Output
6
30
Author
lcy
Recommend
JGShining
#include <stdio.h>
int main()
{
int a,b;
while( scanf( "%d%d" , &a , &b ) && (a||b) ) //输入直到满足a和b均为0结束
{
printf( "%d\n" , a+b ); //一行一个结果
}
return 0;
}
HDOJ1092
输入格式:每组case前有一个控制输入个数的数,当这个数为0结束
输出格式:一行一个结果
#include <stdio.h>
Problem Description
Your task is to Calculate the sum of some integers.
Input
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
Sample Input
4 1 2 3 4
5 1 2 3 4 5
0
Sample Output
10
15
Author
lcy
Recommend
JGShining
int main()
{
int n,sum;
while( scanf( "%d" , &n ) && n ) //每组case前有一个控制该组输入数据的数,为0结束
{
int x;
sum = 0;
while( n-- ) //控制该组输入个数
{
scanf( "%d" , &x );
sum += x;
}
printf( "%d\n" , sum ); //一行一个结果
}
return 0;
}
HDOJ1093
输入格式:一开始有一个控制总的输入case的数,而每个case中又有一个控制该组输入数据的数
输出格式:一行一个结果
Problem Description
Your task is to calculate the sum of some integers.
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input.
Sample Input
2
4 1 2 3 4
5 1 2 3 4 5
Sample Output
10
15
Author
lcy
5
#include <stdio.h>
int main()
{
int casnum,n,sum;
scanf( "%d" , &casnum ); //控制总的输入case的数
while( casnum-- ) //控制总的输入个数
{
int x;
sum = 0;
scanf( "%d" , &n ); //每个case中控制该组输入个数
while( n-- )
{
scanf( "%d" , &x );
sum += x;
}
printf( "%d\n" , sum ); //一行一个结果
}
return 0;
}
HDOJ1094
输入格式:总的case是输到文件结尾,每个case中的一开始要输入一个控制该组个数的数
输出格式:一行一个结果
Problem Description
Your task is to calculate the sum of some integers.
Input
Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.
Output
For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.
Sample Input
4 1 2 3 4
5 1 2 3 4 5
Sample Output
10
15
6
#include <stdio.h>
int main()
{
int n,sum;
while( scanf( "%d" , &n ) != EOF ) //输出到文件结尾
{
int x;
sum = 0;
while( n-- ) //控制该组输入个数
{
scanf( "%d" , &x );
sum += x;
}
printf( "%d\n" , sum ); //一行一个结果
}
return 0;
}
HDOJ1095
输入格式:输入直到文件结束
输出格式:一行一个结果,结果输完后还有一个blank line
Problem Description
Your task is to Calculate a + b.
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
Output
For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.
Sample Input
1 5
10 20
Sample Output
6
30
7
#include <stdio.h>
int main()
{
int a,b;
while( scanf( "%d%d" , &a , &b ) != EOF ) //输入直到文件结束
{
printf( "%d\n\n" , a+b ); //一行一个结果,结果输完后还有一个回车
}
return 0;
}
HDOJ1096
输入格式:一开始输入总的case数,每组case一开始有控制该组输入个数的数
输出格式:一行一个结果,两个结果之间有一个回车,注意最后一个case的处理。
Problem Description
Your task is to calculate the sum of some integers.
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
Output
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.
Sample Input
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
Sample Output
10
15
6
#include <stdio.h>
int main()
{
int casnum,n,sum;
scanf( "%d" , &casnum ); //总的输入case数
while( casnum-- ) //控制输入组数
{
int x;
sum = 0;
scanf( "%d" , &n ); //控制每组的输入个数
while( n-- )
{
scanf( "%d" , &x );
sum += x;
}
printf( "%d\n" , sum ); //一行一个结果
if( casnum ) printf( "\n" ); //两两结果之间有一个回车,最后一个结果后面没有
}
return 0;
}
❿ C语言中如何实现多组数据输入输出
C语言中实现多组数据输入输出主要有两种方式:
1.首先输入一个n,表示将有n个输入输出,例如:
#include<stdio.h>
intmain()
{
intn,a;
scanf("%d",&n);
while(n--){
scanf("%d",&a);
printf("输出:%d ",a);
}
return0;
}
/*
运行结果:
3
255
输出:255
156
输出:156
125
输出:125
*/
2.使用while(scanf("%d",&n)!=EOF){}语句,直达输入ctrl+z,结束输入,例如:
#include<stdio.h>
intmain()
{
inta;
while(scanf("%d",&a)!=EOF){
printf("输出:%d ",a);
}
return0;
}
/*
运行结果:
54
输出:54
5156
输出:5156
21
输出:21
^Z
*/