❶ 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
*/