如何printf每个循环结果 [英] how to printf each result of looping

查看:84
本文介绍了如何printf每个循环结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打印N个整数的总和。



输入



第一行包含整数1≤ T≤100,表示测试用例的数量。每个案例以数字N开头,第二行包含由一个空格字符分隔的N个整数。



输出



T行,每行,打印输入数字的总和。



样本输入

Print sum of N integer numbers.

Input

The first line contains integer 1 ≤ T ≤ 100, denoting the number of test cases. Each case start with a number N, and the second line contains N integers separated by one space character.

Output

T lines, in each line, print sum of input numbers.

Sample Input

2
5
54 78 0 4 9
3
1 2 3





样本输出



Sample Output

145
6





我想让printf最后全部回答..

this我尝试了什么:



I want make printf all answer at the end..
this what I tried :

#include<stdio.h>
int main()
{
    int T, N, test, array[20], sum[20];
    do {
        printf ("How Many Test Case \n");
        scanf ("%d", &T);
        if (T<1 || T>100){
            printf ("Please Input Integer 0<T<100 \n");
        }
    }    while (T<1 || T>100);

    for (test=0; test<T ; test++){
        printf ("How many Integers to sum? \n");
        scanf ("%d", &N);
        int i=0;
        while(i<N)
        scanf("%d",&array[i++]);

        int m=0, sum=0;
        for (m=0; m<N; m++)
        {
            sum = sum + array[m];
            sum = sum[j]
        }
    for (x=0; x<T; x++)
    printf ("%d\n", sum[j]);

        }
    return 0;
    }

推荐答案



代码大多是正确的。

似乎问题在于这部分:


code is mostly correct.
It seems the problem is in this part :
for (m=0; m<N; m++)//calling this loop1
{
    sum = sum + array[m];
    sum = sum[j]
}

for (x=0; x<T; x++)//calling this loop2
    printf ("%d\n", sum[j]);



首先我没有看到j在任何地方定义。

建议::




first of all i do not see j defined anywhere.
suggestion ::

#include<stdio.h>
int main()
{
    int T, N, test, array[20], sum[20];
	//declare another variable to store sum temporarily
	int tempSum;
    do
	{
        printf ("How Many Test Case \n");
        scanf ("%d", &T);
        if (T<1 || T>100){
            printf ("Please Input Integer 0<T<100 \n");
        }
    }    while (T<1 || T>100);
 
    for (test=0; test<T ; test++)
	{
        printf ("How many Integers to sum? \n");
        scanf ("%d", &N);
        int i=0;
        while(i<N)
        scanf("%d",&array[i++]);
 
        int m=0, tempSum=0;
        for (m=0; m<N; m++)
        {
            tempSum = tempSum + array[m];
        }
		sum[test] = tempSum;
	}	
		for (x=0; x<T; x++)
		printf ("%d\n", sum[x]);
 
     return 0;
    }



没有通过执行测试,所以请自己测试。

希望它能正常工作!!


did not test this by executing so please test yourself.
hope it works !!


这是正确的代码:

This is the correct code:
 #include<stdio.h>
int main()
{
    int T, N, test, array[20], sum[100];
    do
	{
        printf ("How Many Test Case \n");
        scanf ("%d", &T);
        if (T<1 || T>100)
		{
            printf ("Please Input Integer 0<T<100 \n");
        }
    }    while (T<1 || T>100);
 
    for (test=0; test<T ; test++)
	{
        printf ("How many Integers to sum? \n");
        scanf ("%d", &N);
        int i=0;
        while(i<N)
        	scanf("%d",&array[i++]);
 
        int m=0;
		sum[test]=0;
        for (m=0; m<N; m++)
            sum[test] += array[m]; 
    }

	int x;
    for (x=0; x<T; x++)
    	printf ("%d\n", sum[x]);

    return 0;
}


这篇关于如何printf每个循环结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆