蓝桥杯2012(第三届)第八题

xiaoxiao2021-02-28  37

#include<iostream> #include<stdio.h> #include<string> using namespace std; int n; int a[7]; int getSum(int n){ int sum; while(n > 9){ sum = 0; int t = n; while(t != 0){ sum += t; t /= 10; } n = sum; } return sum; } int main() { string str = ""; scanf("%d",&n); getchar();//读取第一个换行符 while(n--){ //cout <<str; // getline(cin,str); cin >> str; for(int i = 0; i < str.length(); ++i){ a[i%6] += str[i]; } for(int i = 0; i < a[i]; ++i){ a[i] = getSum(a[i]); } //str.clear(); for(int i = 0; i <6; ++i){ printf("%d",a[i]); a[i] = 0; } printf("\n"); } return 0; } #include<iostream> #include<stdio.h> #include<string.h> #include<string> using namespace std; int n; int a[7]; char c[1000]; int getSum(int n){ int sum; while(n > 9){ sum = 0; int t = n; while(t != 0){ sum += t; t /= 10; } n = sum; } return sum; } int main() { scanf("%d",&n); //getchar();//读取第一个换行符 while(n--){ scanf("%s",c); int len = strlen(c); for(int i = 0; i < len; ++i){ a[i%6] += c[i]; } for(int i = 0; i < a[i]; ++i){ a[i] = getSum(a[i]); } for(int i = 0; i <6; ++i){ printf("%d",a[i]); a[i] = 0; } printf("\n"); } return 0; }

getline(cin,str);读取的是string 对象 注意读取前面需要 getchar()去除读n带来的换行符 scanf("%s")类似于cin  但是 它会忽略读取的一行中的空格 自动进行拼接(去除换行)

getline 与scanf下一次读取的字符串 都会覆盖之前的字符串; 字符串读取总结
转载请注明原文地址: https://www.6miu.com/read-2625604.html

最新回复(0)