C++: Solution bài tập phần vòng lặp
Solution ý 13 Bài tập 3:
#include<Windows.h>
#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
int main(){
unsigned long long n,dc,ds,m,sum,mx;
vector<unsigned long long> d(dc);
cout<<"Nhap N nguyen duong: ";
cin>>n;
while(n<0){
cout<<"Hay nhap N nguyen duong: ";
cin>>n;
}
dc=0;
do{
ds=n/pow(10,dc+1);
dc+=1;
}while(ds>=1);
cout<<"So chu so trong N: "<<dc<<endl;
m=n;
for(int i=dc-1;i>=0;i--){
ds=m/pow(10,i);
m=m-ds*pow(10,i);
d.push_back(ds);
}
sum=0;
mx=0;
for(int j=0;j<=d.size()-1;j++){
sum+=d.at(j);
if(d.at(j)>mx){
mx=d.at(j);
}
}
cout<<"Tong cac chu so trong N: "<<sum<<endl;
cout<<"Chu so lon nhat trong N: "<<mx<<endl;
Sleep(5000);
return 0;
}
Solution Bài tập 7:
#include<bits/stdc++.h>
using namespace std;
main(){
int a,b,c,nam,ngay,tong;
cin>>a>>b>>c;
c=c-1;
nam=(a-1950)*365;
if(b==1){
ngay=120+c;
}else if(b==6){
ngay=151+c;
}else if(b==7){
ngay=181+c;
}else if(b==8){
ngay=212+c;
}else if(b==9){
ngay=243+c;
}else if(b==10){
ngay=273+c;
}else if(b==11){
ngay=304+c;
}else if(b==12){
ngay=334+c;
}
tong = nam + ngay;
if(b<=2){
for(int i=1950;i<a;i++){
if(i%4==0){
tong++;
}
}
}else{
for(int i=1950;i<=a;i++){
if(i%4==0){
tong++;
}
}
}
cout<<tong%7;
}
Solution Bài tập 8:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin, s);
int tong_chan=0, tong_le=0;
for(int i=0; i<s.length(); i++){
if(i%2==0){
if(s[i]=='1'){
tong_chan++;
}
}
else if(i%2==1){
if(s[i]=='1'){
tong_le++;
}
}
}
if((tong_chan==tong_le) || (abs(tong_chan-tong_le)>=2)){
cout<<"0";
}
else{
if(tong_chan>tong_le){
int vt=0;
for(int i=0; i<s.length(); i+=2){
if(s[i]=='1'){
vt= i+1;
break;
}
}
cout<<vt;
}
else if(tong_le>tong_chan){
int vt=0;
for(int i=1; i<s.length(); i+=2){
if(s[i]=='1'){
vt= i+1;
break;
}
}
cout<<vt;
}
}
}