一元二次方程根:
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
double a,b,c;
cin >> a>>b>>c;
double q = b*b - 4*a*c;
if(q>0)
{
double s1 = (-b+sqrt(q))/(2*a);
double s2 = (-b-sqrt(q))/(2*a);
if(s1 == (int)s1)
cout << (int)s1<<endl;
else
cout << fixed << setprecision(2) << s1<<endl;
if(s2 == (int)s2)
cout <<(int)s2;
else
cout << fixed << setprecision(2) << s2;
}else if(q==0)
{
double s = -b/(2*a);
if(s==(int)s)
cout << (int)s;
else
cout <<fixed<<setprecision(2)<<s;
}else
cout <<"无实根";
return 0;
}
ISBN校验码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
char ch;
cin >> str;
int s = 0;
int k = 1;
for(int i=0;i<str.length()-1;i++)
{
if(str.at(i)>='0' && str.at(i)<='9')
{
s += (str.at(i) - '0')*k;
k++;
}
}
s = s%11; //ศกำเส
if(s<10)
ch = '0'+ s;
else
ch = 'X';
if(str.at(str.length()-1) == ch)
cout <<"Right";
else
cout << str.substr(0,str.length()-1)<<ch;
return 0;
}
时间差:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int hourstart,minstart;
int hourend,minend;
cin >> hourstart >> minstart >> hourend >> minend;
int hh,mm;
hh = hourend - hourstart;
mm = minend - minstart;
if(mm <0)
{
hh -=1;
mm += 60;
}
cout << hh<<" "<<mm;
return 0;
}
1-n中有多少个x:
#include <iostream>
using namespace std;
//计算数字n中有多少个x
int fun(int n,int x)
{
int count = 0;
int t;
while(n)
{
t = n%10;
if(t==x)
count++;
n/=10;
}
return count;
}
int main()
{
int n,x,count=0;
cin >> n>>x;
for (int i=1;i<=n;i++)
{
count += fun(i,x);
}
cout << count;
return 0;
}
国王的金币:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int k;
int ts=0;
int dz = 1;
long long jb = 0;
cin >> k;
for(;ts<k;ts+=dz)
{
jb += dz*dz;
dz++;
}
jb += (k-(ts-dz)-1)*dz;
cout << jb;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
cout << "输入 a, b 和 c: ";
cin >> a >> b >> c;
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (discriminant == 0) {
cout << "实根相同:" << endl;
x1 = (-b + sqrt(discriminant)) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "实根不同:" << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0;
}