简体   繁体   English

根据测试用例扫描多个变量?

[英]Scan multiple variables depending on test cases?

I want to calculate sum of arithmetic progression in which we have to take 3 variables from user. 我想计算算术级数的总和,其中我们必须从用户那里获取3个变量。 a=first number, b= step size/increment, c=length of sequence. a =第一个数字,b =步长/增量,c =序列长度。 If there are more than 1 test case , say three, then I have to scan a,b,c three time. 如果有多个测试用例,比如说三个,那么我必须扫描a,b,c三次。 How to do this? 这个怎么做? Eg scanf (" %d %d %d", a,b,c); 例如scanf(“%d%d%d”,a,b,c); 3 times without affect initial values in first test case. 3次不影响第一个测试用例的初始值。

If you know no of test cases read it first and store it in a variable. 如果您不知道测试用例,请先阅读并将其存储在变量中。

int calculate_ap(int a, int b, int c)
{
    //Implement function to calculate Arithmetic progression and return the result
}

int main()
{
    int test_cases = 0;
    int a, b, c;
    scanf("%d", &test_cases); //Reads no of test cases
    while(test_cases--)
   {
        scanf("%d, %d, %d", &a, &b, &c); //read A, B, C
        printf("%d\n", calculate_ap(a, b, c));
   }

}

Hope this helps. 希望这可以帮助。

put scanf (" %d %d %d", a,b,c); 把scanf(“%d%d%d”,a,b,c); inside a loop then in the same loop do your coding what you want to do with the values, make sure to write this line scanf (" %d %d %d", a,b,c); 在一个循环内,然后在同一循环中对值进行编码,请确保将此行写成scanf(“%d%d%d”,a,b,c); before your working code. 在您的工作代码之前。 or if you want to save values then declear array for a,b,c like aValues[] bValues[] cValuesthen[] then put your data inside the array through loop. 或者,如果您要保存值,则为a,b,c清除数组,例如aValues [] bValues [] cValuesthen [],然后通过循环将数据放入数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM