计算一系列数的和
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question
两次 getline()
,并利用 stoi()
转换函数。
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { int sum(0); string str1, str2; while(getline(cin, str1)) { sum = 0; stringstream S(str1); while (getline(S, str2, ' ')) sum += stoi(str2); cout << sum << endl; } return 0; }
#include<stdio.h> int main() { int i,j,k,sum; scanf("%d%d%d",&i,&j,&k); sum=i+j+k; printf("%d\n",sum); }
package main import ( "fmt" "bufio" "os" "strconv" ) func main() { inputReader := bufio.NewReader(os.Stdin) for{ input,_ := inputReader.ReadBytes('\n') if len(input)== 0{ break } sum := 0 for i:= range input{ n,_:= strconv.Atoi(string(input[i])) sum += n } fmt.Println(sum) } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while(scan.hasNextLine()) { String s = scan.nextLine(); String[] strs = s.split(" "); int sum = 0; for(int j = 0; j < strs.length; j++) { sum += Integer.parseInt(strs[j]); } System.out.println(sum); } } }
while(true) { const line = readline(); if (line) { const nums = line.split(' '); let sum = 0; for(let i = 0; i < nums.length; i++) { sum += Number(nums[i]); } console.log(sum); } else { break; } }
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args){ Scanner cin=new Scanner(System.in); while(cin.hasNextLine()){ int sum=0; String[] str=cin.nextLine().split(" "); int len=str.length; for(int i=0;i<len;i++){ sum+=Integer.parseInt(str[i]); } System.out.println(sum); } } }